#!/usr/local/bin/kermit +
;
; i n s t a l l
;
; Script to install new C-Kermit binaries from a staging area to the
; kermit/test/bin area at the Kermit website.  For each binary, any old ones
; are deleted from the target area before moving the new one in because there
; is not enough room in the target area for all the old ones and all the new
; ones at the same time.  Runs in UNIX only.  C-Kermit 7.0 Beta.07 required.
; See the "cleandups" script for an explanation of the filenames, etc.
;
; 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.
;
; Author: F. da Cruz, the Kermit Project, Columbia University, May 1999
;
; Parameters:
;  \%1 = Beta number (one or two digits, required)
;  \%2 = Function: INSTALL or TEST
;  \%3 = Source directory
;  \%4 = Target directory (default = /pub/ftp/kermit/test/bin)
;
local \%b \%e \%f \%j \%k \%l \%m \%n \%x \%y   ; Local scalars
local \&a[] \&b[]                               ; Local arrays
local usage found freed lowest installing count ; Local macros

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

if LLT \v(version) 700000 exit 1 \%m: C-Kermit 7.0 required
if not equal \v(system) UNIX exit 1 \%m: UNIX required

define usage {
    exit 1 Usage: \%m betanumber {INSTALL,TEST} sourcedir targetdir
}
; 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!

undef installing                ; Assume TEST - no touching files for real.
if not def \%2 .\%2 = TEST
if match INSTALL \%2* .installing = 1
else if not match TEST \%2* usage

; Get and verify source directory

if not def \%3 .\%3 = .         ; Default is current directory
cd \%3
if fail exit 1 Can't cd to "\%3"

; Get and verify destination directory

if not def \%4 .\%4 = /pub/ftp/kermit/test/bin
.\%4 := \fpathname(\%4)
if eq \fright(\%4,1) / .\%4 := \fstripx(\%4,/)
if not dir \%4 exit 1 \%4 is not a directory.

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

.\%e = 196                      ; C-Kermit edit number
.lowest = 2                     ; C-Kermit 7.0.195 Betas started off at 2.
.found = 0
.freed = 0
.count = 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 Installing binaries for Beta.\%b; lowest = \m(lowest); previous = \%x
if def installing echo \%3: REALLY INSTALLING...
else echo \%3: JUST TESTING and not really installing...

; 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*"
echo Matches: \%n

sort a                          ; Sort the file-list array.

for \%i 1 \%n 1 {               ; Loop through file list for this beta.
    if exist \%4/\&a[\%i] {
        if not newer \&a[\%i] \%4/\&a[\%i] { ; If target file already exists
            echo [SKIP] \&a[\%i]             ; and it's not older than source
            continue                         ; skip this file.
        }
    }
    for \%j \%x \m(lowest) -1 { ; Search for previous versions of same binary.
        .\%l := \flpad(\%j,2,0)
        .\%f := \%4/\freplace(\&a[\%i],b\%b,b\%l)
        if exist \%f {                       ; Found one
            increment found                  ; Count it
            increment freed \fsize(\%f)      ; Get file size for stats 
            if def installing {              ; If installing
                delete /verbose \%f          ; delete it verbosely
                if fail exit 1 FATAL - DELETE \%f
            } else {                         ; Otherwise only listing
                echo [DELE] \%f              ; Say we would have deleted it
            }
        }
    }
    for \%j \%e-1 193 -1 {      	     ; Same deal for previous edits
        .\%f := \%4/\freplace(\&a[\%i],\%eb\%b,\%j*)
        .\%y := \ffiles(\%f,&b)
        if = \%y 0 continue
        sort /rev b
        set flag on
        for \%k 1 \%y 1 {
            increment found
            increment freed \fsize(\&b[\%k])
            if def installing {
                delete /verbose \&b[\%k]
                if fail exit 1 FATAL - DELETE \&b[\%k]
            } else {
                echo [DELE] \&b[\%k]
            }            
        }
    }
    if def installing {                      ; If installing
        copy /list \&a[\%i] \%4/\&a[\%i]     ; Install the new binary
        if fail exit 1 FATAL - COPY \&a[\%i]
    } else {                                 ; Otherwise
        echo [COPY] \&a[\%i] => \%4          ; just print a message
    }
    increment count                          ; Count this file
}
undef \%m
if not def installing def \%m (NOT REALLY)
echo Binaries installed: \m(count) \%m
echo Files deleted:      \m(found) \%m
echo Bytes Freed:        \m(freed) \%m

exit 0
