#!/bin/sh
#
#	prodsdiff - SGI/IRIX software inventory difference checker
#
# 21-mar-1996 jrv initial release <john.vanderpool@gsfc.nasa.gov> (fish)
#
# NOTE: install as /etc/init.d/prodsdiff and
#       ln -s /etc/init.d/prodsdiff /etc/rc2.d/S99prodsdiff and then
#	call it with init option to initialize 
#
#	(there is no corresponding /etc/rc0.d/K* link needed)
#	
# (if you're still at irix 4.X just uses "versions -b" for prodsbin)

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

self=`basename $0`
prodsbase=/usr/adm			# makes it easier to test in /usr/people
prodstmp=$prodsbase/prods.tmp
prodsout=$prodsbase/prods.out
prodsold=$prodsbase/prods.old
prodsbin="/usr/sbin/showprods -D1"
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 $prodsout ]; then		# not first, this is a rotate
    /bin/mv -f $prodsout $prodsold	# save old
  fi

  $prodsbin > $prodsout			# make new
  exit 0				# bail out
fi

$prodsbin > $prodstmp

if [ -f $prodsout ]; then

  diff $prodsout $prodstmp >/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

    (					# begin data encapsulation via subshell
    echo
    diff $prodsout $prodstmp
    echo "\n< = previous prods"
    echo "> = current  prods"
    echo "\n${self}: ^^^^^ WARNING! ^^^^^ `hostname -s` software inventory has changed." 
    ) | $mailbin -s "`hostname -s` software inventory has changed!" $notify

    echo
    diff $prodsout $prodstmp
    echo "\n< = previous prods"
    echo "> = current  prods"
    echo "\n${self}: ^^^^^ WARNING! ^^^^^ `hostname -s` software inventory has changed." 
    /etc/init.d/prodsdiff init		# update for next run

  fi	# endif prods were different

else					# in case they forgot to init first time

  echo "${self}: $prodsout does not exist, automatically creating for next run."
  $prodsbin > $prodsout

fi
