#! /bin/sh
# netinstall
# Usage: netinstall [options] "file1 file2 ..." directory ["host1 host2 ..."]
# Copies the given files to the given directory on the given hosts and
#   the current host, giving them the same names, except that if the source 
#   file name has a .r suffix, it is stripped.  All files have their mode
#   set to 0755, to allow read and execute access to all, write access to
#   owner only.
# The -r (remote) option, if given, prevents the files from being installed
#   on the local host.
# The -d option causes old versions of the files to be deleted before the 
#   new ones are installed.  This is useful if the installer does not have
#   write access to the the old files, but does have write access to the
#   directory.
# The -m option can be used to change the mode of installed files to
#   something other than the default 0755.  The new mode follows the -m,
#   separated from it by a space.
# If no hosts are given, defaults are set in the first line below.  The
#   -x option causes the xhosts list to be used as a default in place of
#   the defaulthosts list.  The -p option selects the publichosts or
#   xpublichosts list.
#
defaulthosts="shasta navajo whitney carmel helens glacier"
publichosts="shasta navajo whitney"
xhosts="navajo"
xpublichosts="navajo"
xflag=""
pflag=""
mode=0755
dolocal=1
delete=""
moreoptions=1

while [ $moreoptions ]
    do
    case $1 in
        -r)
	    dolocal=""
	    shift
	    ;;
	-d)
	    delete=1
	    shift
	    ;;
	-m)
	    mode=$2
	    shift
	    shift
	    ;;
	-x)
	    xflag=1
	    shift
	    ;;
	-p)
	    pflag=1
	    shift
	    ;;
        *)
	    moreoptions=""
	    ;;
        esac
    done

if [ $xflag ]
    then
    if [ $pflag ]
	then
	defaulthosts=$xpublichosts
	else
	defaulthosts=$xhosts
        fi
    else
    if [ $pflag ]
	then
	defaulthosts=$publichosts
	fi
    fi

dests=""
if [ $dolocal ]
    then
    for file in $1
        do
        dest="`basename $file .r`"
        dests="$dests $dest"
        if [ $delete ]
	    then
	    echo rm -f $2/$dest
	    rm -f $2/$dest
	    fi
	echo cp $file $2/$dest
        cp $file $2/$dest
	done
    echo \(cd $2 \; chmod $mode $dests\)
    (cd $2; chmod $mode $dests)
    else
    for file in $1
	do
        dest="`basename $file .r`"
        dests="$dests $dest"
	done	
    fi

hosts=${3-$defaulthosts}
for host in $hosts
    do
    (
        for file in $1
	    do
	    dest="$2/`basename $file .r`"
	    if [ $delete ]
		then
		echo "delete $dest"
		fi
	    echo "store $file $dest"
            done
    ) | ftp $host
    echo remote $host cd $2 \\\; chmod $mode $dests
    remote $host cd $2 \; chmod $mode $dests
    done
