#!/bin/sh
set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi
# this is cheap argument processing. Must be in this order when checking.
#mailto=$ENSTORE_MAIL
mailto="bakken@fnal.gov berman@fnal.gov huangch@fnal.gov moibenko@fnal.gov zalokar@fnal.gov"
if [ "${1:-}" = "-M" ] ; then mailto=""; shift; fi
if [ "${1:-}" = "-a" ] ; then ALARM=1; shift; else ALARM=0; fi
if [ "${1:-}" = "-m" ] ; then mailto=${2:-$ENSTORE_MAIL}; shift; shift; fi

#echo \"$mailto\"
#echo $ALARM
#exit 1

gang=`gang`
out=/tmp/chk_prod_code
rm -f $out ${out}_1

source $ENSTORE_DIR/sbin/gettkt
rgang $gang " cd /home/enstore/enstore; cvs -qn update -r production" > $out 2>&1

# get rid of known junk
egrep -v " cvs -qn update -r production| This rsh session is using DES encryption for all data transmissions|M etc/enstore_system.html" $out > ${out}_1
mods=`egrep -v " - - - - - - - - - - - - - " ${out}_1`

# anything left is unknown or changed files
changes=`echo "$mods" | sed -e 's/ //g'|wc -w`  # this is either 0 or 1, not a count
if [ $changes -ne 0 ]; then
   # dang listserver suppress identical messages. make the mail unique by adding date
   msg="`date` - cvs production tag inconsistencies found in $gang"
   echo "" >> ${out}_1
   echo "" >> ${out}_1
   echo "SUMMARY:$msg" >> ${out}_1
   if [ $ALARM -eq 1 ]; then
      echo enstore alarm --raise --severity "A" --root-error "$msg"
           enstore alarm --raise --severity "A" --root-error "$msg"
   fi
   if [ "$mailto" != "" ]; then
      /usr/bin/Mail -s "$gang not running latest production code.  Developer Primary check and fix and restart." $mailto <${out}_1
   fi
fi

cat ${out}_1

exit 0
