#! /usr/local/bin/setuid -r
#? /bin/sh /u/dump/bin/slavetar
#
#  - Stuart Levy, Geometry Group, April 1989
#
# slavetar [-a] [-G] [-N yy.mm.dd[.hh.mm[.ss]]] filesystem ...
#
# Privileged shell script.  Gnutar's the specified filesystem(s) to stdout.
#
# -a  Preserve access times on files in the filesystem's.
#     Unfortunately this means their inode-change times are updated,
#     so this screws up incrementals -- all those files will be
#     dumped on the next incremental.
# -N yy.mm.dd...
#     Dump only files modified since the specified date&time.
# -G  Generate Gnutar incremental information (needed even on level-0 "tar"s),
#     also add volume header of `hostname`:filesys1:filesys2:...
#     Unfortunately it generates tar images incompatible with many other tars.
#

CPU=`cat /etc/cputype`
PATH=/usr/local/bin:/usr/local/bin.$CPU:/usr/ucb:/usr/bsd:/bin:/usr/bin:/etc:/usr/etc

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

while :
do
    case "$1" in
	-N) since="$2" ; shift 2 ;;
	-n) echo $0: ignoring $1 >&2 ; shift ;;
	-a) atime=-a   ; shift ;;
	-[GV]) dognu=-GV ; shift ;;
	-C) break ;;
	-*) shift $#   ; break ;; # force Usage message
	*)  break ;;
    esac
done

if [ "$1" = "" ]; then
	echo >&2 \
"Usage:	$0 [-a] [-G] [-N yy.mm.dd[.hh.mm]]  /filesystem ...
Do GNU tar dump of filesystem(s) or file subtree(s) on local host.
Writes to stdout for handling by a remote 'remotetar' script.
-a : preserve access times of tarred files (but screw up later incrementals).
-N yy.mm.dd... : dump only files changed since that date/time.
-G Use gnutar-style directory records (for incrementals); also write volume
   headers of hostname:filesys1:filesys2:...  (Incompatible with other tars!)"
	exit 2
fi

# 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

host="`hostname`"
volnames="`echo $* | tr ' \11' ::`"
case "$1" in
	/*) fs="-C $1 ." ;;
	*)  fs="$1" ;;
esac
shift
fs="$fs $*"

# Parse "since" date into something gnutar can read.
OIFS="$IFS"; IFS=". 	"
set -- "" $since
IFS="$OIFS"
shift
if [ $# = 1 -a "$since" = "" ]; then shift; fi
case $# in
	0) gdate= ;;
	3)
	    gdate="$2/$3/$1" ;;
	4)
	    case "$1#$2#$3#$4" in
		[0-9]*#[0-9]*#[0-9]*#[0-9]*:*)
			gdate="$2/$3/$1 $4"
			;;
		*)
			echo "$0: what kind of yy.mm.dd.hh.mm spec is '$since'?  I quit." >&2
			exit 4
			;;
	    esac
	    ;;
	5)  gdate="$2/$3/$1 $4:$5" ;;
	6)  gdate="$2/$3/$1 $4:$5:$6" ;;
	*)
	    echo "$0: what kind of yy.mm.dd spec is '$since'?  I quit." >&2
	    exit 4
	    ;;
esac

# Now for it!


if [ "$dognu" != "" ]; then
	dognu="-GV ${host}:${volnames}"
fi
if [ "$gdate" != "" ]; then
	gnutar -cl ${dognu} ${atime} -f - -N "${gdate}" -- $fs
else
	gnutar -cl ${dognu} ${atime} -f - -- $fs
fi
