#!/bin/sh
# @(#) ndx - document filePro indexes - (C)2001 Bob Stockler - 02/07/2001

#=#=#=#=#=#=#=#=#=#=#=#=#  User Definable Parameters  #=#=#=#=#=#=#=#=#=#=#=#=#
# Set NDX_HOME to the filePro File containing "prc.ndx":
NDX_HOME=util
# Set NDX_SCREEN to be any screen in that filePro File (it's never seen):
NDX_SCREEN=0
# Set NDX to the name given to the distributed "prc.ndx" input table:
NDX=ndx
# Set NDX_CLERK to "dclerk" (or "rclerk" if the table has been tokenized):
NDX_CLERK=dclerk
# Assign the print command for documentation printing to NDX_DOC_PRNT_CMD:
# NDX_DOC_PRNT_CMD="lp -s"
# To make it be filePro's current printer destination, set it to "@PD":
# NDX_DOC_PRNT_CMD="@PD"
# To make it the value of NDX_DOC_PRNT_CMD set in the environment,
# or if not set, make it be filePro's current printer destination:
: ${NDX_DOC_PRNT_CMD="@PD"}
#=#=#=#=#=#=#=#=#=#=#=#  End User Definable Parameters  #=#=#=#=#=#=#=#=#=#=#=#

trap '
  [ -t 1 ] && echo -n $CN
  trap 0
  exit
' 0 1 2 3 15

: ${cl:=`tput clear`} ${ho:=`tput home`} ${md:=`tput bold`}
: ${se:=`tput rmso `} ${so:=`tput smso`} ${up:=`tput cuu1`}
: ${CO:=`tput civis`} ${CF:=$CO} ${CN:=`tput cnorm`} ${ce:=`tput el`}

PROG=`basename $0`
Usage="$CF$cl
  $so Syntax: $se$md  $PROG [ -f \"name [name ...]\" ] [ -d pathname ] [ -p ] [ -r ]$se\n\n
  Executed without the$md -f$se flag and $md\"list\"$se argument $so $PROG $se presents a menu
  of three options to document filePro indexes:\n
  $so 1 $se - Interactively - Permits entering filePro file names and viewing
        or writing the documentation to files, or both.\n
  $so 2 $se - One filePro file, non-interactively - Writes to a file the index
        documentation for one filePro file.\n
  $so 3 $se - All or a Subset of All filePro files - Accepts entry of filePro
        file names to be documented.  Names may include shell regular
        expressions ( $md*$se, $md?$se and $md[c1-c2]$se ) for filename generation.  This
        option writes documentation for all indexes in all files named.\n
  A documentation file is named with the name of the filePro file and
  the extension $so .ndx $se, and by default is placed in $so \$PFPROG/fpmerge $se.\n\n
  Press $so ENTER $se to continue:$CN "
case "$NDX_DOC_PRNT_CMD" in
  @[Pp][Dd]) PrInTeR="[ the value held in @PD when in filePro ]" ;;
  *) PrInTeR=$NDX_DOC_PRNT_CMD ;;
