#!/bin/bash
#
# This script "cleans" the UPPER directory tree for a subhost by
# comparing it with the LOWER tree and remove all files that are equal
# to content.

PROGRAMDIR="$(dirname $(realpath $0))"
. $PROGRAMDIR/functions

subhost_name $1
subhost_config



: ${LOWER:-/}

if [ ! -d "$UPPER" ] || [ ! -d "$LOWER" ] ; then
    echo "*** needs a root path" >&2
    exit 1
fi

if is_live $NAME ; then
    echo "** Cannot clean running subhost **" >&2
    exit 1
fi

UPPER="${UPPER%/}"
LOWER="${LOWER%/}"

if [ "$UPPER" = "$LOWER" ] ; then
    echo "** UPER and LOWER are the same directory **" >&2
    exit 1
fi

du -sh $UPPER
exit 0
DIFFS=/tmp/clean-$NAME.$$
rm -f $DIFFS
find $UPPER -type f -printf '%P\n'| while read X ; do
    cmp "$UPPER/$X" "$LOWER/$X" >> $DIFFS 2>&1 && rm "$UPPER/$X"
done 
du -sh $UPPER
echo "(See details in $DIFFS)"
