#!/bin/sh
#
# Usage: ./P5 host device
#
usage="Usage: $0 host device
       where host is one of:
           linux-aout solaris sgi
       and device is a non-rewinding tape device such as
           /dev/rmt/0mn (HP/UX 4mm DAT)
           /dev/nrst0 (Sun)
           /dev/rmt/0n (Solaris)"
if [ $# -ne 2 ]; then
	echo "$usage"; exit 1
fi
host="$1"
device="$2"
case "$host" in
	linux*) host=linux-aout ;;
	sgi*) host=sgi ;;
	solaris*) host=solaris ;;
	*) echo "bad host <$host>"
	   echo "$usage"; exit 2;
	;;
esac
images="docs ${host}-install krb5"
case "$device" in
	# both /dev and /devices are legitmate
	/dev*) ;;
	*) echo "bad device <$device> should be in /dev or /devices"
	   echo "$usage"; exit 3;
	;;
esac
if [ ! -w "$device" ]; then
	echo "Tape device <$device> not found"
	echo "$usage"; exit 4;
fi
if [ ! -c "$device" ]; then
	echo "Tape not a character special device <$device>"
	echo "$usage"; exit 5;
fi
vault=/usr/cygnus/release-vault
if [ ! -d ${vault} ]; then
	echo "release vault <$vault> missing"
	echo "$usage"; exit 6;
fi
release=cns5-95q3
if [ ! -d ${vault}/${release} ]; then
	echo "release <$release> missing from vault <$vault>"
	echo "$usage"; exit 7;
fi
tgz=.tar.gz
missing=false
# mustn't quote images here, so it breaks up into words
for i in ${images}; do
	f=${vault}/${release}/${i}${tgz}
	if [ ! -f ${f} ]; then
		echo "Can't find $i ($f)"
		missing=true
	fi
done
if [ $missing = true ]; then
	echo "some files were missing, dump aborted"
	echo "$usage" ; exit 8;
fi
# at this point, we can actually make the tape...
BLOCKSIZE=62k
if mt -f ${device} rew ; then
	echo "rewind succeeded"
	mtflag=-f
else
	echo "trying mt -t..."
	if mt -t ${device} rew ; then
		echo "rewind succeeded"
		mtflag=-t
	else
		echo "rewind failed, aborting dump" ; exit 10
	fi
fi

# mustn't quote images here, so it breaks up into words
for i in ${images}; do
	f=${vault}/${release}/${i}${tgz}
	if dd conv=sync if=${f} of=${device} obs=${BLOCKSIZE}; then
		echo "succeeded writing <$i>"
	else
		echo "failed writing <$i> aborting dump"; exit 11
	fi
done
echo "all images written. testing checksums..."
	if mt $mtflag $device rew ; then
		echo "rewind succeeded"
	else
		echo "rewind for testing failed"; exit 12
	fi
	# mustn't quote images here, so it breaks up into words
	for i in ${images}; do
		echo "trying to read image for ${i}"
		if dd if=${device} ibs=${BLOCKSIZE} | gzip --test ; then
			echo "image ${i} passed gzip integrity check"
		else
			echo "image ${i} failed gzip integrity check"
			echo "aborting dump"; exit 13
		fi
	done
echo "checksum testing passed."
echo "this tape for <$host> is written"

exit 0
