#!/bin/sh -

# mirrorif - mirror current directory to where depending on current host
# Steve Kinzler, steve@kinzler.com, May 14
# https://kinzler.com/me/home.html#unix

bn=`/bin/pwd`; bn=`basename "$bn"`

mirror=mirror
case "$1" in
-h)	cat << EOF 1>&2
usage: $0 [ -V ] [ host_pattern dest ] ... [ dest ]
	-V	mirror with verbose comments
Mirrors the current directory to the dest with the host_pattern
matching the current host, else the final dest, if any.
Any \$MIRRIFARGS are passed to the mirror command.  Any dest value "-"
is ignored.  Any trailing "@@" in a dest is replaced with the basename
of the current directory.
EOF
	exit 1;;
-V)	set -x; mirror=mirrorv; shift;;
esac

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

while :
do
	case $# in
	0)	exit;;
	1)	test "$1" = '-' && exit
		dest=`echo "$1" | sed "s/@@$/$bn/"`
		exec csh -c "$mirror $MIRRIFARGS . '$dest'";;
	*)	case "$host" in
		$1)	test "$2" = '-' && exit
			dest=`echo "$2" | sed "s/@@$/$bn/"`
			exec csh -c "$mirror $MIRRIFARGS . '$dest'";;
		esac;;
	esac
	shift; shift
done
