#!/bin/sh

# Contributed by Doug Blakewell - dbakewell@bfound.com 5/1/2000
#
# The agent watches to see when files change and sends an error. It is meant
# to watch small files that should not be changing on a regular basis. If the
# file does change the agent sends an error and also tells you the location of
# the original file which it has a copy of. This has only been tested on
# Linux. The args for the agent is a list of files to watch. All the base
# names for the files must be different since they are all copied into
# $BCNUTMP/files/.

# Modified by JP - works on solaris

#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

OK=1

if [ ! -d $BCNUTMP/files ]; then
        mkdir -p $BCNUTMP/files
fi

for param in $BCNUPARAM
do
bcnu_param
#set -x

file=$p1
basefile=`basename $file`

#Note if $file returns "/blah/blah/hello" then ${file##*/} returns just "hello"

#if we don't have an original of this file
if [ ! -f $BCNUTMP/files/$basefile ]; then
        cp $file $BCNUTMP/files/$basefile
	  #protect the origanal copy of the file
        chmod og-rwx $BCNUTMP/files/$basefile
fi

# do a full diff to make sure the files are exactly the same. Date stamps and
# size can be faked.

diff $file $BCNUTMP/files/$basefile > /dev/null
if [ $? = 1 ]; then
        BCNUMSG="$BCNU -m \"error - $file has changed. Orig in $BCNUTMP/files/$basefile \" -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
   bcnu_err_send
   OK=
fi
done

if [ $OK ]
        then
                BCNUMSG="$BCNU -m 'ok - no critical files have changed' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
                bcnu_send
fi
