#!/bin/sh
#
#

DEVICE=${DEVICE:-"/dev/rmt/0m"}
IMAGE=${IMAGE:-"SX00156-A07"}
TMP=${TMP:-"/tmp"}

if test $# -eq 1
then
	IMAGE=$1
else
	if test $# -ne 0
	then	echo "Usage: $0 [IMAGE]"
		exit 1
	fi
fi

echo ""
echo ""
echo "                  5022 HP-UX Driver Tape Build script"
echo "                  -----------------------------------"
echo "The device node $DEVICE must exist on this system and a tape must"
echo "be inserted in the tape drive."
echo ""
echo "This script was designed to run in the same directory as the image to be"
echo "written to tape.  Verify that that the file <$IMAGE> is currently in"
echo "this directory."
echo ""
echo "NOTE: You may change the locations of any of the following by setting"
echo "      a new value in your environment before invoking this script"
echo "              DEVICE=$DEVICE"
echo "              IMAGE=$IMAGE"
echo "              TMP=$TMP"
echo "      For example, to change the device to /dev/rmt/0m, use one"
echo "      of the following if you are using Bourne Shell or C-Shell:"
echo "              % DEVICE=/dev/rmt/0m; export DEVICE"
echo "            OR"
echo "              % setenv DEVICE /dev/rmt/0m"
echo "      You may also specify the source file on the command line:"
echo "              % $0 $IMAGE"
echo ""

echo "Press Enter to continue or interrupt this script, e.g. ^C to abort->"

read answer

if [ -f $IMAGE ]
then
	if [ -c $DEVICE ]
	then
		echo "$IMAGE is being written to tape. Please wait!"
		dd if=$IMAGE of=$DEVICE bs=10k
		echo "Verifying tape image:"
		rm ${TMP}/${IMAGE}
		dd if=$DEVICE of=${TMP}/${IMAGE} bs=10k
		cmp ${IMAGE} ${TMP}/$IMAGE > ${TMP}/diff_results || {
			echo "ERROR: The image on the tape does not match the"
			echo "       image in this directory. "
			echo "       TAPE BUILD FAILED!"
			exit 1
		}
		echo "Tape Build Successful!"
		rm ${TMP}/diff_results
		rm ${TMP}/$IMAGE
		exit 0
	else
		echo "ERROR: $DEVICE does not exist"
		exit 1
	fi
else
	echo "ERROR: $IMAGE does not exist"
	exit 1
fi