esac
Usage1="$CF$ho
  $so Syntax: $se$md  $PROG [ -f \"name [name ...]\" ] [ -d pathname ] [ -p ] [ -r ]$se\n\n$cd
  The destination directory for documentation files will be that held in
  $so \$PFMERGE $se set in the environment or the pathname argument to the $so -d $se
  flag on the command line, instead of the default, if either is present.\n
  The $so -f $se flag documents all indexes in all filePro files in the$md quoted$se,
  list of$md space-separated$se filePro file$md names$se following it.\n
  The $so -p $se command line flag causes the documentation to be printed using
  the command $so $PrInTeR $se.\n
  The $so -r $se command line flag causes the documentation file to be removed
  after it is printed.\n
  $so Menu $se users are prompted, after selecting an option, to accept/change
  the destination directory for documentation files, then whether or not
  to print them (and, if printed, whether or not to remove them).\n\n
  Press $so ENTER $se:$CN "

# Assure PFPROG, PFDATA and PFDIR are set:
: ${pfprog:=$PFPROG} ${pfdata:=$PFDATA} ${pfdir:=$PFDIR}
: ${pfprog:=$FPPROG} ${pfdata:=$FPDATA} ${pfdir:=$FPDIR}
[ -z "$pfprog" -o \( -z "$pfdata" -a -z "$pfdir" \) ] && {
  i=1
  eval `
    while read p
    do case $i in 1) PFprog=$p ;; 11) PFdata=$p ;; 111) PFdir=$p ;; esac
       [ $i = 111 ] && echo PFprog=$PFprog PFdata=$PFdata PFdir=$PFdir
       i=1$i
    done </etc/default/fppath
  `
  : ${pfprog:=$PFprog} ${pfdata:=$PFdata} ${pfdir:=$PFdir}
}
: ${PFPROG=$pfprog} ${PFDATA=$pfdata} ${PFDIR=$pfdir}

[ -f $PFPROG/fp/dclerk ] || {
  echo -n "$CF
  Can't find the filePro Programs.
  Press $so ENTER $se: $CN"
  [ -t 1 ] && read junk
  exit 2
}

[ -r $PFPROG/fp/lib/errmsg ] || {
  echo -n "$CF
  Only users $so root $se and $so filepro $se may run this program/\n
  Press $so ENTER $se: $CN"
  [ -t 1 ] && read junk
  exit 2
}

[ -d $PFDATA$PFDIR/filepro ] || {
  echo -n "$CF
  Can't find the filePro Files.
  Press $so ENTER $se: $CN"
  [ -t 1 ] && read junk
  exit 2
}

[ -z "$PFMERGE" ] && {
  [ -d $PFPROG/fpmerge ] && PFMERGE=$PFPROG/fpmerge || PFMERGE=/tmp
}
export PFPROG PFDATA PFDIR PFMERGE

NDX_DOC_FILES=""
# Test for command line flags
[ $# -gt 0 ] && {
  # Get command line flags:
  while getopts prd:f: flag 2>/dev/null
  do case $flag in
       f) NDX_DOC_FILES=$OPTARG ;;
       d) [ -d $OPTARG -a -w $OPTARG ] && PFMERGE=$OPTARG ;;
       p) NDX_DOC_PRNT=1 ;;
       r) NDX_DOC_RM=1 ;;
      \?) echo -n "$Usage" ; read x ; echo -n "$Usage1" ; read x ; exit 2 ;;
     esac
  done
  shift `expr $OPTIND - 1`
}

whence() {
  # Emulate the KornShell built-in function:
  type=`type $1`
  nf="not found"
  case $type in
    *$nf) echo "" ;;
       *) echo $type | { read junk junk path ; echo $path ;} ;;
  esac
}
: ${AWK:=`whence mawk`}
: ${AWK:=`whence gawk`}
: ${AWK:=`whence nawk`}
: ${AWK:=`whence awk`}
export AWK

Get_fPfiles() {
  # Append "/map" to each filePro filename:
  FILES=`echo "$NDX_DOC_FILES" |
    $AWK '{x=split($0,y);for(i=1;i<=x;i++)printf "%s/map ",y[i]}'`
  # List the files, remove the "/map", and put the names in
  # 32-character, fixed length fields:
  cd $PFDATA$PFDIR/filepro
  fPfiles=`ls $FILES 2>/dev/null |
    $AWK '{fPfile=substr($0,1,length($0)-4);printf "%-32s",fPfile}'`
}

[ -n "$NDX_DOC_FILES" ] && {
  [ -z "$TERM" ] && { TERM=ansi ; export TERM ;}
  Get_fPfiles
  exec $PFPROG/fp/$NDX_CLERK $NDX_HOME -s$NDX_SCREEN -y '' -z $NDX \
                                 -r  "$NDX_DOC_PRNT" \
                                 -rx "$fPfiles" \
                                 -ry "$NDX_DOC_PRNT_CMD" \
                                 -rz "$NDX_DOC_RM" >/dev/null &
  exit 0
}

Get_fPmerge() {
  while :
  do echo -n "$CF$ho\n\n\n$cd
  Press $so ENTER $se for Index Documentation to be in the directory:\n
    $so $PFMERGE $se\n
  with the $Name of the filePro $File and the extension: $so .ndx $se\n
  or enter the pathname of the directory in which to place it:\n
  $so ==> $se $CN$cd"
     read DIR_PATH
     [ -z "$DIR_PATH" ] && break
     [ -d "$DIR_PATH" ] || {
     echo -n "$CF
  $so $DIR_PATH $se is not a directory.\n
  Press $so ENTER $se to continue: $CN"
       read junk
       continue
     }
     [ -w "$DIR_PATH" ] && break || {
       echo -n "$CF
  Can not write to $so $DIR_PATH $se.\n
  Press $so ENTER $se to continue: $CN"
       read junk
       continue
     }
  done
  [ -n "$DIR_PATH" ] && PFMERGE=$DIR_PATH
  [ "$NDX_DOC_PRNT" = 1 ] && {
    query="
  Print the documentation ?  ( $so y $se or n ): $CN"
  } || {
    query="
  Print the documentation ?  ( y or $so n $se ): $CN"
  }
  while :
  do echo -n "$CF$ho\n\n\n$cd
  Press $so ENTER $se alone for highlighted default response.
  $query"
     read YN
     [ -z "$YN" ] && break
     case $YN in
       [Yy]) NDX_DOC_PRNT=1  ; break ;;
       [Nn]) NDX_DOC_PRNT="" ; break ;;
     esac
  done
  [ "$NDX_DOC_PRNT" = 1 ] && {
    [ "$NDX_DOC_RM" = 1 ] && {
      query="
  Remove doc $File after printing ?  ( $so y $se or n ): $CN"
    } || {
      query="
  Remove doc $File after printing ?  ( y or $so n $se ): $CN"
    }
    while :
    do echo -n "$CF$ho\n\n\n$cd
  Press $so ENTER $se alone for highlighted default response.
  $query"
       read YN
       [ -z "$YN" ] && break
       case $YN in
         [Yy]) NDX_DOC_RM=1  ; break ;;
         [Nn]) NDX_DOC_RM="" ; break ;;
       esac
    done
  }
}

