#!/bin/sh
#vim:filetype=sh

if [ "$#" -lt "2" ]
then
	echo "$0 merges Unionfs snapshots onto the base."
	echo "$0 base snap0 [ snap1 ... ]" 1>&2
	exit 1
fi

BASE=$1

shift

WD=`pwd`
if [ ! `echo $BASE | cut -b1` = "/" ] ; then
	BASE="$WD/$BASE"
fi

while [ ! -z "$1" ]
do
	SNAP=$1
	if [ ! -d $SNAP ] ; then
		echo "$0: $SNAP not a directory" 1>&2
		exit 1
	fi

	cd $SNAP || exit 1

	echo "Merging $SNAP onto $BASE."
	
	# Directories
	find . -type d | tail +2 |  sed -e 's/\.\///' |
	while read N
	do
		mkdir -p $BASE/$N
	done
	
	# Copy Files
	find . -not \( -regex '.*/\.wh\.[^/]*' -type f \) -not -type d |  sed -e 's/\.\///' |
	while read N
	do
		cp -a $N $BASE/$N
	done

	# Handle Whiteouts
	find . \( -regex '.*/\.wh\.[^/]*' -type f \) | sed -e 's/\.\///;s/\.wh\.//' |
	while read N
	do
		rm -rf $BASE/$N
	done

	cd $WD
	shift
done

cd $WD

exit 0
