#!/bin/sh
#
#     tiger - A UN*X security checking system
#     Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
#
#     Please see the file `COPYING' for the complete copyright notice.
#
# genmsgidx  - 06/14/93
#
#-----------------------------------------------------------------------------

basedir=${TIGERHOMEDIR:=.}

for parm
do
   case $parm in
   -B) basedir=$2; break;;
   esac
done

BASEDIR=${BASEDIR:=$basedir}
export BASEDIR

findcmd()
{
  CMD=$1

  SRCH=/usr/ucb:/usr/bin:/bin:/etc:/usr/etc
  
  SAVEIFS=$IFS
  IFS=:
  set $SRCH
  IFS=$SAVEIFS
  for dir
  do
    [ $TESTEXEC $dir/$CMD ] && {
      echo $dir/$CMD
      return
    }
  done
}

haveallof()
{
  retval=0
  what=$1
  shift
  for file
  do
    eval cmd=\$$file
    [ ! -n "$cmd" ] && {
      echo "--CONFIG-- Don't have all required $what (missing $file)"
      retval=1
    }
  done
  return $retval
}

TESTEXEC=-x
( [ $TESTEXEC /bin/sh ] ) 2> /tmp/te.$$
[ -s /tmp/te.$$ ] && TESTEXEC=-f
export TESTEXEC

RM=`findcmd rm`
[ -n "$RM" ] && $RM /tmp/te.$$
AWK=`findcmd awk`
BASENAME=`findcmd basename`
CHMOD=`findcmd chmod`

haveallof commands AWK BASENAME || exit 1
haveallof variables BASEDIR || exit 1

for infile
do
  file=`$BASENAME $infile`
  $AWK '
  BEGIN {start=0}
  /^%.*/ {
    if(start)
       printf("%s %s %d %d\n", key, "'$file'", start+1, NR-1);
    start=NR;
    key=substr($0, 2, length($0)-1);
  }
  END { printf("%s %s %d $\n", key, "'$file'", start+1); }
  ' $infile
done > $BASEDIR/doc/explain.idx

status=$?

$CHMOD 644 $BASEDIR/doc/explain.idx

exit $status
