#! /bin/ksh

# 
# Find the man directory in this packaging directory
#      do not want to find the "root/.../man" or "reloc/../man" inside a built package
#

function usage {
   print "usage: $SCRIPT [start dir]"
   exit 1
}


SCRIPT=$0

if [[ $# -ge 2 ]]
then
   print "Too many arguments; only configured to work on 1 directory at a time."
   usage
fi

if [[ $# -eq 1 ]]
then
   START_DIR=$1
   if [[ ! -d $START_DIR ]]
   then
      print "Not an existing directory: $START_DIR"
      usage
   fi
else
   START_DIR=$PWD
fi

HOME=$PWD
cd $START_DIR

for MANDIR in `find . -type d -name 'man' | grep -v root | grep -v reloc`  ; do

echo MANDIR = $MANDIR
   cd $MANDIR
   pwd
   for SECTION in `ls -d man*`; do
      CATSEC=cat${SECTION#man}
      echo " - working on section $SECTION and $CATSEC"
      for FILE in `ls $SECTION`; do
         #
         # Assuming that only man pages have been deposited here and that
         # the suffix correctly reflects the section.
         #
         # Build a preprocessed nroff file in the corresponding "cat" section
         #
         echo  "     building $CATSEC/$FILE"
         if [[ ! -d $CATSEC ]]
         then
            mkdir $CATSEC
         fi

         /usr/ucb/eqn $SECTION/$FILE | /usr/ucb/tbl | /usr/ucb/nroff -man \
                   > $CATSEC/$FILE
      done
   done

   cd -
done
