# Kermit Macro TPUT (Kermit 95 FTP client)
#
# Sends a single file in text mode to an FTP server on an FTP
# connection that must already be open.  Compensates for a bug
# in Kermit 95 2.1.3 that inhibits proper end-of-line conversion
# in text-mode FTP uploads to Unix.  To be used only with Unix-based
# FTP servers.  Usage:
#
#   tput filename
#
# where "filename" is the name of a single text file.
# Creates a temporary file with Unix-style line terminators,
# sends the temporary file in binary mode (under the original file's
# name), then deletes it.
#
# Add this macro definition to your \v(appdata)k95custom.ini
# file and then use "tput" instead of "put" to upload text files
# with FTP from Windows to Unix.
#
# F. da Cruz, Columbia University, 8 April 2004
#
define TPUT {
    local \%c \%w \%i file tmpfile 
    .file := \fcontents(\%1)
    if not = \v(argc) 2 end 1 "Usage: \%0 filename"
    if not exist \m(file) end 1 "?Name of single existing file required"
    fopen /read \%c \m(file)
    if fail end 1
    set flag off
    for \%i 1 1000 1 {
        .tmpfile := \m(file).\frandom(10000)
        if not exist \m(tmpfile) {
            set flag on
            break
        }
    }
    if not flag end 1 "?Can't find unique temp file name"
    fopen /write /binary \%w \m(tmpfile)
    if fail end 1 
    while not \f_eof(\%c) {
        fread /line \%c line
        if fail end 1 "File read error: \m(file)"
        fwrite /string \%w \m(line)\10
        if fail end 1 "File write error: \m(tmpfile)"
    }
    fclose all
    ftp put /binary \m(tmpfile) \m(file)
    if fail end 1 "FTP PUT \m(tmpfile) failed"
    ldelete \m(tmpfile)
    end 0 "TPUT \m(file) OK"
}
