#!/bin/ksh
##############################################################################
# (c) Copyright 1992 Hewlett-Packard Company.  All rights reserved.
#
# Filename:     checksum
#
# Description:  Uses sum(1) to checksum the LMX files and then compares
#               the result to the master that is stored on tape.
#
##############################################################################

PATH=$PATH:/usr/local/bin:.:
VIRTUALROOT=/tmp/.lmxchecksum
CHECKSUM=/tmp/.checksum
CHECKSUMCMP=/tmp/.checksum.cmp
export PATH CHECKSUM VIRTUALROOT CHECKSUMCMP

if [ $# -ne 1 ]
then
  echo "usage: $0 file|device"
  exit 1
elif [ `id -u` -ne 0 ]
then
  echo "$0 must be run as root"
  exit 1
fi

######################
rm -rf $CHECKSUMCMP
rm -rf $VIRTUALROOT
######################

mkdir -p $VIRTUALROOT
cd $VIRTUALROOT

echo "\tRetrieving files from update image ..."
tar -xpf $1

echo "\tRe-calculating checksums using sum(1) ..."
find . -type f -exec sum {} \; | sort +2.0b +3.0b > $CHECKSUMCMP

ls -nHR | egrep -v -e "LM_MAN|LM_RUN|system|total" | tr -s " " | \
    cut -d" " -f1-5,9 | \
	sed -e "/system\/$/s/ [0123456789]* / /" | \
	sed -e "/usr\/$/s/ [0123456789]* / /" |
	sort -u >>$CHECKSUMCMP

echo "\tdiff(1) master checksum file with our checksum file ..."
echo "\tdiff $CHECKSUM $CHECKSUMCMP"
if diff $CHECKSUM $CHECKSUMCMP ; then
  echo "\tdiff(1) shows that checksums are identical."
else
  echo "\tdiff(1) shows that checksums are different!!!"
fi

cd /
rm -rf $VIRTUALROOT $CHECKSUM $CHECKSUMCMP

# Done
exit 0
