#/usr/local/bin/kermit +
;
; f t p d i r e c t o r y
;
; Gets a directory listing from an FTP server
; that shows the size and full modification date and time of each file.
; The date and time are in UTC (GMT).
;
; Illustrates: how to get remote file date/time and size from server.
;   How to get a filename list from the server.
;   How to get file date/time and size info from the server.
;   How to read a local file.
;
; This example merely prints the file information but you could also
; use it to make decisions about whether to fetch each file, etc.
;
; Arguments:
;  \%1 hostname (empty if you already have an FTP connection)
;  \%2 username on host (empty for anonymous)
;  \%3 directory on host (empty if you are already CD'd as desired)
;  \%4 file specification (default "*")
;
; F. da Cruz, Columbia University, February 2003
;
; Modified Aug 2004 to show how to identify server directories.

set quiet on                               ; No messages
set exit warning off                       ; No warnings
.listname := \v(tmpdir)ftplist.\v(pid)     ; Temp file name
if defined \%1 {                           ; Open FTP connection
    if defined \%2 {                       ; Log in as real user
        ftp open \%1 /user:\%2
    } else {                               ; or anonymously
        ftp open \%1 /anonymous
    }
    if fail exit 1                         ; Check FTP OPEN
} else if not \v(ftp_connected) {          ; Check connection
    exit 1 "No FTP connection"
}
if not \v(ftp_loggedin) {                  ; Check if logged in
    exit 1 "Not logged in to FTP server"
}
if defined \%3 {                           ; CD to disired directory
    ftp cd \%3
    if fail exit 1                         ; Check
}
if not def \%4 def \%4 *                   ; Default filespec if not given

ftp mget /namelist:\m(listname) \%4        ; Get list of names into file
if fail exit 1                             ; Check

fopen /read \%c \m(listname)               ; Open name-list file
if fail exit 1                             ; Check
while true {                               ; Loop to read each line
    fread /line \%c name                   ; Read a name
    if fail break                          ; Check for EOF
    ftp quote MDTM \m(name)                        ; Get file's modtime
    if fail exit 1 "Server does not support MDTM"  ; Check
    .time := \v(ftp_message)                       ; Save
    ftp size \m(name)                              ; Get file's size
    if fail exit 1 "Server does not support SIZE"  ; Check
    .size := \v(ftp_message)                       ; Save

; The next bit checks to see if the file is a directory.
; This works only if the server returns directory names in its NLST response.
; (Most servers do not.)

    undef dir                              ; Assume it's not a directory
    ftp cd \m(name)                        ; Try to CD to it
    if success {                           ; If it works...
        .dir = "(DIRECTORY)"               ; it's a directory
        ftp cdup                           ; Go back up.
    }
    echo "\flpad(\m(size),12) \fcvtdate(\m(time)) \m(name) \m(dir)" ; Show info
}
fclose \%c                                 ; Close name-list file
delete \m(listname)                        ; Delete name-list file
if def \%1 exit 0                          ; BYE to server and exit Kermit
end 0
