#       @(#)lightenfs   1.2     /sccs/src/cmd/sadmin/shell/s.lightenfs
#       "lighten the ship" routine to clean up file systems

#!      chmod +x ${file}

set -- `getopt d:lt:v "$@"`  ||  exec $0

days=4
listonly=
threshold=4000
verbose=
for flag
{
       case "${flag}" in
       -d )
               days=$2
               shift
               ;;
       -l )
               listonly=yes
               ;;
       -t )
               threshold=$2
               shift
               ;;
       -v )
               verbose=yes
               ;;
       -- )
               shift
               break
               ;;
       -* )
               admerr $0 "Bad flag argument '${flag}'."
               exit
               ;;
       * )
               continue
       esac
       shift
}

if [ $# -ne 2 ]
then
       echo >&2 "Usage:  $0 [-d<days>] [-t<threshold>] [-v] [-l] filesystem patternfile
For more detail type:   prtdoc syscmd.`basename $0`"
       exit 1
fi

fs=$1
patternfile=$2
blocks=`expr "\`df ${fs}\`" : '.*[^0-9]\([0-9]\{1,\}\) blocks '`
if [ ${blocks} -ge ${threshold} ]
then
       if [ ${verbose} ]
       then
               echo ${blocks} blocks greater than threshold ${threshold}
       fi
       exit 0
fi
if [ ${verbose} ]
then
       echo ${blocks} blocks free, need ${threshold}
fi
patterns=`sed 's/.*/\\\\;&;p/' ${patternfile}`

find ${fs} -type f -mtime +${days} -print 2>/dev/null  |
       sed -n "${patterns}"  |
       xargs ls -tr  |
       while read f
       do
               if [ ${listonly} ]
               then
                       echo ${f}
                       continue
               fi
               rm -f ${f}
               blocks=`expr "\`df ${fs}\`" : '.*[^0-9]\([0-9]\{1,\}\) blocks '`
               if [ ${verbose} ]
               then
                       echo rm ${f} -- now ${blocks} blocks free
               fi
               if [ ${blocks} -ge ${threshold} ]
               then
                       if [ ${verbose} ]
                       then
                               echo Threshold met.
                       fi
                       #       swallow the rest of the ls
                       cat >/dev/null
                       exit 0
               fi
       done