#! /usr/local/bin/setuid -r
#? /bin/sh /u/dump/bin/localdump
#
#  - Stuart Levy, Geometry Group, April 1989
#
# localdump [-n] {0-9}[u] /filesystem-or-device
#
# Privileged shell script.  Dump(8)'s the specified local filesystem to $TAPE.
# Only one filesystem is permitted.
#
# {0-9}	Dump level - a single digit in the range 0..9.  Required.
# u	Update the local /etc/dumpdates file.
#
# No other dump options are accepted.
#
# One or two lines are added to the ./toc (tape table-of-contents) file:
# hostname  filesystem   level  yy.mm.dd.hh.mm  nbytes  "dump"
# The date is that at the beginning of the dump.
#
# A copy of the table-of-contents is written to tape after the dump
# unless the -n option is given.  If written, another line is added to toc:
# "Table" "of" "contents"  yy.mm.dd.hh.mm  nbytes  "toc"
#

notoc=
if [ "$1" = "-n" ]; then
	notoc=-n; shift
fi

case "$1#$2#$3" in
    [0-9]#/*#|[0-9]u#/*#)
	;;
    *)
	echo >&2 \
"Usage:  $0  [-n]   {0-9}[u]  /filesystem-or-device
Dump(8) specified local filesystem to tape given by TAPE envariable.
Digit 0..9 is dump level, optional 'u' => update /etc/dumpdates.
Writes a copy of the table-of-contents to tape after the dump unless -n."
	exit 1
	;;
esac

if [ ! -w toc ]; then
	echo "$0: must have ./toc present and writable.  Run gettoc first." >&2
	exit 1
fi

CPU=`cat /etc/cputype`
PATH=/u/dump/bin:/u/dump/etc.$CPU:/usr/local/bin.$CPU:/usr/ucb:/usr/bsd:/bin:/usr/bin:/etc:/usr/etc
TAPE=${TAPE:-/dev/nrrt0}
BSIZE=${BSIZE:-64512}
BLKS=`expr $BSIZE / 512`
TSIZE=145000

DATE="`date +%y.%m.%d.%H.%M`"


# Security check.  We should only be executed by uid "dump"!
# SETUID_REALUSER provided by a hack to /usr/local/bin/setuid.

case "$SETUID_REALUSER" in
    dump|root) ;;
    *) echo "$0 may only be invoked by user 'dump'.  Go away." >&2; exit 3 ;;
esac

dumpopts=$1
fs=$2

Q=
if [ -x /u/dump/etc.$CPU/dump ]; then
	Q=q
fi

TEMP=/tmp/buffer.$$
rm -f $TEMP

dump $dumpopts${Q}fsb $TAPE $TSIZE ${BLKS}  $fs  2>&1 | maketoc $fs >>./toc
s=$?

if [ "$s" = 0 -a "$notoc" = "" ]; then
	cp ./toc $TEMP
	echo Table of contents `date +%y.%m.%d.%H.%M` $BSIZE toc >>$TEMP
	if dd if=./toc of=$TAPE bs=$BSIZE conv=sync; then
		cp $TEMP ./toc
	else
		echo $0: Error writing table-of-contents to $TAPE >&2
		s=1
	fi
fi
rm -f $TEMP
exit $s