H=$CF$cl
while :
do echo -n "$H
                           $so Document filePro Indexes $se    (C)2001 Bob Stockler\n\n
                  $so 1 $se - Interactively\n
                  $so 2 $se - One filePro File, non-interactively\n
                  $so 3 $se - All or a Subset of All filePro Files\n
                  $so Q $se - Quit\n\n
                  Your choice: $CN$ce"
   read choice
   case $choice in
     [1-3QqXx] ) break ;;
     *) H=$CF$ho ;;
   esac
done

case $choice in
  1) File=files ; Name=names ; Get_fPmerge
     exec $PFPROG/fp/$NDX_CLERK $NDX_HOME -s$NDX_SCREEN -y '' -z $NDX -d \
                                    -r  "$NDX_DOC_PRNT" \
                                    -ry "$NDX_DOC_PRNT_CMD" \
                                    -rz "$NDX_DOC_RM" \
                                    -h "\r Document filePro Indexes \r"
     ;;
  2) H=$CF$ho
     File=file ; Name=name ; Get_fPmerge
     while :
     do echo -n "$H\n\n\n$cd
  Enter the filePro file name: $CN$cd"
        read PFNAME
        [ -z "$PFNAME" ] && continue
        [ -f $PFDATA$PFDIR/filepro/$PFNAME/map ]  && break || {
          echo -n "$CF
  $so $PFNAME $se is NOT a filePro file.\n
  Press $so ENTER $se to continue: $CN"
          read junk
        }
     done
     exec $PFPROG/fp/$NDX_CLERK $NDX_HOME -s$NDX_SCREEN -y '' -z $NDX \
                                    -r  "$NDX_DOC_PRNT" \
                                    -rw "$PFNAME" \
                                    -ry "$NDX_DOC_PRNT_CMD" \
                                    -rz "$NDX_DOC_RM" >/dev/null &
     ;;
  3) H=$CF$ho
     File=files ; Name=names ; Get_fPmerge
     echo -n "$CF$H\n\n\n$cd
  Enter the $so names $se of filePro files, one name per line.\n
  Shell regular expressions ( $md*$se, $md?$se and $md[c1-c2]$se ) may be used
  for filename generation.  For example:\n
    $md*$se       matches ALL filePro files
    $md[A-Z]*$se  matches all names beginning with an upper case letter
    ${md}fp*$se     matches all names beginning with the letters \"fp\"\n
  Do not enclose names within quotation marks.\n
  Press $so ENTER $se alone when done:\n\n  $so ==> $se $CN"
     while :
     do read file ; [ -z "$file" ] && break
        case $file in
          *\**|*\[*|*\?*) NDX_DOC_FILES="$NDX_DOC_FILES $file" ;;
          *) [ -f $PFDATA$PFDIR/filepro/$file/map ] && \
               NDX_DOC_FILES="$NDX_DOC_FILES $file" ;;
        esac
        [ -n "$NDX_DOC_FILES" ] && SO=$so
        echo -n "$CF\n  $SO$NDX_DOC_FILES $se$up$up$up\n  $so ==> $se $CN$ce"
     done
     [ -z "$NDX_DOC_FILES" ] && {
       echo -n "$CF
  $so NO $se files were named.\n
  Press $so ENTER $se: $CN"
       read junk ; exit 0
     }
     Get_fPfiles
     exec $PFPROG/fp/$NDX_CLERK $NDX_HOME -s$NDX_SCREEN -y '' -z $NDX \
                                    -r  "$NDX_DOC_PRNT" \
                                    -rx "$fPfiles" \
                                    -ry "$NDX_DOC_PRNT_CMD" \
                                    -rz "$NDX_DOC_RM" >/dev/null &
     ;;
  [QqXx]) exit 0
     ;;
esac

exit 0

# EOF 'ndx' - Bob Stockler - Wed Feb  7 20:20:20 EST 2001
