#!/bin/bash

export LC_ALL=C

while true; do
	if test -z "$1"; then
		break
	fi
	if [ "${1:0:1}" = "-" ]; then
		break
	fi
	if test -z "$src"; then
		src=$1
	elif test -z "$dst"; then
		dst=$1
	else
		echo "Ambiguous destination."
		exit
	fi
	shift
done

function merge () {
exec 3<$1 4<$2
ifs=$IFS
IFS='\'
on=""
pat="(.*)\.(.*)"
while read -r -u 4 nn ns nt; do
        while [[ "$nn" > "$on" ]]; do
                if read -r -u 3 on os ot; then
                        continue
                else
			echo ${nn/% /}
			while read -r -u 4 nn ns nt; do
				echo ${nn/% /}
			done
			IFS=$ifs
			return
                fi
        done
	if [ "$nn" = "$on" ]; then
		if [ "$nn" != " " ]; then
			if [[ "$nt" =~ $pat ]]; then
				nti=${BASH_REMATCH[1]}
				ntf=${BASH_REMATCH[2]}
			else
				nti=$nt
				ntf=0
			fi
			if [[ "$ot" =~ $pat ]]; then
				oti=${BASH_REMATCH[1]}
				otf=${BASH_REMATCH[2]}
			else
				oti=$ot
				otf=0
			fi
			if [ $ns -ne $os -o $nti -gt $oti -o $nti -eq $oti -a $ntf -gt $otf ]; then
				echo ${nn/% /}
			fi
		fi
	else
		echo ${nn/% /}
	fi
done
IFS=$ifs
}

pat="(.*):(.*)"
if [[ "$src" =~ $pat ]]; then
	srcuh=${BASH_REMATCH[1]}
	srcfile=${BASH_REMATCH[2]}
	pat="(.*)@(.*)"
	if [[ "$srcuh" =~ $pat ]]; then
		srcuser=${BASH_REMATCH[1]}
		srchost=${BASH_REMATCH[2]}
	else
		srcuser=""
		srchost=$srcuh
	fi
else
	srcfile=$src
	srcuser=""
	srchost=""
fi
if test -z "$srcfile"; then
	echo "No source file !?"
	exit
fi

pat="(.*)/(.*)"
if [[ "$srcfile" =~ $pat ]]; then
	srcfiled=${BASH_REMATCH[1]}
	srcfilep=${BASH_REMATCH[2]}
	srcfiled="$srcfiled/"
else
	srcfiled="."
	srcfilep="$srcfile"
fi
if [ -z "$srcfilep" ]; then
	srcfilep="."
	pp=P
else
	pp=p
fi

pat="(.*):(.*)"
if [[ "$dst" =~ $pat ]]; then
	dstuh=${BASH_REMATCH[1]}
	dstfile=${BASH_REMATCH[2]}
	pat="(.*)@(.*)"
	if [[ "$dstuh" =~ $pat ]]; then
		dstuser=${BASH_REMATCH[1]}
		dsthost=${BASH_REMATCH[2]}
	else
		dstuser=""
		dsthost=$dstuh
	fi
else
	dstfile=$dst
	dstuser=""
	dsthost=""
fi

if test -z "$dstfile"; then
        dstfile="."
fi

cdate=`date +%s`
nfile="/tmp/$cdate.n"
ofile="/tmp/$cdate.o"
lfile="/tmp/$cdate.l"
ffile="/tmp/$cdate"

function die () {
  rm -f $ofile $nfile $lfile $ffile
  exit $1
}

if test -z "$dsthost" -a -z "$dstuser"; then
	cd "$dstfile" || die $? && find "$srcfilep" -printf "%$pp \\\%s\\\%T@\n" 2>/dev/null | sort >$ofile && cd - >/dev/null
else
	if test -z "$dstuser"; then
		bbftp $@ -p 1 -e "pipe cd \"$dstfile\" && find \"$srcfilep\" -printf \"%$pp \\\\\\%s\\\\\\%T@\n\"" $dsthost 2>/dev/null | sort >$ofile
	else
		bbftp $@ -p 1 -e "pipe cd \"$dstfile\" && find \"$srcfilep\" -printf \"%$pp \\\\\\%s\\\\\\%T@\n\"" -u $dstuser $dsthost 2>/dev/null | sort >$ofile
	fi
fi

if test -z "$srchost" -a -z "$srcuser"; then
	cd "$srcfiled" || die $? && find "$srcfilep" -printf "%$pp \\\%s\\\%T@\n" | sort >$nfile && cd - >/dev/null
else
	if test -z "$srcuser"; then
		bbftp $@ -p 1 -e "pipe cd \"$srcfiled\" && find \"$srcfilep\" -printf \"%$pp \\\\\\%s\\\\\\%T@\n\"" $srchost 2>/dev/null | sort >$nfile
	else
		bbftp $@ -p 1 -e "pipe cd \"$srcfiled\" && find \"$srcfilep\" -printf \"%$pp \\\\\\%s\\\\\\%T@\n\"" -u $srcuser $srchost 2>/dev/null | sort >$nfile
	fi
fi

merge $ofile $nfile >$lfile
if test -z "$srchost"; then
	mv -f $lfile $ffile
	chmod +r $ffile
else
	if test -z "$srcuser"; then
		cat $lfile | bbftp $@ -p 1 -e "pipe cat >$ffile" $srchost 2>/dev/null
	else
		cat $lfile | bbftp $@ -p 1 -e "pipe cat >$ffile" -u $srcuser $srchost 2>/dev/null
	fi
fi

if test -z "$srchost" -a -z "$srcuser"; then
	if test -z "$dsthost" -a -z "$dstuser"; then
		tar --totals --no-recursion -C "$srcfiled" --files-from=$ffile -c | tar --totals -v -x -C "$dstfile"
	else
		if test -z "$dstuser"; then
			tar --totals -v --no-recursion -C "$srcfiled" --files-from=$ffile -c | bbftp $@ -e "pipe tar --totals -C \"$dstfile\" -x" $dsthost
		else
			tar --totals -v --no-recursion -C "$srcfiled" --files-from=$ffile -c | bbftp $@ -e "pipe tar --totals -C \"$dstfile\" -x" -u $dstuser $dsthost

		fi
	fi
else
	if test -z "$dsthost" -a -z "$dstuser"; then
		if test -z "$srcuser"; then
			bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" $srchost | tar --totals -v -x -C "$dstfile"
		else
			bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" -u $srcuser $srchost | tar --totals -v -x -C "$dstfile"
		fi
	else
		if test -z "$dstuser"; then
			if test -z "$srcuser"; then
				bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" $srchost | (sleep 10; bbftp $@ -e "pipe tar --totals -x -C \"$dstfile\"" $dsthost)
			else
				bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" -u $srcuser $srchost | (sleep 10; bbftp $@ -e "pipe tar --totals -x -C \"$dstfile\"" $dsthost)
			fi
		else
			if test -z "$srcuser"; then
				bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" $srchost | (sleep 10; bbftp $@ -e "pipe tar --totals -x -C \"$dstfile\"" -u $dstuser $dsthost)
			else
				bbftp $@ -e "pipe tar --totals --no-recursion -C \"$srcfiled\" --files-from=$ffile -c" -u $srcuser $srchost | (sleep 10; bbftp $@ -e "pipe tar --totals -x -C \"$dstfile\"" -u $dstuser $dsthost)
			fi
		fi
	fi
fi

die $?
