: #	@(#)ppshow	1.2	status reporter for ppurge system
# ppshow:
#	Show for a given Ouser either all his ppurged files, all files
#	that would be ppurged, or all files everywhere.  Results are 
#	mailed to the invoker, or admin if LOGNAME is not set.
# 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
TMP=/tmp/ppsh$$ export TMP
trap "rm -f $TMP" 0
export SA

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

CMD=`basename $0`
USAGE="
Usage:  nohup $CMD [-p] [-o] [-a] [-f] Ouser &

Finds all files owned by Ouser that have been ppurged (-p, default),
that would be ppurged (-o for \"originals\"), or everywhere altogether
(-a for \"all files\").  The -f option forces processing for an active
user name (not of the form 'Ouser').  Attempts to mail most output to
the invoker or to admin.  
"

set -- `getopt poafh\? $*`
if test $? != 0
then
	echo "$USAGE"
	exit 2
fi
ERR_ONEOPT="$CMD:  Only 1 option from -p, -o, and -a may be selected."
for i in $*
do case $i in
	-p)	if test "$OPTION" != ""
		then echo "$ERR_ONEOPT"; echo "$USAGE"; exit 2; fi
		OPTION=purgatories; shift;;
	-o)	if test "$OPTION" != ""
		then echo "$ERR_ONEOPT";echo "$USAGE";exit 2; fi
		OPTION=originals; shift;;
	-a)	if test "$OPTION" != ""
		then echo "$ERR_ONEOPT";echo "$USAGE";exit 2; fi
		OPTION=everything; shift;;
	-f)	FORCE=true; shift;;
	-[h\?])	echo "$USAGE"; exit 0;;
	--)	shift; break;;
	esac
done
OUSER=${1:?"no Ouser given
$USAGE"}
OPTION=${OPTION:-purgatories}

# 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." 1>&2
	#exit 4
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

# Prepare informative mailx subject line.
SUBJECTLINE="$CMD $OUSER $OPTION" export SUBJECTLINE

# Prepare for the worst.
trap "" 1 2 3	# resist stray interrupts
trap "status \"$CMD KILLED\";	# leave evidence for the coroner
exit 15" 15

# Find which name is used for the /fjc (/app) file system so we can
# grep out duplicate reports the linked alternate root name.
ROOTS=`/etc/mount | fgrep -v '/remote ' | awk '{print $1}'` export ROOTS
for fs in $ROOTS
do
	case "$fs" in
	/app)	FS_DUPL=/fjc; break;;
	/fjc)	FS_DUPL=/app; break;;
	esac
done
export FS_DUPL

# Do the work.
status "begin $CMD $OPTION"
case "$OPTION" in
purgatories)
	# Don't need to grep out FS_DUPL; we are only finding
	# from specifically mounted real file systems.
	find `/etc/mount | fgrep -v '/remote ' |
	    awk '{print  $1 "/purgatory"}'` \
	    -user $OUSER -print 2>&1 |
	mailx -s "$SUBJECTLINE" $SA
	;;

originals)
	# Create a sed script (TMP) to delete purgatory file references.
	# This script will filter the output of find / -print.  We first
	# find the list of file system root directories from the list of
	# mounted file systems.  This assumes both that all file systems
	# are mounted, and that the mount table is healthy.
	for fs in $ROOTS
	do
		# First, escape all the slashes in mount point names.
		# Then make the cmd to find and delete all purgatory 
		# subfiles.
		case "$fs" in
		/)
			# root filesystem is special case:
			# //purgatory will not match anything.
			echo "/^\\/purgatory\\//d" >> $TMP
			;;
		*)
			echo $fs | 
			sed 's:/:\\/:g
			s:^.*$:/^&\\/purgatory\\//d:' >> $TMP
			;;
		esac
	done

	# Find everything, filter out purgatories.
	if test "$FS_DUPL" != ""
	then
		# Need to filter out /fjc (/app) duplicates.
		find / -user $OUSER -print |
		sed -f $TMP |
		grep -v "^$FS_DUPL/" |
		mailx -s "$SUBJECTLINE" $SA
	else
		find / -user $OUSER -print |
		sed -f $TMP |
		mailx -s "$SUBJECTLINE" $SA
	fi
	;;

everything)
	if test "$FS_DUPL" != ""
	then
		# Need to filter out /fjc (/app) duplicates.
		find / -user $OUSER -print 2>&1 |
		grep -v "^$FS_DUPL/" |
		mailx -s "$SUBJECTLINE" $SA
	else
		find / -user $OUSER -print 2>&1 |
		mailx -s "$SUBJECTLINE" $SA
	fi
	;;
esac
# Hack away the bug in mail that lets mail messages get merged.
# If msgs don't come at the same time, they won't be merged.
sleep 10
status "end $CMD $OPTION"
