#!/bin/sh
#
#     tiger - A UN*X security checking system
#     Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2, or (at your option)
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#     Please see the file `COPYING' for the complete copyright notice.
#
# genmsgidx  - 06/14/93
#
# 09/19/2003 - jfs - Applied fixes contributed by Nicolas François
# 08/13/2003 - jfs - Avoided bashism
# 08/26/2008 - jfs - Fix temporary race condition by using tempfile
#                    in those systems that have it
# 11/27/2008 - jfs - Detect if we cannot write to the file and abort
# 11/22/2015 - jfs - Make LS behave the same independently of the environment
# 04/02/2018 - jfs - Do not assume tempfile exists, check before calling
#
#-----------------------------------------------------------------------------

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

# Setup a tempfile
[ -n "$TMPDIR" ] && TMPDIR=/tmp/
tempfile=""
if [ -n "`which mktemp`" ] ; then
    tempfile=`mktemp`
fi
[ -z "$tempfile" ] && tempfile=$TMPDIR/te.$$
[ ! -e $tempfile ] && >$tempfile

# This test is only needed for some old systems that do not support -x
( [ $TESTEXEC /bin/sh ] ) 2> $tempfile
[ -s $tempfile ] && TESTEXEC=-f
export TESTEXEC

RM=`findcmd rm`
[ -n "$RM" ] && $RM $tempfile
AWK=`findcmd awk`
BASENAME=`findcmd basename`
CHMOD=`findcmd chmod`
LS=`findcmd ls`

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

# Clear idx file and detect error
#> $BASEDIR/doc/explain.idx || {
#    echo "Error: Cannot write over the index file $BASEDIR/doc/explain.idx. Aborting"
#    exit 1
#}

LC_ALL=C.UTF-8 LC_COLLATE=C.UTF-8 $LS $BASEDIR/doc/*.txt |
while read 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
