#!/bin/sh -
#
#  $Id: scpvia,v 1.2 2002/07/06 23:38:18 dgregor Exp $
#
# Use scp to copy a file between two hosts that are only accessable
# through an intermediary.

usage="usage: scpvia <src> <via> <dst>"

basename="`basename $0`"
die() {
	echo "${basename}: $*" >&2
	exit 1
}


if [ $# -ne 3 ]; then
	die $usage
fi

src="$1"; shift
via="$1"; shift
dst="$1"; shift

tmpdir="/tmp/scpvia.$HOSTNAME.$$"

# create a temporary directory on $via
ssh $via mkdir -m 700 $tmpdir || \
	die "could not make $tmpdir on $via"

trap "ssh $via rm -rf $tmpdir" 0

if echo "$src" | grep ":" > /dev/null; then
	# copy the files from $src to $via:$tmpdir
	ssh -o forwardagent\ yes $via scp $src $tmpdir/.
	
	# copy the files from $via:$tmpdir to $dst
	scp $via:$tmpdir/* $dst
elif echo "$dst" | grep ":" > /dev/null; then
	# copy the files from $src to $via:$tmpdir
	scp $src $via:$tmpdir/.

	# copy the files from $via:$tmpdir to $dst
	ssh -o forwardagent\ yes $via scp $tmpdir/* $dst
else
	# neither the src nor dst are remote -- just copy
	scp $src $dst
fi
