#! /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 by default to 644, to allow write access to the owner, who should
#   also be the installer.
# 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 644.  The new mode follows the -m,
#   separated from it by a space.
# The -M option prevents netinstall from changing file modes at all.
# 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.
# -D causes the actual cp, chmod, etc commands to be skipped, but they
#  are still echoed.  This can be used for debugging.
# If the environment variable INSTALL_LOCAL is set, the files are installed
#   on the local host only.  This overrides any remote hosts specified,
#   either by default, by options, or explicitly on the command line.  In
#   this case the -r option will prevent the files from being installed
#   at all.
#
defaulthosts=""
publichosts="gregorio carmel navajo labrea"
xhosts="gregorio"
xpublichosts="gregorio"
xflag=""
pflag=""
mode=0644
dolocal=1
delete=""
moreoptions=1
debug=0
# The following is a variable so it can easily be changed for debugging
filedate=/etc/V/filedate

while [ $moreoptions ]
    do
    case $1 in
        -r)
	    dolocal=""
	    shift
	    ;;
	-d)
	    delete=1
	    shift
	    ;;
	-m)
	    mode=$2
	    shift
	    shift
	    ;;
	-M)
	    mode=""
	    shift
	    ;;
	-x)
	    xflag=1
	    shift
	    ;;
	-p)
	    pflag=1
	    shift
	    ;;
	-D)
	    debug=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

if [ $INSTALL_LOCAL ]
    then
    defaulthosts=""
    fi

dests=""
times="-b "
for file in $1
    do
    filebasename="`basename $file`"
    atime=
    mtime=
    eval `$filedate -a -m $file`
    if [ $mtime ]
	then
	if [ $atime ]
	    then
	    times="$times -A $atime"
	    fi
	times="$times -M $mtime $filebasename"
	fi
    dests="$dests $filebasename"
    done

# install the remote hosts first to keep rdist from screwing up.
hosts=${3-$defaulthosts}
for host in $hosts
    do
    if [ $delete ]
	then
	echo "rsh $host \"(cd $2 ; rm -f $dests)\" < /dev/null"
	if [ $debug  0 ]
	    then
	    rsh $host "(cd $2 ; rm -f $dests)"  < /dev/null
	    fi
	fi
    echo "rcp $1 $host:$2"
    if [ $debug = 0 ]
	then
	rcp $1 $host:$2
	fi
    if [ $mode ]
	then
	echo "rsh $host \"(cd $2 ; chmod $mode $dests)\" < /dev/null"
	if [ $debug = 0 ]
	    then
	    rsh $host "(cd $2 ; chmod $mode $dests)"  < /dev/null
	    fi
	fi
    echo "rsh $host \"(cd $2 ; $filedate $times)\" < /dev/null"
    if [ $debug = 0 ]
	then
	rsh $host "(cd $2 ; $filedate $times)"  < /dev/null
	fi
    done

if [ $dolocal ]
    then
    if [ $delete ]
	then
	echo "(cd $2 ; rm -f $dests)"
	if [ $debug = 0 ]
	    then
	    (cd $2 ; rm -f $dests)
	    fi
	fi
    echo "cp $1 $2"
    if [ $debug = 0 ]
	then
	cp $1 $2
	fi
    if [ $mode ]
	then
	echo "(cd $2 ; chmod $mode $dests)"
	if [ $debug = 0 ]
	    then
	    (cd $2 ; chmod $mode $dests)
	    fi
	fi
    echo "(cd $2 ; $filedate $times)"
    if [ $debug = 0 ]
	then
	(cd $2 ; $filedate $times)
	fi
    fi

true  #don't return an error code even if the last "remote" failed

