#!/usr/local/bin/kermit +
;
; c l e a n d u p s
;
; Script to clean out duplicate C-Kermit 6.1/7.0 test binaries at the
; Kermit website.  Illustrates some of C-Kermit 7.0's new features.
;
; F. da Cruz, the Kermit Project, Columbia University, March 1999
;
; Change the first line to have the path of C-Kermit 7.0 on your computer
; and give this file execute permission.  Then run this script exactly as if
; it were a shell script.
;
; Parameters:
;  \%1 = Beta number (one or two digits, required)
;  \%2 = Function: LIST-ONLY or DELETE (default = DELETE, abbreviations OK)
;  \%3 = Directory (default = current directory)
;
; Examples:
;  cleandups                     ; Missing Beta number gives usage message.
;  cleandups 5                   ; Beta.05, DELETE, default directory.
;  cleandups 5 d                 ; Beta.05, DELETE, default directory.
;  cleandups 5 delete            ; DELETE or LIST-ONLY can be spelled out
;  cleandups 5 del               ; or abbreviated.
;  cleandups 5 l /some/other/directory ; List dups in /some/other/directory.
;
; The files have names like ck<x><vvv>[ab]<nn><text>, where:
; <x>    Is a single letter or digit denoting the platform
; <vvv>  Is the C-Kermit edit number (e.g. 193, 194, 195, ...)
; [ab]   Is "a" for an Alpha test and "b" for a Beta test
; <nn>   The two-digit Alpha or Beta test number.
; <text> Is arbitrary text specific to the platform.
;
; For example, if we have just installed a new file cku195b05.aix41, this
; script will delete (or list) cku195b04.aix41, cku195b03.aix41, and so on,
; as well as any cku*.aix41 files earlier than version 195, in the target
; directory.  Similarly for all other old versions superseded by newer ones.
;
; When listing, the new file is shown first followed by the old files to
; be deleted in reverse order; each such set of files is set off by a
; dashed line.
;
local \%b \%e \%f \%j \%k \%l \%m \%n \%x \%y ; Local scalars
local  \&a[] \&b[]                            ; Local arrays
local usage found freed lowest deleting       ; Local macros

.\%m := \fbasename(\%0)                       ; Script name for usage message

if LLT \v(version) 700000 exit 1 C-Kermit 7.0 required

define usage {
    exit 1 Usage: \%m betanumber [ { DELETE, LIST-ONLY } [ directory ] ] }
}
; Check parameters...
;
if not def \%1 usage
if not numeric \%1 exit 1 Beta number not numeric
if < \%1 1 exit 1 Beta number not positive
if > \%1 99 exit 1 Beta number more than two digits -- Too many Betas!

.deleting = 1                   ; Assume LIST-ONLY - no deleting.
if not def \%2 .\%2 = DELETE
if match LIST-ONLY \%2* .deleting = 0
else if not match DELETE \%2* usage
if not def \%3 .\%3 = .
cd \%3
if fail exit 1 Can't cd to "\%3"

; Define constants (note new assignment operators)...

.\%e = 196                      ; C-Kermit edit number
.lowest = 2                     ; C-Kermit 7.0.19x Betas started off at 2.
.found = 0
.freed = 0

.\%x ::= \%1 - 1                ; Beta number before this one.
.\%b := \flpad(\%1,2,0)         ; Current beta number left-padded with zero.

; Say what will happen...

echo Checking duplicates for Beta.\%b; lowest = \m(lowest); previous = \%x
if \m(deleting) echo \fpathname(\%3): DELETING old versions...
else echo \fpathname(\%3): LISTING old versions but not deleting them...

; The following command assigns the list of files that matches the pattern
; for this Beta version to the array \&a[] and the number of files to \%n.

.\%n := \ffiles(ck*\%eb\%b*,&a)
if < \%n 1 exit 1 No files match "ck*\%eb\%b*"

sort a                          ; Sort the file-list array.

for \%i 1 \%n 1 {               ; Loop through file list for this beta.
    set flag off                ; Assume no old files matching this one.
    for \%j \%x \m(lowest) -1 { ; Search for previous versions of same binary.
        .\%l := \flpad(\%j,2,0)
        .\%f := \freplace(\&a[\%i],b\%b,b\%l)
        if exist \%f {                ; Found one.
            increment found           ; Count it.
            if \m(deleting) {         ; If deleting
                .\%9 := \fsize(\%f)   ; get this one's size for stats 
                delete /verbose \%f   ; and delete it verbosely.
                if fail exit 1        ; But stop if deletion fails.
                increment freed \%9   ; Accumulate bytes freed.
            } else {                  ; Otherwise only listing.
                if not flag dir /message:(KEEP) \&a[\%i] ; List first one once
                dir /message:(DELETE) \%f ; List this one
            }
            set flag on               ; Found one - remember.
        }
    }
    for \%j \%e-1 193 -1 {            ; Same deal for previous edits
        .\%f := \freplace(\&a[\%i],\%eb\%b,\%j*)
        .\%y := \ffiles(\%f,&b)
        if = \%y 0 continue
        sort /rev b
	if not flag dir /message:(KEEP) \&a[\%i]
        set flag on
        for \%k 1 \%y 1 {
            increment found
            if \m(deleting) {
                .\%9 := \fsize(\&b[\%k])
                delete /verbose \&b[\%k]
                if fail exit 1
                increment freed \%9
            } else {
                dir /message:(DELETE) \&b[\%k]
            }            
        }
    }
    if flag echo {---------}
}

echo Files: \m(found) 
echo Freed: \m(freed)

exit 0
