wsh/
wsh/binsrc/
wsh/docs/help/
wsh/docs/old/
wsh/etc/
wsh/src/util/
#!/bin/sh
# try /bin/sh5 if you get errors
#
# fget/fput/frm/fls:  FTP put, get, rm and ls shell script  (Version 2.4)
#                     For usage with the WizPort or as a unix utility
#                     David Ljung (ljung@cae.wisc.edu)
# If this version doesn't work for you becauser of '[' errors, contact me.
#
# Usage: fls [dir]
#        frm <file>
#        fget|fput <file> <dest>
#                  (Number of args depends on setting of WizPort variable)
#
# Needs environment variables set:  HOMEHOST, HOMEUSER and HOMEDIR
#
# Does:
#   ftp's to $HOMEHOST
#   Logs in as $HOMEUSER - asks for password
#   changes directory to $HOMEDIR
#   does appropriate remote action (get, put, rm, ls)
#
#   INSTALLATION:
# This file should be called 'fget' and should be executable (chmod u+x fget)
# Then link fput, frm and fls to fget (ln -s fget fput; ...)
# Put all this in your bin/ directory
#
# Set the WizPort variable if you are going to use this with the WizPort
# This requires TWO file args for fput and fget, so the local file
# can be checked by the WizPort security.  This also disallows "/",
# and "\" in the local arg and " " in the first arg.
# You also need to add the following to "NON_FILE_ARGS:" in "etc/exceptions"
#       fget/1 fput/2 frm fls
# If you don't want this, you can remove it, but don't add the NON_FILE_ARGS.
# this would assume that they have a mirror of (at least a portion) of the
# filesystem here.  In other words, to fput "users/joe/moo.c" they would
# have to have "users/joe" to put it into at their HOMEHOST
WizPort=1

# What pager to use.  Choose 'less' or 'cat'
PAGER="less"
# options in case you choose less
# (-E means you don't have to do a 'q' at the end of the paged output)
LESS="-E"; export LESS

# Full paths: (find these outside wsh with the 'which' command)
# These are probably right.  If not you will get an error to change these.
GREP=/bin/grep
FTP=/usr/bin/ftp

#
#  That should be all you have to worry about! :)
#

# Make sure we have executables
tmp=`type $PAGER`
if [ "$tmp" = $PAGER" not found" ]; then
  echo "Could not find the pager: $PAGER."
  echo "Please put the proper path in this script (use 'less' or 'cat')"
  exit
fi
if [ ! -x $GREP ]; then
  echo "'grep' is not at $GREP.  Please put the proper path in this script"
  exit
fi
if [ ! -x $FTP ]; then
  echo "'ftp' is not at $FTP.  Please put the proper path in this script"
  exit
fi

# Which is it?  fget, fput, fls or frm?
if (echo $0 | $GREP fls$ > /dev/null); then
  ME="fls"
  DO="ls $1"
elif (echo $0 | $GREP fget$ > /dev/null); then
  ME="fget"
  DO="get"
  LOCAL=$2
elif (echo $0 | $GREP fput$ > /dev/null); then
  ME="fput"
  DO="put"
  LOCAL=$1
elif (echo $0 | $GREP frm$ > /dev/null); then
  if [ $# -ne 1 ]; then
     echo "Usage: frm <file>"
     exit
  fi
  ME="frm"
  DO="delete $1"
else
  echo "This program can only be invoked as fget, fput, fls or frm"
  echo "Make sure the executable is named as at least one of these"
  exit
fi

# little help echo if not a clean exit
trap OnExit 0
OnExit() {
  if [ -z "$CleanExit" ]; then
    echo "'$ME -h' for help"
  fi
}

# If the first arg is "-?" or "?" or "-h" then show help
if [ $# = 1 ] && ([ $1 = "-?" ] || [ $1 = "?" ] || [ $1 = "-h" ]); then
# set up usage text depending on $WizPort
  if [ -z "$WizPort" ]; then
USAGE="\
Usage:  fls [dir]
        frm <remote file>
        fget <remote file>
        fput <remote file>"
  else
USAGE="\
Usage:  fls [dir]
        frm <remote file>
        fget <remote file> <local file>
        fput <local file> <remote file>"
    if [ -z "$WSHLOGIN" ]; then
USAGE="$USAGE

To install for the WizPort:
--------------------------

Put this file in the WizPort bin/ directory as 'fget'
Then make links to it from 'frm' 'fls' and 'fput'
(Ex: ln -s fget frm)
If you don't have links on your machine, just copy 'fget' for each one.

Then add the following to \"NON_FILE_ARGS:\" in \"etc/exceptions\"
fget/1 fput/2 frm fls"
    fi
  fi

  $PAGER <<EOF

  Help for fget, fput, fls and frm
  --------------------------------

These commands are file-transfer commands that work from the command-line.
Each time they are invoked they set up an ftp session.
The following environment variables must be set before using these commands:

HOMEHOST      site to ftp to
HOMEUSER      user to login as
HOMEDIR       directory to get files from (defaults to ".")

An example of how to set these is:
setenv HOMEHOST cae.wisc.edu     (Usually done in the .login file)

Each invocation of fget, fput, fls and frm requires that you enter
your password.  This is because of the difficulties of piping input
to ftp, as well as the fact that this would be very unsecure.

Make sure you don't start typing your password until you see the line:
Password:

It is important to realize that you are trusting the admin of this site
to NOT be logging the passwords - if you don't trust them, you might want
to use a temporary password instead

You can also set HOMEUSER to be your user name followed by your password.
Don't do this unless you trust the people on the machine though!  It isn't
hard to find out what an environment variable is set to for a certain process,
so be careful!  Also, don't set HOMEUSER in your .login if you are going to
put your password in it.  (as an example:  'setenv HOMEUSER ljung mypassword')

$USAGE

EOF
CleanExit=1
  exit
fi

# Make sure we have these environment vars set
if [ -z "$HOMEDIR" ]; then
HOMEDIR="."
fi

if [ -z "$HOMEHOST" ] || [ -z "$HOMEUSER" ]; then
  echo "Must set at least \$HOMEHOST and \$HOMEUSER before running fget"
  exit
fi

# fput and fget are a little more complex with arg checking
if [ $ME = "fget" ] || [ $ME = "fput" ]; then
  if [ -z "$WizPort" ]; then
    if [ $# -ne 1 ]; then
      echo "Usage: $ME <file>"
      exit
    fi
    DO="$DO $1"
    LOCAL=$1
  else
    if [ $# -ne 2 ]; then
      if [ $ME = "fget" ]; then
        echo "Usage: fget <remote file> <local file>"
      else
        echo "Usage: fput <local file> <remote file>"
      fi
      exit
    fi
    DO="$DO $1 $2"
  fi
fi

# check for "\" for ftp to dos in the $LOCAL
# do this only if $WizPort??
# actually, we no longer need to do this at all! :)
#if (echo $LOCAL | $GREP "/" > /dev/null) \
#    || (echo $LOCAL | $GREP "\\\\" > /dev/null); then
#  echo "$LOCAL: Cannot have a '/' or a '\\' in the local file name"
#  exit
#fi

if (echo $1 | $GREP " " > /dev/null); then
  echo "$1: Cannot have spaces in the file names"
  exit
fi

$FTP $HOMEHOST <<EOF | $GREP -v "Name ($HOMEHOST\:" | $PAGER
$HOMEUSER
#Then it will ask for password:
cd $HOMEDIR
$DO
bye
EOF
CleanExit=1