
#!/bin/sh
# File  : chk_rc-files.sh
# By    : Maarten.deBoer@AtosOrigin.com, 180502
# Subject       : Script to Check if RC file has been changed
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
FILERS="${HOME}/etc/filers"
SSH="/usr/bin/ssh -n"
HOSTNAME=`hostname |cut -d\. -f1`
MAILTO="maarten.deboer@atos.net"
FILTER="[?]*"

DATADIR="/appl/dfm/rc-files"

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options       :"
  echo "    -f          : Filter "
  echo "    -h | --help : this help"
  echo "    -V          : Version"
  echo "    -x          : set -x"
  echo "    --set       : do SET"
}
# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    --set) SET=1 ;;
    -f) FILTER="${2}" ; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 2 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

echo "`date` ${PGM} v${VER}  started"|tee -a ${LOG}
if [ ! -d ${DATADIR} ]; then
  echo "  NO data-dir (${DATADIR}) available. Exiting ..."|tee -a ${LOG}
  exit 3
fi

cd ${DATADIR}
mkdir .test
EC=${?}
if [ ${EC} -ne 0 ]; then
  echo "  NOT be able to create (test)dir in DATADIR (${DATADIR}). Exiting ..."|tee -a ${LOG}
  exit 4
else
  rmdir .test
fi
echo "Ran at `date`" > ${TMP}

cat ${FILERS}|grep -v ^#|awk '{print $1}'|grep ${FILTER}| while read FILER REST
do  
  echo "  ${FILER} ..."
  if [ ! -d ${FILER} ]; then
    mkdir ${FILER}
  fi
  DATI=`date +%Y-%m-%d-%H-%M-%S`
# Collect RC-file and save as one with Date&Time-stamp
  ${SSH} ${FILER} "rdfile /etc/rc" > ${DATADIR}/${FILER}/${FILER}_${DATI}
# Check collected RC-file with current one
  if [ -f ${DATADIR}/${FILER}/${FILER}.rc ]; then
    echo "  Check ${FILER}-RC with ${DATADIR}/${FILER}_${DATI} ..."
    cat ${DATADIR}/${FILER}/${FILER}.rc|grep -v ^# > ${TMP}.rc
    cat ${DATADIR}/${FILER}/${FILER}_${DATI}|grep -v ^# > ${TMP}.dati
    diff ${TMP}.rc ${TMP}.dati > ${TMP}.diff
    EC=${?}
    if [ ${EC} -ne 0 ]; then
# Diff found.
      echo "  Diff in ${FILER} RC-file:"|tee -a ${LOG} ${TMP}
      cat ${TMP}.diff >> ${TMP}
    fi

    rm ${TMP}.rc ${TMP}.dati ${TMP}.diff
  else
    echo "  NO current .rc-file for ${FILER}"| tee -a ${LOG} ${TMP}
  fi

done  # cat ${FILERS}


cat ${TMP}|mailx -s ":${HOSTNAME}: Check RC-files [${PGM} v${VER}]" ${MAILTO}
echo "  Mailed to ${MAILTO}"|tee -a ${LOG}

rm ${TMP}
echo "`date` ${PGM} v${VER} finished"|tee -a ${LOG}
exit 0

