#!/bin/sh
#
# $Id$
#
# Check configuration file CRC's and compare with those internal to RSF-1.
#
RSFCLI="/opt/HAC/RSF-1/bin/rsfcli"
CRC="/opt/HAC/bin/hac_crc"
CONFIG_FILE="/opt/HAC/RSF-1/etc/config"
POPULATE=false
CRC_FILES="/opt/HAC/etc/crc.files"
CRC_SUM="/opt/HAC/etc/crc.sum"
FORCE="false"
REFRESH="false"
TESTDIRS="false"

export POPULATE

rsf_crc() {
    if [ ! -x "${RSFCLI}" ] ; then
	echo "${RSFCLI}: no such file or directory."
	return
    fi
    RUNNING_CRC=`/opt/HAC/RSF-1/bin/rsfcli stat | grep CRC | sed -n '1p' | sed 's/.*CRC =//'|awk '{print $1}'|sed 's/^0x//'`

    if [ ! -x "${CRC}" ] ; then
	echo "${CRC}: no such file or directory."
	return
    fi

    ONDISK_CRC=`${CRC} ${CONFIG_FILE}|sed 's/^0x//'`

    if [ ! -z "${ONDISK_CRC}" ] ; then
	echo "CRC for ${CONFIG_FILE} = ${ONDISK_CRC}"
    fi
    if [ ! -z "${RUNNING_CRC}" ] ; then
	echo "CRC for config in RSF-1 = ${RUNNING_CRC}"
    fi

    if [ -z "${RUNNING_CRC}" -o "${RUNNING_CRC}" = "" ] ; then
	echo "It appears RSF-1 is not running on this machine, therefore no check can be made"
	exit 1
    fi

    if [ "${RUNNING_CRC}" = "${ONDISK_CRC}" ] ; then
	echo "$0: ondisk and running configuration file CRC's are the same (${ONDISK_CRC} == ${RUNNING_CRC})"
    else
	echo "$0: *** ondisk and running configuration file CRC's differ!"
	echo "$0: ondisk CRC = 0x${ONDISK_CRC}, running CRC = 0x${RUNNING_CRC}"
    fi
}

do_crc_sum() {
    ${CRC} -p ${CRC_SUM} `cat ${CRC_FILES}`
}

populate_dirs() {
    echo "Populating HAC dirs with checksum snapshot data."
    if [ ! -d /opt/HAC/etc ] ; then
	mkdir -p /opt/HAC/etc
    fi
    if [ -f ${CRC_FILES} -a "${FORCE}" = "false" ] ; then
	echo "${CRC_FILES} already exists; use the -f flag to force overwrite"
	exit 0
    else
	find /opt/HAC -type f | grep -v ${CRC_SUM} | grep -v ${CRC_FILES} > ${CRC_FILES}
	do_crc_sum
    fi
}

#
# Parse command line args
while [ $# -gt 0 ]; do
    case "$1" in
	    -t)
	    TESTDIRS="true"
	    ;;

	    -r) 
	    REFRESH="true"
	    ;;

	    -p) 
	    POPULATE="true"
	    ;;

	    -f) 
	    FORCE="true"
	    ;;

	    -h)
	    echo ""
	    echo "Usage:"
	    echo "      $0 <-p> <-f> <-t> <-r> <-h>"
	    echo ""
	    echo "      -p   populate the list of files to save CRC information from."
	    echo "           The list is saved to the file ${CRC_FILES} and initially"
	    echo "           populated with all files under the /opt/HAC directory, "
	    echo "           the CRC sums of these files are then saved to ${CRC_SUM}"
	    echo "           Use the -c flag to check the ${CRC_SUM} file for changes"
	    echo "           If ${CRC_FILES} exists then it will not be overwritten;"
	    echo "           use the -f flag to force overwriting."
	    echo ""
	    echo "      -f   used in conjunction with the -p flag to force overwriting"
	    echo "           the file ${CRC_FILES}"
	    echo ""
	    echo "      -r   refresh the CRC checksum database from the CRC files list"
	    echo ""
	    echo "      -c   check the CRC sum database for changes to files since it"
	    echo "           was first populated"
	    echo ""
	    echo "      -h   print this help page"
	    echo ""
	    echo "Generally, on first installation use  the -p flag to populate the saved"
	    echo "CRC sums, then add any other files you want check summing to the end of"
	    echo "of the save file list (${CRC_FILES}), then run again with the"
	    echo "-r option to update the check sums. Finally run with the -c flag at any"
	    echo "time to check for file differences."
	    echo ""
	    exit 0
	    ;;
	esac
    shift
done

if [ "${POPULATE}" = "true" ] ; then
    populate_dirs
elif [ "${REFRESH}" = "true" ] ; then
    echo "Refreshing CRC sum database (${CRC_SUM}) from ${CRC_FILES}"
    do_crc_sum
elif [ "${TESTDIRS}" = "true" ] ; then
    if [ -f ${CRC_SUM} ] ; then
	${CRC} -c ${CRC_SUM}
    else
	echo "No ${CRC_SUM} file! Use the -p flag to create an initial CRC file."
    fi
else
    rsf_crc
fi