#!/bin/sh
#
# Wrapperskript to ftp to simply ftp files from/to Muds.

# --- Usage information ---
usage() {
  cat <<+++USAGE+++
Usage: ${myname} [-r] [-m <mudname>] <file1> [ <file2> ...]

Transfer a file to a mud, or if '-r' is given, retrieve it.
If <mudname> is given, the specified mud is accessed, else the default
mud as specified in ~/.mftprc.

Examples:
  ${myname} README
    Transfer the local file 'README' to '/players/<yourname>/README'.
  ${myname} obj/t.c
    Transfer the local file 'obj/t.c' to '/players/<yourname>/obj/t.c'.
  ${myname} /obj/living.c
    Transfer the local file 'living.c' to '/obj/living.c'.
  ${myname} -r -mTubMud /obj/living.c
    Retrieve '/obj/living.c' from TubMud to the local file 'living.c'.

The ~/.mftprc file specifies address, port, wizardname and password for
the various muds. Each line specifies one mud and has the format

  game <mudname> host <address> port <portno> wizard <name> password <passwd>

The entries for port and password are optional.
The default mud to be used by ${myname} is specified in the same file by a line

  default <mudname>

This is in fact the same format mtp uses.
+++USAGE+++
}

# --- ---
tmp=/tmp/nftp$$
trap "rm -f $tmp*" 0
trap "exit 1" 1 2 15

myname=`basename $0`

# --- Parse arguments ---
if [ $# -lt 1 ]
then
  usage
  exit 0
fi

set -- `getopt rm: $*`
if [ $? != 0 ]
then
  usage
  exit 2
fi

retrieve=0
mudname=

for i in $*
do
  case $i in
  -r) retrieve=1; shift;;
  -m) mudname=$2; shift 2;;
  --) shift; break;;
  esac
done

# --- Get data for: mudhost, mudport, wizard and wizpasswd ---

if [ ! -f $HOME/.mftprc ]
then
  echo "$myname: File ~/.mftprc missing."
  exit 1
fi

if [ -z "$mudname" ]
then
  mudname=`grep -i 'default' $HOME/.mftprc | head -1 | awk '{ print $2 }'`
fi

if [ -z "$mudname" ]
then
  echo "${myname}: No (default) mud specified."
  exit 1
fi

mtpdata=`grep "game $mudname" $HOME/.mftprc | head -1`

if [ -z "$mtpdata" ]
then
  echo "${myname}: No entry for $mudname in ~/.mftprc"
  exit 1
fi

mudhost=`echo $mtpdata | awk '{ for (i=1; i<=NF; i++) if ($i == "host") { i++; print $i; break; } }' `
mudport=`echo $mtpdata | awk '{ for (i=1; i<=NF; i++) if ($i == "port") { i++; print $i; break; } }' `
wizard=`echo $mtpdata | awk '{ for (i=1; i<=NF; i++) if ($i == "wizard") { i++; print $i; break; } }' `
wizpasswd=`echo $mtpdata | awk '{ for (i=1; i<=NF; i++) if ($i == "password") { i++; print $i; break; } }' `

if [ -z "$mudhost" ]
then
  echo "${myname}: No host specified for $mudname"
  exit 1
fi

if [ -z "$wizard" ]
then
  echo "${myname}: No wizardname specified for $mudname"
  exit 1
fi

if [ -z "$wizpasswd" ]
then
  wizpasswd=$wizard@`hostname`
  exit 1
fi

# Construct the basic ftp command file
echo "user $wizard $wizpasswd" > $tmp.ftpinit
echo "ascii" >> $tmp.ftpinit

# Now loop over the filenames
while [ $# -ge 1 ]
do
  absolute=`echo "$1" | awk -F/ '{print $1}'`
  if [ -z "$absolute" ]
  then
    relname=`echo $1 | sed -e 's+^/++'`
  else
    relname=$1
  fi

  # Local filename
  if [ -z "$absolute" ]
  then
    localname=`basename $1`
  else
    localname=$relname
  fi
  
  # Remote name
  mudname=$1

  if [ $retrieve -ne 0 ]
  then
    echo "Retrieving $mudname as $localname."
  else
    echo "Uploading $localname to $mudname."
  fi

  # Construct the ftp command file
  cp $tmp.ftpinit $tmp.ftpcmd
  if [ $retrieve -ne 0 ]
  then
    echo "get $mudname $localname" >> $tmp.ftpcmd
  else
    echo "put $localname $mudname" >> $tmp.ftpcmd
  fi
  echo "quit" >> $tmp.ftpcmd

  # Do it.
  ftp -n -v $mudhost $mudport < $tmp.ftpcmd

  shift
done

exit 0
