#!/bin/sh -

rsh="${RSHCMD-rsh}"
rcp="${RCPCMD-rcp}"

#dflthost=`hosts ppp`
dflthost=s6lite
#dflthost=aludel
#dflthost=akinzler
files=	#'/usr/local/etc/dialins /usr/local/etc/dialins'

# dailyp - routine pushed hosts home directory update and check
# Steve Kinzler, steve@kinzler.com, Oct 96/Jun 08
# https://kinzler.com/me/home.html#homedir

quiet=; bad=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-q)	quiet=-e;;

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

case "$#,$bad" in
*,t)	cat << EOF 1>&2
usage: $0 [ -q ] [ host ... ]
	-q	quiet mode, don't print routine reporting information
Default host: $dflthost
EOF
	exit 1;;
0,*)	set x $dflthost; shift;;
esac

safe=
(safe -d 3 /bin/true) 2> /dev/null && safe='safe -q'

for host
do
	case "`$safe $rsh -n -q -x "$host" echo GreenIdeas`,$quiet" in
	GreenIdeas,?*)	;;
	GreenIdeas,*)	echo "updating host $host ...";;
#	*,?*)		continue;;
	*)		echo "$0: skipping $host (cannot $rsh)" 1>&2
			continue;;
	esac

	case "$host" in
	# NOTE: 2024-12-30 termux-exec not working for "/usr/bin/env foo"
	#   see https://github.com/termux/termux-exec/pull/24
	#   not sure I'd prefer it anyway
	# NOTE: 2025-01-04 can ignore warnings of "Unknown user: kinzler."
	#   and "No /usr/bin/perl file"
	s6lite)	PREFIX=/data/data/com.termux/files
		xtra='perl -pi -e "s/~kinzler/~/" .cshrc .vimrc etc/tags'
		xtra="$xtra"'; perl -pi -e "s,^#\041,#\041'"$PREFIX",
		xtra="$xtra"' if m,^#\041/usr/bin/perl," binp/*';;
	esac

	case `hosts -l "$host"` in
	?*)	hosts -l "$host" | rdisth - 2>&1 | rdistsumm -g $quiet;;
	*)	echo "$0: rdisth failure, no hosts info for $host" 1>&2;;
	esac
	rm -f core.[0-9][0-9]* 2> /dev/null
	seth='setenv HOST'; dh='dailyh -d -u -g -l'
	$safe $rsh -n -q -x "$host" csh -c \
  \'"$seth"' "'"$host"'"; '"$dh"'; dailyl |& sed "s/^/'"$host"': /"; '"$xtra"\'

	echo "$files" |
	while read file pfile
	do
		case "$file" in
		?*)	$safe $rcp "$file" "$host:$pfile";;
		esac
	done

	case "$host" in
	aludel)	(cd; cd .remmina && make mirrorp);;
	esac
done
