#! /bin/sh

# gettoc - get table-of-contents file for the current tape.
# Reads the tape header, deduces what kind of dump tape this is if any,
# then looks in ./tocdir/{date-of-tape} to find what's on it.
# Links ./tocdir/{date} to ./toc for use by other scripts
#  (localtar, remotetar, localdump, remotedump).
# If successful, positions tape after last indicated file (ready to append)
# If "-r" flag, rewinds instead.
# If "-a" flag, really prepares to append: skips to indicated file,
#   backspaces one file, writes fresh EOF mark.
#
PATH=/u/dump/bin:/usr/local/bin.sun3os3:/usr/local/bin:/usr/ucb:/bin:/usr/bin:/etc:/usr/etc

TAPE=${TAPE:-/dev/nrrt0}
TOCDIR=./tocdir
TOC=./toc
BSIZE=${BSIZE:-64512}

skipopt=
case "$1" in
    -[ra]) skipopt=$1; shift ;;
    -*)  echo >&2 \
"Usage:	$0 [-r] [-a]
Identifies tape in drive given by TAPE envariable.
Links its table-of-contents file in ./tocdir/* to file ./toc
for use by {local,remote}{dump,tar} scripts.
Positions after last tape file indicated by toc unless
-r => rewind tape after finding toc or
-a => position to last file, then mt bsf, mt weof (recommended for appending)."

	exit 1
	;;
esac

if [ ! -d ${TOCDIR} ]; then
	echo "$0: You must cd to where '$TOCDIR' is first." >&2
	exit 1
fi

rm -f tapelabel tapetoc $TOC
DDT=/tmp/gettoc.$$
s=0
trap "rm -f tapetoc ${DDT}; exit "'$s'  0

echo -n Fetching table-of-contents from $TAPE ... ""
if m="rewind" && mt rew && \
   m="read first block on" && dd if=$TAPE of=tapelabel bs=$BSIZE count=1 2>$DDT
then :
else
	if [ -f $DDT ]; then cat $DDT; fi
	echo "$0: could not even $m $TAPE.  I quit." >&2
	s=2; exit
fi
echo ""

if TAPE=tapelabel rlabel >tapetoc 2>&- ; then
	TAPETOC=$TOCDIR/`cat tapetoc`
	format=rawdump
else
	set -- "" `head -2 tapelabel`
	case "$2#$3#$4#$5#$6#$7" in
	OKTODUMP#[0-9]*#*#*#*#[0-9][0-9].[0-9][0-9].[0-9][0-9])
	    # Viktor Dukhovni format dump tape
	    # "OKTODUMP\nNDumps\nHost Filesys Level yy.mm.dd\nHost ..."
		TAPETOC="$TOCDIR/$7"
		format=Viktor
		;;
	*able#of#*ontents#[0-9][0-9].[0-9][0-9].[0-9][0-9]*#*)
	    # Minnesota format dump tape
	    # "Table of contents yy.mm.dd.hh.mm nnnnnn toc\n"
		TAPETOC="$TOCDIR/$5"
		format=minn1
		;;
	*)
		echo >&2 \
"The tape in $TAPE appears to be neither an old-format raw dump tape,
new (3/89) Viktor format, nor new (4/89) Minnesota format.
To initialize a tape to Minnesota format use   'inittoc'."
		s=3; exit
	esac
fi

if [ ! -f $TAPETOC ]; then
	echo "There is no table-of-contents file $TAPETOC, constructing one..."
	case "$format" in
	Viktor)
		(echo Table of contents $7.00.00 $BSIZE toc;
		 tr <tapelabel | tail +3) >$TAPETOC
		;;
	minn1)
		tr <tapelabel >$TAPETOC
		;;
	*)
		echo "Can't construct toc file for raw dump tapes.  Do it manually.  I quit."
		s=4; exit
		;;
	esac
fi
ln $TAPETOC $TOC
echo -n Table of contents in $TOC. " "
case "$skipopt" in
    "")
	n=`wc -l <$TOC`
	echo -n Skipping $n files ... ""
	mt fsf $n
	;;
    -a)
	n=`wc -l <$TOC`
	echo -n Skipping $n files ... "" && mt fsf $n \
	&& echo -n Backspacing 1 ... "" && mt bsf \
	&& echo -n Rewriting EOF ... "" && mt weof
	;;
   -r)
	echo -n "Rewinding ... "
	mt rew
	;;
esac
s=$?
echo ""
exit $s
