#!/usr/local/bin/kermit +
#
# This is the DELIVER script from page 453 of "Using C-Kermit", 2nd Edition,
# with a couple typos fixed.  C-Kermit 6.0 or K95 1.1.8 or later required,
# and it also requires that the definition of the ACCESS macro (see text,
# Chapter 19) from the standard C-Kermit initialization file be defined
# already.  This script uses the ACCESS macro to make a connection to the
# desired service and send it the desired file or files.  If the transfer
# fails (e.g. because the connection is broken), a new connection is made
# and the transfer is resumed from the point of failure.
#
# In UNIX, this can be used as a "kerbang" script, and invoked from the UNIX
# prompt with the service name, filename, and optional reconnection limit as
# arguments, e.g. "deliver headquarters budget.xls 500".  The Kerbang
# features require C-Kermit 7.0.
#
# Authors: C. Gianone & F. da Cruz, Columbia University, 1997.

COMMENT - DELIVER macro
;  \%1 = service name
;  \%2 = filename (can be wild)
;  \%3 = Maximum tries allowed (default = 1000)
;
define DELIVER {
    local \%i \%p             ; Local variables
    set input timeout proceed
    if not def \%3 def \%3 1000
    while not def \%p {
        askq \%p { Password for \%1 }
    }
    set file type binary      ; Transfer mode must be binary
    for \%i 1 \%3 1 {
        if > \%i 1 {          ; If not the first try
            hangup            ; hang up and print message
            echo CONNECTION BROKEN.
            Echo Reconnecting...
        }
        access \%1 \%p        ; Make the connection and log in
        if fail continue      ; Keep trying
        out kermit\13         ; Start Kermit on remote system
        input 10 >            ; Wait for prompt
        out receive\13        ; Tell it to RECEIVE
        input 10 KERMIT READY ; Wait for READY message
        pause 1               ; Plus a second for safety
        resend \%2            ; RESEND the file(s)
        if success break
    }
    if > \%i \%3 -
      end 1 FAILED after \%3 tries.
    output logout\13          ; Success, log out
    pause 5                   ; Give it time...
    hangup                    ; Hang up
    end 0 \%2 delivered after \%i tries
}

# This part requires C-Kermit 7.0...

if LLT \v(version) 60000 end 0
define usage { exit 1 Usage: \fbasename(\v(cmdfil)) service file(s) }

if equal "\%0" "\v(cmdfil)" {
    if < \v(argc) 3 usage
    if not def access exit 1 Fatal - ACCESS macro is not defined.
    deliver {\%1} {\%2} \%3
    exit \v(status)
}
