#!/bin/sh
# Pre-process (nroff) help files and store them in the directory .Cat.Help.
# .Cat.Help must exist in the same directory as the .Help directory.
# If you only supply the object_directory, all help files in its .Help
# directory will be nroffed.  You can also provide a list of help files
# after the object_directory and only those help files will be nroffed.

set -f

: ${SHOME?} 

case $# in
    0) echo "Usage: $0 <ALL> object_directory [helpfile1] [helpfile2 ...]"
       exit 1 ;;
    1) DIR=$1 ;;
    2) DIR=$1
       shift
       FILES=$* ;;
esac

if [ X$1 = Xall ] || [ X$1 = XAll ] || [ X$1 = XALL ]
then
   DIRS1="$SHOME/s/.Functions $SHOME/s/.Datasets"
   DIRS2="$SHOME/splus/.Functions $SHOME/splus/.Datasets"
   DIRS3="$SHOME/stat/.Functions $SHOME/stat/.Datasets"
   DIRS="$DIRS1 $DIRS2 $DIRS3"
elif [  X$1 = Xlibs ] || [ X$1 = XLibs ] || [ X$1 = XLIBS ]
then
   DIRS1="$SHOME/library/Defunct/.Data $SHOME/library/Matrix/.Data"
   DIRS2="$SHOME/library/chron/.Data $SHOME/library/demo/.Data"
   DIRS3="$SHOME/library/examples/.Data $SHOME/library/external/.Data"
   DIRS4="$SHOME/library/image/.Data $SHOME/library/maps/.Data"
   DIRS5="$SHOME/library/mathematic/.Data $SHOME/library/progdraw/.Data"
   DIRS6="$SHOME/library/progexam/.Data $SHOME/library/semantics/.Data"
   DIRS7="$SHOME/library/statsci/.Data $SHOME/library/trellis/.Data"
   DIRS="$DIRS1 $DIRS2 $DIRS3 $DIRS4 $DIRS5 $DIRS6 $DIRS7"
else
   DIRS=$DIR
fi

for DIR in $DIRS
do
    FILES="`ls -A $DIR/.Help`"
    if [ ! -d $DIR/.Cat.Help ]
    then
        echo "Making $DIR/.Cat.Help directory."
    mkdir $DIR/.Cat.Help
    fi
    for i in $FILES
    do
        echo processing "$i"
        rm -f $DIR/.Cat.Help/"$i"
        if [ .Help.find.sum != "$i" ]; then        
        $SHOME/cmd/help "$i" $DIR > /dev/null
        fi
    done
done
