# l o g r o t a t e
#
# C-Kermit 7.0 includes a connection log feature.  If you enable it by
# putting a LOG CONNECTIONS command in your C-Kermit startup file, each
# new connection is appended to the log.  Therefore your connection log
# file just grows and grows.  This script (if included in your C-Kermit
# startup file) automatically starts a new log at the beginning of each
# month, and keeps 4 months' worth of logs, discarding older ones.
#
# It works by comparing the yyyymm portion of the modification date
# (\fdate()) of the given file (\%1) with the current yyyymm.  If they
# differ, the current file has the yyyymm suffix (from its most recent
# modification date) appended to its name.  Then we search through all
# other such files, find the oldest one, and delete it.  Thus the current
# log, plus the logs from the most recent four months, are kept.  This is
# all done automatically every time you start C-Kermit.
#
define LOGROTATE {                    ; Define LOGROTATE macro
    local \%i \%m \%d \%n \%f MAX
    def MAX 4                         ; How many months to keep
    if not def \%1 -                  ; No argument given
      end 1 \%0: No filename given
    if  < 1 \ffiles(\%1) -            ; Exactly 1 file must match
      end 1 \%0: "\%1" - File not found
    if > 1 \ffiles(\%1) -
      end 1 \%0: "\%1" - Too many files match
    .\%d := \fsubstr(\fdate(\%1),1,6) ; Arg OK - get file year & month
    if = \%d -                        ; Compare file year & month
      \fsubstr(\v(ndate),1,6) -       ; with current year & month
	end 0                         ; Same year & month - done
    rename \%1 \%1.\%d                ; Different - rename file
    .\%n := \ffiles(\%1.*)            ; How many old files
    if < \%n \m(MAX) end 0            ; Not enough to rotate
    .\%m := \%1.999999                ; Initial compare string
    for \%i 1 \%n 1 {                 ; Loop thru old logs
       .\%f := \fnextfile()           ; Get next file name
       if llt \%f \%m .\%m := \%f     ; If this one older remember it
    }
    delete \%m                        ; Delete the oldest one
}
log connections                       ; Now open the (possibly new) log
logrotate \v(home)CX.LOG              ; Run the LOGROTATE macro
