#!/bin/bash -e
from="$1"
to="$2"
if [ -z "$to" -o -n "$3" ]; then
	. usage from to
fi
from=`p "$from"`
to=`p "$to"`
if [ -z "$from" -o -z "$to" ]; then
	. usage from to
fi
if [ -d "$to" ]; then
	to="$to/`basename "$from"`"
fi
if [ -d "$from" ]; then
	mkdir -p "$to"
	( cd "$from" ; find . -type d ) | ( cd "$to" ; xa mkdir -p )
	( cd "$from" ; find . -type f ) | ( cd "$to" ; while read F; do
		cp -v -i -a --sparse=always "$from/$F" "$to/$F" && rm -f "$from/$F"
	done; )
	find "$from" -type d | tac | xa rmdir
else
	cp -v -i -a --sparse=always "$from" "$to" && rm -f "$from"
fi
