#!/bin/sh -

delayh=300		# timeout in seconds for dailyh
lndelay=1800		# timeout in seconds for dailyh -l
delayl=300		# timeout in seconds for dailyl

# Don't attempt to run dailyl on these hosts, because, for example, rsh
# to them is unreliable.
#except=gh.luddy.indiana.edu	# space separated list

hostplugs="${TWMRC-$HOME/.twmrc} ${CTWMRC-$HOME/.ctwmrc}"

# daily - routine main home directory update and distribution
# Steve Kinzler, steve@kinzler.com, Nov 93
# https://kinzler.com/me/home.html#homedir

case $# in
0)	bad=t;;
*)	bad=;;
esac

std=; web=; dist=; home=; link=; local=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-s)	std=$1;;
		-w)	web=$1;;
		-c)	dist=$1;;
		-d)	home=$1;;
		-l)	link=$1;;
		-r)	local=$1;;
		-a)	std=-s; web=-w; dist=-c; home=-d; local=-r;;
		-A)	std=-s; web=-w; dist=-c; home=-d; link=-l; local=-r;;

		-h)	bad=t; break;;
		-*)	bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)	bad=t; break;;
		esac
		shift;;
	esac
done

case "$#$bad" in
0)	;;
*)	cat << EOF 1>&2
usage: $0
       [ [ -s ] [ -w ] [ -c ] [ -d ] [ -l ] [ -r ] | -a | -A ]
	-s	run standard checks and updates (mkshosts, setenvls)
	-w	run web updates (mkhome)
	-c	run rdisth
	-d	run dailyh on all homedirs
	-l	run mkhomes and pass -l (mkl) argument to dailyh
	-r	run dailyl on all hosts
	-a	-s, -w, -c, -d and -r
	-A	-s, -w, -c, -d, -l and -r
EOF
	exit 1;;
esac

cd || exit $?

host=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
case "$host" in
'')	host=`uname -n`;;
esac
case "$host" in
'')	echo "$0: cannot determine hostname"			1>&2; exit 2;;
esac
host=`echo "$host" | sed 's/\..*//'`

set x `hosts main | sed 's/\..*//'`; shift
case $# in
0)	echo "$0: main host is not defined"			1>&2; exit 3;;
1)	;;
*)	echo "$0: multiple main hosts defined ($*)"		1>&2; exit 4;;
esac

case "$host" in
"$1")	;;
*)	echo "$0: this host ($host) is not the main host ($1)"	1>&2; exit 5;;
esac

case "$std" in
?*)	
#	ckaccts

	mkshosts &
#	mkxauthority -k -i &	# now run elsewhere
	(for f in $hostplugs; do plug "$f"; done;	: makehostmenus) &
#	lesskey -o etc/lesskey etc/lesskey.txt &
#	mkwhatis &		# not worth supporting any longer
	(TERM=color_xterm setenvls -c) | grep -v LS_OPTIONS > etc/colorrc.csh &
	wait;;
esac

safe=
test -n "$web" -o -n "$local" &&
	(safe -d 3 /bin/true) 2> /dev/null && safe='safe -q'

case "$web" in
?*) #	mkbookmarks &
#	mkhome &
##	mkhits &
#	wait;;
#	$safe ${RCPCMD-rcp} ${VH}:.mozilla/bookmarks.html ${HYHOST}:.hyplan
	$safe ${RSHCMD-rsh} "$HYHOST" -n cd .hyplan\;  bin/mkhome\; \
						       exec make -s mirror
#					 chmod 644 bookmarks.html\; \
#	mkrefs;;
##	mkmonthstats;;
esac

case "$link" in
?*)	mkhomes; delayh="$lndelay";;
esac

case "$dist" in
?*)	rdisth 2>&1 | rdistsumm -g -e;;
esac

case "$home" in		# assuming main in home set here
?*)	hosts main |
	    eachhost -V -n -d $delayh exec dailyh -d -u -g $link -p -s -t -o
	hosts home | grep -v "^`hosts main | sed 's/\./\\\\./g'`$" |
	    eachhost -V -n -d $delayh exec dailyh -d -u -g $link;;
esac

case "$local" in
?*)	case "$except" in
	?*)	hostargs="-v $except";;
	*)	hostargs=;;
	esac
	inode=`$safe ${RSHCMD-rsh} "$MH" -n ls -i '$MAIL' 2> /dev/null |
	       awk '{ print $1 }'`
	hosts -l home nfs | hosts - $hostargs |
		eachhost -V -n -d $delayl exec dailyl $inode;;
#		sh -x $HOME/binp/eachhost -V -n -d $delayl exec dailyl $inode;;
esac
