#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved.
#
# Name - checksum.sh
#
# Purpose - Checksum the release files
#
# $Id: checksum.sh 26311 2011-08-01 22:42:15Z stever $
#
#=========================================================================

comid="checksum"
if [ "x$GEMVERSION" = "x" ]; then
  GEMVERSION=10
  export GEMVERSION
fi

# make sure of minimum path
PATH=:/bin:/usr/bin:/usr/ucb:/usr/etc:$PATH; export PATH

snefruFlags="-l4 -o8"
diffFlags=  # "-c0"

# Set up environment
getMyName() {
  ORIGDIR="`pwd`"
  if [ "$0" = "`basename $0`" ]; then
    # invoked from path
    SCRIPTNAME="`type -path $0`"
  else
    # some sort of path in $0...
    SCRIPTNAME="$0"
  fi
  SCRIPTDIR="`dirname $SCRIPTNAME`"
  # canonicalize SCRIPTDIR by using pwd.
  cd $SCRIPTDIR
  SCRIPTDIR="`pwd`"
  cd $ORIGDIR
  }
getMyName

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

Syntax="$comid <rootDir> <manifest> <resultFile> <omitFile> ["originate"]"
Descript="
This script checksums a release, as defined by a manifest.
The <rootDir> is the root of the release tree to be checksummed.
The <manifest> is the manifest file which gives the list of files to checksum.
The <resultFile> is used two different ways:
 1) If the fifth argument (originate) is missing, the checksums are compared
     against those given in <resultFile>, which are assumed to be good.
 2) If the fifth argument (originate) is given, the checksums are written to
     <resultFile>.  In this case, it is an error if <resultFile> already exists.
The <omitFile> is the name of a file to skip, commonly checksums.good.
This script currently uses the Xerox Secure Hash Function.
"
# give a little help
if [ "x$1" = "xhelp" ]; then
  echo "$Syntax"; echo "$Descript"; exit
fi

tmpFile="/tmp/checksumgs-$$"
outFile=/tmp/checksums.dat-$$
if [ "x$1" = "x" ]; then
  echo "$Syntax" ; echo "$Descript" ; exit 1
fi
rootDir="$1"
if [ ! -d $rootDir ]; then
  echo "$comid[Error]:    No such directory, $rootDir"
  exit 1
fi

if [ "x$2" = "x" ]; then
  echo "$Syntax" ; echo "$Descript" ; exit 1
fi
manifestFile="$2"
if [ ! -f $manifestFile ]; then
  echo "$comid[Error]:    No such manifest, $manifestFile"
  exit 1
fi

if [ "x$3" = "x" ]; then
  echo "$Syntax" ; echo "$Descript" ; exit 1
fi
resultFile="$3"

omitName="$4"
if [ "$omitName" = "" ]; then
  echo "$comid[Error]:    expected omitName"
  exit 1
fi

if [ "x$5" = "xoriginate" ]; then
  diffing=n
  if [ -f $resultFile ]; then
    echo "$comid[Error]:    resultFile $resultFile already exists."
    exit 1
  fi
elif [ "x$5" != "x" ]; then
  echo "$comid[Error]:    Peculiar fifth argument, \"$5\""
  exit 1
else
  diffing=y
  if [ ! -f $resultFile ]; then
    echo "$comid[Error]:    No such resultFile, $resultFile"
    exit 1
  fi
fi


export SCRIPTDIR

if [ ! -x $GEMSTONE/install/snefru ]; then
  echo "$comid[Error]:    No executable snefru; call GemStone Technical Support."
  exit 1
fi

if [ "$diffing" = "n" ]; then
  outFile=$resultFile
  echo "$comid[Info]:     Creating checksum file $resultFile"
fi

foundErr=n
rm -f $outFile $tmpFile
touch $outFile
status=$?
if [ $status -ne 0 ]; then
  echo "$comid[Error]:    Unable to create file $outFile"
  exit 1
fi

echo "$comid[Info]:     Making checksums from ${manifestFile}..."
echo "                    This will take a while; please be patient..."
rm -f /tmp/*-$$

cat $manifestFile | \
while read encodedname junk
do
  # while more file, prot, subdir info
  # Expand buried environment variables...
  filename=`eval echo "$encodedname"`

  if [ "x$filename" = "x" ]; then	# skip empty line
    continue
  fi

  case $filename in
  \!*) 
    continue ;;   # comment

  *)
    if [ ! -f $rootDir/$filename ]; then
      echo "$comid[Error]:    No file $rootDir/${filename}."
      touch $tmpFile
      echo "$encodedname	DOES NOT EXIST" >>$outFile
      continue
    fi

    if [ "$filename" = "$omitName" ]; then
      continue		# can't checksum checksum file, tee hee!
    fi
    echo "$encodedname"

    # do the checksum
    result=`$GEMSTONE/install/snefru $snefruFlags <$rootDir/$filename`
    status=$?
    if [ $status -ne 0 ]; then
      echo "$comid[Error]:    snefru execution error on file $rootDir/$filename"
      touch $tmpFile
      echo "$encodedname	SNEFRU ERROR" >>$outFile
    fi
    echo "$encodedname	$result" >>$outFile
    ;;
  esac
done

if [ -f $tmpFile ]; then
  rm -f $tmpFile
  foundErr=y
  echo "$comid[Error]:    Error detected above; proceeding anyway"
fi

cd $SCRIPTDIR

if [ "$diffing" = "n" ]; then
  echo "$comid[Info]:     Done making checksum file"
  echo "                    $outFile"
  rm -f /tmp/*-$$
  if [ $foundErr = n ]; then
    exit 0
  else
    exit 1
  fi
fi


# Get here, it means we are doing a diff.
echo "$comid[Info]:     Comparing $resultFile with $outFile"
diff $diffFlags $resultFile $outFile
status=$?
rm -f $outFile
if [ $status -eq 0 -a $foundErr = n ]; then
  echo "$comid[Info]:     Congratulations, no errors detected"
  exit 0
fi

echo "$comid[Error]:    Problems detected.  Please reload from tape"
echo "                    or contact GemStone Technical Support."
exit 1
