#!/bin/bash

cd /

DEVICE=0,0,0
MNTDEV=/dev/scd0

BKDIR=/tmp/cdbkup-test-suite

prefix=/usr
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
cdbkup=$sbindir/cdbkup
cdrstr=$sbindir/cdrstr
gnutar=/bin/tar

function checksum () {
	md5sum * */* */*/* */*/*/* 2>/dev/null | sort | md5sum
}

function setup_input () {
	rm -rf $BKDIR
	mkdir $BKDIR
	pushd $BKDIR

	mkdir input
	mkdir input/subdir
	mkdir input/s
	mkdir input/s/s
	echo Test File > input/s/s/file1
	perl -e 'print $i while(++$i)' |
		dd bs=1024 count=1024 >input/bigfile 2>/dev/null
	echo Another test file > input/subdir/file2
	echo Another subdir-test file > input/subdir/file-to-delete
	echo Yet another test file > input/file3
	chmod u+x input/file3
	ln -s bigfile input/link-bigfile
	ln -s non-file input/broken-link

	cd input
	PREMOD_CKSUM=`checksum`

	popd
}

function modify_input () {
	pushd $BKDIR

	perl -e 'print $i while(++$i)' | dd bs=1024 count=2048 \
		> "input/subdir/big file with spaces in its name" 2>/dev/null
	touch input/subdir/file2
	rm input/subdir/file-to-delete

	cd input
	POSTMOD_CKSUM=`checksum`

	popd
}

function warn () {
	echo "Please insert a CD-RW into device $MNTDEV.  All of its" 1>&2
	echo "current contents will be erased.  Press Enter to continue." 1>&2
	read
}

function clean_up () {
	rm -rf $BKDIR
}

if [ "$1" ]; then echo "*** Test Phase: $1" 1>&2; fi

case $1 in

'')
	$0 nocd
	$0 exclude
	$0 lengths
	$0 create-cdrw &&   # verify-cdrw depends on create-cdrw succeeding.
	$0 verify-cdrw
	;;

nocd)
	setup_input

	pushd $BKDIR

	$cdbkup 0 -t --label=cdbkup-test-suite $DEVICE $BKDIR/input 2>&1
	result=cdbkup-test-suite-`/bin/date +%Y-%m-%d`-0.tar.gz

	if [ ! -f $result ]; then
		echo Backup File Not Created 1>&2
		exit 1;
	fi

	mkdir output
	cd output
	$gnutar xfz ../$result 2>&1

	if [ `checksum` != $PREMOD_CKSUM ]; then
		echo Incorrect fileset when using tar 1>&2
		exit 1;
	fi

	cd ..
	rm -rf output

	modify_input

	$cdbkup 3 --no-iso --test -l cdbkup-test-suite \
		-S 696320 $DEVICE $BKDIR/input 2>&1

	mkdir output

	(echo;echo 1) | $cdrstr -t output 2>&1

	cd output

	if [ `checksum` != $PREMOD_CKSUM ]; then
		echo Incorrect fileset when using cdrstr 1>&2
		exit 1;
	fi

	cd ..

	(echo;echo 2;echo;echo q) | $cdrstr -t output 2>&1

	cd output
	if [ `checksum` != $POSTMOD_CKSUM ]; then
		echo Final backup has incorrect checksum 1>&2
		exit 1;
	fi

	cd ..
	rm -rf output

	$cdbkup 3 --test -l cdbkup-test-suite \
		-S 14899199 $DEVICE $BKDIR/input 2>&1
	
	mkdir output

	(echo;echo 1) | $cdrstr -t output 2>&1

	mkdir mnt1
	mkdir mnt2
	if ! mount -o loop cdbkup-test-suite-*-3.tar.gz.1.iso mnt1; then
		echo "Can't mount ISO images for testing." 1>&2
		exit 1;
	fi
	mount -o loop cdbkup-test-suite-*-3.tar.gz.2.iso mnt2

	cd output

	cat ../mnt1/* ../mnt2/* | $gnutar -f - -xz --incremental 2>&1

	umount ../mnt1
	umount ../mnt2

	if [ `checksum` != $POSTMOD_CKSUM ]; then
		echo Incorrect fileset based on ISO images 1>&2
		exit 1;
	fi

	popd

	clean_up

	;;

exclude)
	setup_input

	pushd $BKDIR

	$cdbkup 0 -t -e $BKDIR/input/subdir --exclude=$BKDIR/input/file3 \
		--label=cdbkup-test-suite $DEVICE $BKDIR/input 2>&1

	cd input
	rm -rf subdir file3
	EXCLUDE_CKSUM=`checksum`
	cd ..

	mkdir output

	(echo;echo 1) | $cdrstr -t output 2>&1

	if [ -e output/subdir ]; then
		echo Exclusion of subdirectory failed. 1>&2
		exit 1;
	fi

	if [ -e output/file3 ]; then
		echo Exclusion of file failed. 1>&2
		exit 1;
	fi

	cd output
	if [ `checksum` != $EXCLUDE_CKSUM ]; then
		echo Exclusion broke something else. 1>&2
		exit 1;
	fi

	popd

	clean_up

	;;

create-cdrw)
	warn

	setup_input

	pushd $BKDIR

	yes '' | $cdbkup 0 -c bz2 -b --label=cdbkup-test-suite \
		$DEVICE $BKDIR/input 2>&1

	if [ -x /usr/bin/eject ]; then eject $MNTDEV; fi

	modify_input

	yes '' | $cdbkup 3 -l cdbkup-test-suite $DEVICE $BKDIR/input 2>&1

	if [ -x /usr/bin/eject ]; then eject $MNTDEV; fi

	popd

	;;

verify-cdrw)

	# Make sure we know the proper checksums
	if [ "$POSTMOD_CKSUM" == '' ]; then
		setup_input
		modify_input
	fi

	pushd $BKDIR

	mkdir output
	(echo;echo 1) | cdrstr -c bz2 -d $MNTDEV output 2>&1

	cd output
	if [ `checksum` != $PREMOD_CKSUM ]; then
		echo Burn of level 0 to CD-ROM produced incorrect checksum 1>&2
		exit 1;
	fi
	cd ..

	(echo;echo 2) | cdrstr -c gz -d $MNTDEV output 2>&1

	cd output
	if [ `checksum` != $POSTMOD_CKSUM ]; then
		echo Burn of level 3 to CD-ROM produced incorrect checksum 1>&2
		exit 1;
	fi
	cd ..

	popd

	clean_up

	;;

lengths)

	setup_input

	pushd $BKDIR

	filesize=152007

	$cdbkup 0 -c none -t -S $filesize --no-iso --label=cdbkup-test-suite \
		$DEVICE $BKDIR/input 2>&1

	if [ `wc -c < *.1` != $filesize ]; then
		echo Incorrect file size 1>&2
		exit 1;
	fi

	mkdir output
	cd output
	cat ../cdbkup-test-suite-* | $gnutar xf -

	if [ `checksum` != $PREMOD_CKSUM ]; then
		echo -t,-S,-I produced incorrect checksum 1>&2
		exit 1;
	fi
	cd ..

	rm -rf output

	$cdbkup 0 -c none -t -S 14815233 --label=cdbkup-test-suite $DEVICE $BKDIR/input 2>&1

	mkdir mnt
	if ! mount -o loop cdbkup-test-suite-*.tar.1.iso mnt; then
		echo "Can't mount ISO images for testing." 1>&2
		exit 1;
	fi

	if [ `wc -c < mnt/*` != 614400 ]; then
		umount mnt
		echo Incorrect file size 1>&2
		exit 1;
	fi

	umount mnt

	clean_up

	;;

esac

if [ "$1" ]; then echo "*** Success!" 1>&2; fi
