#! /bin/sh
##
##  Script to find old core, etc., files and send mail to their owners.
##  It assumes your find has the "-xdev" option to not cross mount
##  points.  It would like to use System V "xargs" command, but can
##  use the BSD "fmt" command as a hack, instead.  Also check the mail
##  commands, below.
##
##  The file core_errs will contain any errors; core_ls will be the full
##  list of old and bogus files.
##

##  Setup.
cd /usr/adm
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin
HOST=`hostname`
rm -f core_ls core_errs core_unknown

##  If first argument is "-i" then we're interactive.
if [ "x$1" = "x-i" ] ; then
    shift
else
    exec 2>core_errs
fi

##  Optional next argument is how old files should be before we complain.
AGE=${1-5}

##  Cute trick to get list of directly-mounted filesystems, only.
FILESYSTEMS=`df | awk '$1 ~ /^\/dev\// {print $6}'`

##  You might want to tweak the predicates in here.
find ${FILESYSTEMS} \( \
	    -name core -o -name a.out \
	    -o -name 'drft.[0-9]*' \
	    -name 'msg[0-3][0-9][JFMASOND]*' \
	    -o -name ',*' \
	    -o -name '#*' -o -name '.#*' \
	    -o -name '*~' -o -name '.*~' \
	    -o -name '.tpen*' \
	\) -a -xdev -a -mtime +${AGE} -print \
    | fmt -200 | sed -e 's@^@/bin/ls -l @' | sh >core_ls
#  If you have xargs, it is more efficient.
#   | xargs /bin/ls -l >core_ls

##  Unlikely, but maybe we didn't find anything.
if [ ! -s core_ls ]; then
    exec rm -f core_ls
fi

##  Assuming a standard "ls", third field is the users.  Loop over list
##  of all badguys.  The funky grep commands ignore filenames; be careful
##  if your "ls -l" uses tabs.
for c in `awk '{print $3}' <core_ls | sort -u` ; do
    case $c in
    _*|[0-9]*)
	grep " $c " core_ls >>core_unknown
	;;
    *)  
	(
	    echo "You have the following old files on the ${HOST}:"
	    grep " $c " core_ls
	    echo "which are greater than ${AGE} days old."
	    echo ''
	    echo 'If you are finished with them, it would be nice to remove'
	    echo 'them, freeing up the disk space.'
	    echo ''
	    echo 'Thanks,'
	    echo ''
	    echo "The Filesystem on ${HOST}"
	    echo ''
	) | \
	    Mail -s "Old files on ${HOST}" $c
# We use sendmail; MMDF is probably this:
#	    v6mail -to $c -subj "Old files on ${HOST}"
	;;
    esac
done
if [ -s core_unknown ] ; then
    (
	echo "Found unknown old files on ${HOST}:"
	cat core_unknown
    ) | \
	Mail -s "${HOST}: No owner for these ancient files." root
# We use sendmail; MMDF is probably this:
#	v6mail -to root -subj "${HOST}: No owner for these ancient files."
fi
