#!/usr/bin/env expect
#!/bin/env expect
# $Id: ftpget,v 1.4 1998/04/24 20:22:56 balay Exp $ 
#
#  Retrieves a single file from an anonymous ftp site
# 
#  Calling sequence: 
#      ftp://hostname/directoryname/file localfilename ioflag
#
#  Convert ftp://hostname/directoryname/file
#  to hostname and directoryname/file
#
  log_user [lindex $argv 2]

#
#
  set ftppath [lindex $argv 0]
#
# Could not simply scan out the hostname and file so use this 
# hack instead
#
  scan $ftppath "ftp://%s" ftppath
  set x [string first "/" $ftppath] 
  set host [string range $ftppath 0 [expr $x -1]]
  set y [string length $ftppath]
  set file_in [string range $ftppath [expr $x +1] $y]

  set file_out [lindex $argv 1]

     spawn ftp
     expect {ftp>*}

     # ftp can take a while to open a connection initially
     set timeout 90
     send "open $host \r"
     expect {*Name*}
     send "anonymous\r"
     expect {*Password:}
     send "username@mcs.anl.gov\r"
     expect {*ftp>*}
     # may be moving a large file 
     set timeout 2000
     send "binary \r"
     expect {*ftp>*}
     send "get $file_in $file_out\r"
     expect {*ftp>*}
     send "quit\r"

  set errcode [catch { exec chmod u+x $file_out}]

  exit $errcode