#!/usr/local/bin/kermit +
#
# i k s g e t  --  Get files from an Internet Kermit Service
#
# Helper for IKSD URLs.
#
# If necessary, change the top line to show the full pathname of C-Kermit 7.0.
# Put this file in your PATH and give it execute permission (chmod +x).
#
# Usage:
#
#   iksget iksd://hostname/path/filespec
#
# (The "iksd:" and "//" are optional.)
#
# Separates the argument into access (IKSD://, optional), host, and path.
# Logs into the IKSD (if any) at the given host as "anonymous" and gets the
# given file(s).  Metacharacters in filespec must be quoted to prevent shell
# expansion.  Example:
#
#   iksget "iksd://kermit.columbia.edu/kermit/f/ck*.txt"
#
# Illustrates:
#  . Anonymous access to IKSD
#  . Retrieving command-line arguments
#  . Use of \fsplit() function for parsing strings
#  . Compact substring notation
#
# Requires: C-Kermit 7.0
#
# Author: F. da Cruz, Columbia University, 5 March 2000
#
local \&a[] \%n host path rc            ; Local variables.
set case off                            ; Caseless string comparisons.

if ( != 2 \v(argc) ) {                  ; Exactly one argument required.
    exit 1 {Usage: \%0 <iksd-url>}
}
.\%n := \fsplit(\&_[1],&a,:)            ; Split access://host/path.

switch \%n {                            ; Verify result.
  :0, exit 1 {Error: empty argument}    ; Nothing found.
  :1, .host := \&a[1]                   ; IKSD: omitted - assume host/path.
      break
  :2, if ( not eq "\&a[1]" "iksd" ) {   ; Access: included.
          exit 1 {Error: non-IKSD URL}  ; Make sure it's "IKSD:".
      }
      .host := \&a[2]                   ; Get host/path.
      break
  :default,                             ; Bad URL.
      exit 1 {Error: too many :'s.}
}
while ( eq "\s(host[1:2])" "//" ) {     ; Strip any leading "//".
    .host := \s(host[3])
}
.\%n = \findex(/,\m(host))              ; Find host/path separator.
if ( < \%n 1 ) {                        ; Fail if none found.
    exit 1 {Error: No filename given}
}
.path := \s(host[\%n+1])                ; This is the path.
.host := \s(host[1:\%n-1])              ; This is the hostname.

set host \m(host):1649 /telnet          ; Connect to IKSD on the host.
if fail {
    exit 1 {Can't reach \m(host) - \v(errstring)}
}
set exit warning off                    ; Disable "OK to quit?" prompt.

remote login anonymous \v(user)@\v(host)  ; Log in as user "anonymous".
if fail exit 1 {Anonymous login rejected} ; Check for failure.

set transfer display brief              ; Use BRIEF file-transfer display.
set transfer bell off                   ; Don't ring the bell.
get \m(path)                            ; GET requested file(s).
.rc := \v(status)                       ; Retain status.
bye                                     ; Sign off from server.
exit \m(rc)                             ; Exit with GET return code.
