: #	@(#)pflush	1.2	remove (or archive) Ouser files
# pflush,parchive
#	Pfind and delete all files owned by a given Ouser in purgatory.
#	Optionally just outputs the names of the files for feeding a
#	cpio archive.
# Author:  Larry Bamford, AO, OAT, STD, SEB, ETC, 8/13/90

SA=${LOGNAME:-admin}	# notify whoever started this, else admin
PATH=/bin:/etc:/usr/bin:/adm export PATH	# for security

# Define status reporting function.
STATUSFILE=${ADM:-/adm}/lastrites export STATUSFILE
status() {
	echo "$OUSER|`date`|$1" >> $STATUSFILE
}

CMD=`basename $0`
USAGE="
Usage:  parchive [-f] [-v] Ouser > /dev/anytape
        nohup pflush [-f] Ouser &

If invoked as parchive, finds and archives to standard output (cpio -cB
format) all files in all purgatory directories owned by the given
Ouser.  If invoked as pflush, deletes all files owned by Ouser in the
purgatory directories.  The files are archived relative to the /
directory, i.e., full pathnames with a preceding dot.  pflush attempts
to mail most output to the invoker or to admin if LOGNAME is not set.

Options:
	-f	forces processing for a active user name (not of the 
		form 'Ouser')
	-v	use verbose option with cpio (parchive only)

parchive and pflush are identical and should be linked.
"

# Switch mode depending on command name used.
case "$CMD" in
parchive)	ARCHIVE=true ;;
pflush)		DELETE=true ;;
*)		echo "$USAGE"; exit;;
esac

# Process and verify rest of command line.
set -- `getopt avfh\? $*`
if test $? != 0
then
	echo "$USAGE"
	exit 2
fi
for i in $*
do case $i in
	-v)	VERBOSE=v; shift;;	# cpio option
	-f)	FORCE=true; shift;;
	-a)	ALL=all; shift;;	# unadvertized by Usage msg
	-[h\?])	echo "$USAGE"; exit 0;;
	--)	shift; break;;
	esac
done
case "$FORCE$ALL" in
trueall)
	echo "$CMD:  incompatible options, -f and -a"
	exit 2
	;;
esac
if test "$ALL" = "all"
then
	if test $# -gt 0
	then
		echo "$CMD:  -a and Ouser cannot both be specified."
		exit
	fi
else
	OUSER=${1:?"no Ouser given
	$USAGE"}
fi

# Check form of OUSER name.
case "$OUSER" in
O*) : ok ;;
*)
	if test "$FORCE" != "true"
	then
		echo "$CMD:  $OUSER is not a former user."
		exit 5
	fi
	;;
esac

# Check for existence of OUSER in password file.
if grep "^$OUSER:" /etc/passwd > /dev/null
then : proceed
else
	echo "$CMD:  $OUSER not found in /etc/passwd."
	exit 6
fi

# Get either all files in purgatory, or just OUSER's files.
case "$ALL" in
all)	UPHRASE="" ;;
*)	UPHRASE="-user $OUSER" ;;
esac

# Provide the courtesy of reminding him that privilege is necessary.
if test "`id | grep -c -v 'uid=0('`" -eq 1
then # am not superuser
	echo "$CMD: Only superuser or admin may run this program."
	exit 4
fi

# Ok, do the work.
trap "" 1 2 3	# resist stray interrupts
trap "status \"$CMD KILLED\";	# leave evidence for the coroner
exit 15" 15
status "begin $CMD ${ALL:-$OUSER}"
if test "$DELETE" = "true"
then
	# Delete the files.  Here we can use full pathnames.
	find `/etc/mount | fgrep -v '/remote ' |
	    awk '{print  $1 "/purgatory"}'` \
	    -depth $UPHRASE -print |
	awk '{print "rm -f", $0, "2>/dev/null || rmdir", $0}' |
	/bin/sh -s 2>&1 |
	mailx -s "$CMD ${ALL:-$OUSER} output" $SA
else
	# Feed the names of the potential victim files, relative to
	# the / directory, into cpio.  Everything is archived by
	# relative path name, so we need to be in / or else.
	OLDDIR=`pwd`	# remember where we are
	cd /		# relative paths are only good from here
	find `/etc/mount | fgrep -v '/remote ' |
	    awk '{print  $1 "/purgatory"}'` \
	    $UPHRASE -print |
	sed 's/^/\./' |	# give relative pathnames
	cpio -ocB$VERBOSE	# user better catch stdout on tape...
	cd $OLDDIR	# go back whence we came
fi
status "end $CMD ${ALL:-$OUSER}"
