
:
#######################################################
# fchange.sh: log and report upon files that have 
# changed.  requires fchangedir control directory, logs 
# subdirectory, and fchange.files control file.
# Copyright (C) 1993 Steven G. Isaacson
#######################################################

test "$debugxv" && set -xv # primitive debugger

tmpfile=/tmp/fch.$$
: ${HOME:=/usr/stevei}
fchangedir=$HOME/cron/fchange
logdir=$fchangedir/logs
parm_file=${fchangedir}/fchange.files
def_mail_to=stevei ; export def_mail_to
mailer=/bin/mail ; export mailer
subject="On `uname -n` this file was changed recently"
eqsn="======"

PATH=/bin:/usr/bin:/usr/ucb:.:$PATH
export PATH

cd $fchangedir

# use awk to skip comments, blank lines, null lines, and 
# colons 

awk '/^#|^ |^:|^$/ { next }
{ print }' $parm_file |
while read unique fname mail_to
do
    # stop if there is a problem
    set -e

    logname=${logdir}/${unique}.log

    # if the log file isn't there, create it
    test -f "$logname" || > $logname

    # set tracking flag.  default is no
    track=no

    case "$mail_to" in
        *track) track=yes
            mail_to=`echo $mail_to | sed 's,track,,'` ;;
    esac

    # save original number of lines in log file
    oldlen=`cat $logname | wc -l`

    # those particularly concerned about security may 
    # also want to add the output from sum (1-SysV) to 
    # the line.  that way, even if the size and time 
    # stamps are rigged, you'll still see that a change 
    # was made.

    test -f "$fname" && /bin/ls -l $fname >> $logname

    # sort the log file uniquely.  if the file in 
    # question hasn't changed, then the line we just 
    # added with ls is a duplicate and will be removed.

    sort -u $logname -o $logname

    # get new number of lines in log file
    newlen=`cat $logname | wc -l`

    # if there is a difference, send mail
    test "$oldlen" -eq "$newlen" || {

        echo "Subject: $subject ($unique)" > $tmpfile
        tail $logname >> $tmpfile
        test "$mail_to" || mail_to=$def_mail_to
        cat $tmpfile | $mailer "$mail_to"

        test "$track" = "yes" && {
            echo "#_change $eqsn `date` $eqsn" \
              >> ${logname}.track
            cat $fname >> ${logname}.track
        }
    }
done
exit 0

