#!/bin/sh
#
#	hinvdiff - SGI/IRIX hardware inventory difference checker
#
# 10-dec-1993 jrv add init option, send diff output through mail not just msg
#  5-aug-1993 jrv initial release (John_Vanderpool@gsfc.nasa.gov)
#
# NOTE: install as /etc/init.d/hinvdiff and
#       ln -s /etc/init.d/hinvdiff /etc/rc2.d/S99hinvdiff and then
#	call it with init option to initialize 
#
#	(there is no corresponding /etc/rc0.d/K* link needed)

${SHDEBUG:-}            # export SHDEBUG="set -x" externally, else this is a nop

self=`basename $0`
hinvbase=/usr/adm			# makes it easier to test in /usr/people
hinvtmp=$hinvbase/hinv.tmp
hinvout=$hinvbase/hinv.out
hinvold=$hinvbase/hinv.old
hinvbin=/bin/hinv
mailbin=/usr/sbin/Mail
notify=root				# who to notify if there is a change

if [ "${1:-}" = "init" ]; then		# first time use or known change happened

  if [ -f $hinvout ]; then		# not first, this is a rotate
    /bin/mv -f $hinvout $hinvold	# save old
  fi

  $hinvbin > $hinvout			# make new
  exit 0				# bail out
fi

$hinvbin > $hinvtmp

if [ -f $hinvout ]; then

  diff $hinvout $hinvtmp >/dev/null 2>&1

  if [ $? != 0 ]; then			# h/w inventory has changed

    # its crazy to do this twice but w/o tpipe utility not much you can do

    echo
    diff $hinvout $hinvtmp
    echo "\n< = previous hinv"
    echo "> = current  hinv"
    echo "\n${self}: ^^^^^ WARNING! ^^^^^ `hostname -s` hardware inventory has changed." 
    echo "\nrun \"$0 init\" if this new configuration is correct."

    (					# begin data encapsulation via subshell
    echo
    diff $hinvout $hinvtmp
    echo "\n< = previous hinv"
    echo "> = current  hinv"
    echo "\n${self}: ^^^^^ WARNING! ^^^^^ `hostname -s` hardware inventory has changed." 
    echo "\nrun \"$0 init\" if this new configuration is correct."
    ) | $mailbin -s "`hostname -s` hardware inventory has changed!" $notify

  fi

else					# in case they forgot to init first time

  echo "${self}: $hinvout does not exist, automatically creating for next run."
  $hinvbin > $hinvout
fi
