#!/bin/sh

# Handy script to send a Starlab-format tar file (in the current
# directory) to the specified site, optionally documenting the
# transfer with hash output.  The file is sent to the starlab/incoming
# anonymous ftp directory on the target site, which must be
# properly listed locally in ~/.netrc via a line like:
#
#	default login ftp password starlab@somewhere

find_file=0
tarfile=' '
hash=' '
target_dir=pub/starlab/incoming

site="$1"

case "$#-$2" in
  0-*) echo 'Usage:  send_tar  target-host  tarfile  [hash]' >&2
    exit 1
    ;;
  1-*)
    find_file=1
    shift
    ;;
  2-hash|2-h)
    find_file=1
    hash=hash
    ;;
  2-*)
    tarfile="$2"
    shift; shift
    ;;
  *)
    tarfile=$2
    hash=hash
    shift; shift
    ;;
esac

if [ $find_file = 1 ]; then
    set -- *s_tar*    	; # Note: filename format is assumed and ls not used

    if [ $# = 0 ]; then
	echo $0: No tar files found >&2
	exit 1
    fi
    while [ $# -gt 1 ]; do shift; done
    tarfile="$1"
fi

if [ -z "$tarfile" ]; then
    exit 1
fi

case "$site" in
  komaba|tokyo) site=alexandria.c.u-tokyo.ac.jp ;;
esac

echo Sending tar file $tarfile to $site

# Special check for IAS (different FTP structure):

case "$site" in
  ftp.sns.ias.edu) target_dir=incoming/starlab ;;
esac

ftp $site <<EOF
    cd $target_dir
    bin
    $hash
    put $tarfile
    quit
EOF
