#!/bin/sh
#
# Inventory -- take an inventory of the lslk distribution's MANIFEST
#
# $Id: Inventory,v 1.1 96/06/12 12:40:21 abe Exp $

# Establish echo type -- Berkeley or SYSV.

j=`echo -n ""`
if test "X$j" = "X-n "
then
  EC="\c"
  EO=""
else
  EC=""
  EO="-n"
fi

# Display the introduction and basic explanation.

cat << .CAT_MARK

This configuration step (the Inventory script) takes inventory of
the lslk distribution.  The script runs for a minute or two while
it checks that all the subdirectories, information files, scripts,
header files and source files that should be present really are.

It's not absolutely necessary that you take inventory, but it's a
good idea to do it right after the lslk distribution has been
unpacked.  Once the inventory has been taken, this script creates
the file ./.ckMANIFEST as a signal that the inventory step has been
done.

You can call the Inventory script directly at any time to take
inventory.  You can inhibit the inventory step permanently by
creating the file ./.neverInv, and you can tell the Configure script
to skip the inventory and customization steps with the -n option.
.CAT_MARK

END=0
while test $END = 0
do
  echo ""
  echo $EO "Do you want to take inventory (y/n)? $EC"
  read ANS EXCESS
  if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
  then
    exit 0
  fi
  if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
  then
    END=1
  else
    echo ""
    echo "Please answer y or n."
  fi
done

# The current directory is assumed to be the lslk distribution home.

D=`pwd`

# If .ckMANIFEST exists, the manifest has already been checked.
# See if the caller wants to check it again.

CK=$D/.ckMANIFEST
if test -r $CK
then
  cat << .CAT_MARK

======================================================================

The lslk disribution inventory in MANIFEST has already been checked.
.CAT_MARK

  END=0
  while test $END = 0
  do
    echo ""
    echo $EO "Do you want to check the inventory again (y/n)? $EC"
    read ANS EXCESS
    if test "X$ANS" = "Xn" -o "X$ANS" = "XN"
    then
      exit 0
    else
      if test "X$ANS" = "Xy" -o "X$ANS" = "XY"
      then
	END=1
      else
	echo ""
	echo "Plaease answer y or n."
      fi
    fi
  done
fi
echo ""

# See if manifest exists.  Exit if it does not.

if test ! -r MANIFEST
then
  echo "FATAL: MANIFEST file not found or not readable; Inventory exits."
  echo ""
  exit 1
fi

# Start the inventory.

S=""
echo "Conducting an inventory of the lslk distibution; this will take a while."
echo ""
echo $EO "Examining ${D}:$EC"
ERR=0
OK=1
for i in `cat MANIFEST | sed 's/\*$//'`
do
  if test "X$i" != "X"
  then
    j=`expr $i : '\(.*\)/$'`
    if test $? -eq 0
    then

    # Check a subdirectory reference.

      if test ! -d ${D}/${S}/$j
      then
	if test $OK = 1
	then
	  echo ""
	fi
	echo "    Subdirectory ${S}/$j is missing. ++++"
	ERR=1
	OK=0
      fi
    else
      s=`expr $i : '\(.*\):$'`
      if test $? -eq 0
      then

      # Process a subdirectory change.

	if test $OK -eq 1
	then
	  echo " OK"
	fi
	OK=1
	S=$s
	echo $EO "Examining $S:$EC"
	if test ! -d ${D}/$S
	then
	  echo " ERROR"
	  echo "    Subdirectory $S is missing. ++++"
	  ERR=1
	  OK=0
	fi
      else

	# Process a file reference.

	if test ! -r ${D}/${S}/$i
	then
	  if test $OK -eq 1
	  then
	    echo " ERROR"
	  fi
	  echo "    File ${S}/$i is missing. ++++"
	  ERR=1
	  OK=0
	fi
      fi
    fi
  fi
done
if test $OK -eq 1
then
  echo " OK"
fi
echo ""
if test $ERR -ne 0
then
  echo "+++++++++++++++++++++++++++++++++++++++++++++++"
  echo "+                                             +"
  echo "+  SOME FILES OR DIRECTORIES MAY BE MISSING!  +"
  echo "+                                             +"
  echo "+++++++++++++++++++++++++++++++++++++++++++++++"
else
  echo "This lslk distribution seems to be complete."
fi
echo ""
echo "" >> $CK
exit $ERR
