#! /bin/sh

goal="test existing archive with plain compressed files"
bru=${BRU:-bru}
compress=${COMPRESS:-compress}

# Parse our args.  Options include:
#
#	-s	Print our one line test goal and quit
#

while true
do
	case $1 in
		-d)	dflag="on"
			shift
			;;
		-g)	echo $goal
			exit 0
			;;
		*bru*)	bru=$1
			shift
			;;
		*)	break
			;;
	esac
done

if [ "$dflag" != "on" ]
then
	trap "rm -rf emptyfile smallfile biggerfile junk.bru zfiles.bru ; exit 1" 1 2 13 15
fi

# First clean up any stuff that may be left from a previous test.

rm -rf emptyfile smallfile biggerfile junk.bru zfiles.bru

# Begin running tests

echo "Do not be disturbed by the strange permissions, uid, gid, and"
echo "dates on the archived files.  They have deliberately been manipulated"
echo "to test ranges.  For example, emptyfile has been touched to be"
echo "Jan 1, 1970 GMT and smallfile has been touched to be Jan 1, 2000 GMT."
echo "The permissions test all combinations {---, --x, -w-, ..., rwx}."
echo ""

# Note that we have to ensure that TZ is GMT for the duration, to get
# the right dates.

TZ=GMT ; export TZ

echo "Decompressing the reference archive..."
echo ""

$compress -d <zfiles.bru.Z >zfiles.bru

echo "Check that a listing of the contents is correct."

echo ""
$bru -tvf zfiles.bru
echo ""

echo "Check that a listing of the contents with -Z is correct."

echo ""
$bru -tvZf zfiles.bru
echo ""

echo "Check that we can extract the files without problems."
echo ""
$bru -xvf zfiles.bru
echo ""

echo "Check that we can rearchive them again."

echo ""
$bru -cvZf junk.bru emptyfile smallfile biggerfile
echo ""

echo "Check that the dates, permissions, etc are still ok."

echo ""
$bru -tvf junk.bru
echo ""

echo "Check that the dates, permissions, etc are still ok with -Z."

echo ""
$bru -tvZf junk.bru
echo ""

# Now do any cleanup required

if [ "$dflag" != "on" ]
then
	rm -rf emptyfile smallfile biggerfile junk.bru zfiles.bru
fi

exit 0
