#!/bin/sh

set -e

# Initial setup
prg=$(basename $0)
copyright="(C) 2000-2002 Jonas Smedegaard <dr@jones.dk>"
V=''
action=''
hosts=''
rsync_opts='-aHz --delete --force'
ssh_opts=''
localdir=''
remotedir=''

args=''
while [ $# -gt 0 ]; do
	case $1 in
		-h|--help)	echo "$prg, $copyright

usage: $prg [<options>] host [path [path...]]
   or: $prg [<options>] \"host [host...]\" [path [path...]]
and options are the following:
       --download	Make local directory equal to the remote one
       --upload		Make remote directory equal to the local one
       --test		Simulate, don't actually change anything
       --dry-run	(Alias for --test)
       --force		Always copy whole files (disable rsync feature)
       --whole-file	(Alias for --force)
    -v|--verbose	Verbose mode
       --host		Hostname(s) of remote host(s)
    -l|--localdir	Local base directory (only if path specified)
    -r|--remotedir	Remote base directory
    -p|--port		ssh port number
    -b|--batch		Enable BatchMode (for use in automatic scripts)
       --ssh-opts	Arbitrary ssh options
    -h|--help		This help text
    -*			Arbitrary rsync options

One of either --download or --upload is required. Last occurence
supersedes earlier ones.
If no path specified, current working directory is used."
				exit 0
				;;
#		-v|--verbose)		V=1; rsync_opts="$rsync_opts --progress --stats";;
		-v|--verbose)		V=1; rsync_opts="$rsync_opts -v";;
		--download)		action=download;;
		--upload)		action=upload;;
		--test|--dry-run)	rsync_opts="$rsync_opts --dry-run";;
		--force|--whole-file)	rsync_opts="$rsync_opts --whole-file";;
		--host)			hosts="$hosts $2"; shift;;
		-l|--localdir)		localdir="$2"; shift;;
		-r|--remotedir)		remotedir="$2"; shift;;
		-p|--port)		ssh_opts="$ssh_opts -p $2"; shift;;
		-b|--batch)		ssh_opts="$ssh_opts -o BatchMode=yes";;
		--ssh-opts)		ssh_opts="$ssh_opts $2"; shift;;
		-*)			rsync_opts="$rsync_opts $1";;
		*)			args="$args $1";;
	esac
	shift
done
set -- $args

if [ -z $hosts ]; then
	hosts=$1
	shift
fi
if [ -z $hosts ]; then
	echo "$prg error: Hostname missing!"
	echo
	$0 --help
	exit 1
fi

if [ -z "$1" ]; then
	workdir="`pwd`"
	localdir=''
fi

while [ -n "$workdir" ]; do
	for host in $hosts; do

		thisdir="$localdir$workdir"
		thatdir="$remotedir$workdir"
		shellmagic=""

		if [ -z "$localdir" ] && echo "$host" | grep -q '@'; then
			relativeworkdir=$(echo "$workdir" | sed "s|^$HOME/\?||")
			if [ "$workdir" != "$relativeworkdir" ]; then
				thisdir="$workdir"
				thatdir="$relativeworkdir"
				shellmagic="~/"
			fi
		fi

		case $action in
			download)
					[ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:$shellmagic'$thatdir/'" "$thisdir"
					rsync --rsh="ssh $ssh_opts" $rsync_opts "$host:$shellmagic'$thatdir/'" "$thisdir"
					;;
			upload)	
					[ $V ] && echo rsync --rsh="ssh $ssh_opts" $rsync_opts "$thisdir/" "$host:$shellmagic'$thatdir'"
					rsync --rsh="ssh $ssh_opts" $rsync_opts "$thisdir/" "$host:$shellmagic'$thatdir'"
					;;
			*)		echo "$prg error: You need to specify either --upload or --download!"
					echo
					$0 --help
					exit 1
					;;
		esac
	done
	workdir="$1"
	[ -n "$1" ] && shift
done

exit 0