#!/bin/sh
# arb_update [-nowait] source [ -host host ] dest1 dest2 ...
# if $1 == -nowait than wait for user
# Updates this arb directory from source $1 to dest $2 $3 $4 ...
# if $2 == '-host' than $3 == dest host

OLD=`pwd`

if test "$1" = "-nowait"; then
	shift
	WAIT=""
else
	WAIT="wait"
fi

SOURCE=`(cd $1;pwd)`
shift

if test "$1" = "-host"; then
	shift
	TARGET="$1"
	shift
	TIMESTAMP="$SOURCE/timestamp.$TARGET"
else
#		uses first $DEST Timestamp
	TARGET=""
	DEST=`(cd $1;pwd)`
	TIMESTAMP="$SOURCE/timestamp"
fi

echo	Timestamp	$TIMESTAMP
echo	Source		$SOURCE
echo	Dest		$DEST
rm -f /tmp/arb_update
rm -f /tmp/arb_names
cd $SOURCE;
	find . lib/. -name DEPOT -prune -o \( \
		-depth -newer $TIMESTAMP \
		! -perm -001 \
		! -name '*.[oa]' ! -name '*%' ! -name '*.so*' \
		! -name '*~' ! -name '*.ARF' \
		! -name '*.he' ! -name '*.cs' ! -name '*.arb.pt' \
	        ! -name '*.gz' ! -name '*tgz' ! -name '*.arb.quick?' \
	        ! -name '*.arb' ! -name '*.otf'\
	        ! -name '*.eps' ! -name '*.tre'\
	        ! -name '*.prb' ! -name '#*#'\
	 	! -name '*.a[0-9][0-9]' ! -name '*.ARM' \
		! -name 'core' ! -name config.makefile \
		! -name 'gde*_*'  ! -name arb_tcp.dat  \
		! -name treefile ! -name outfile ! -name infile \
		! -type d \
		\)	-print >/tmp/arb_names

	find	util SH INCLUDE PERL_SCRIPTS lib/arb_default -depth -newer $TIMESTAMP \
		! -name '*%' ! -name 'core' ! -type d -print  >>/tmp/arb_names

	find	arb_README.txt arb_install.sh arb_src.README load -depth -newer $TIMESTAMP \
		 -print  >>/tmp/arb_names

cd $OLD

if test "X$TARGET" != "X"; then
	(cd $SOURCE;tar cf - `cat /tmp/arb_names` ) >/tmp/arb_update
fi


if test "X$WAIT" = "X"; then
	echo now moving:
	echo `cat /tmp/arb_names`
	echo press cntrl-c to abort, return to continue;
	read a
fi

while test $# -ne 0; do
	if test "X$TARGET" = "X"; then
		DEST=`(cd $1;pwd)`
		echo ---------------- [1] Current Destination $DEST
		cd $SOURCE
		cat /tmp/arb_names | cpio -pvmd $DEST
		cd $OLD
	else
		DEST="$1"
		echo '---------------- [2] Current Destination $DEST (Target=$TARGET)'
		cat /tmp/arb_update |  ssh arb@$TARGET "(cd $DEST;tar xfv -)"
	fi
	shift
done


rm -f /tmp/arb_names
rm -f /tmp/arb_update
touch $TIMESTAMP
