#! /bin/sh
 
#    DEC 09/91 - F.Majaess
 
#id  haid    - Used to list "id" header line(s) from a script (or file)
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "haid" script is used to list "id" header line(s) for 
#hd            "procnam" script (or file) in "lpath" subdirectory.
#hd            NOTE: "procnam" is ignored if ("full" = "yes").
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      procnam = script (or file) name 
#pr                (needed only if "full" != "yes").
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr      full   = switch accommodating user's request to process whole set in 
#pr               "lpath" (=no/yes).
#pr      lpath  = path of subdirectory containing scripts (=$CCRNSTD/$SUBPROC).
#pr
#ex  EXAMPLE: 
#ex
#ex    haid aid
#ex    haid haid
#ex    haid  procnam=audit
#ex    haid  full lpath

IFS=' '
export IFS

unset procnam full lpath prmtrl

#  * Obtain the file names and any specified option.

arg_list=$@
for arg in $arg_list
do
  case $arg in
      -*) set $arg ; set_opt=$arg  ;;
     *=*) eval $arg                ;;
    full) full=yes                 ;;
   lpath) lpath="$SUBPROC"         ;;
       *) tmp1=${procnam:+$arg} ;
          procnam=${procnam:=$arg}
  esac
done
 
full=${full:='no'}
lpath=${lpath:="$CCRNSTD"}

#   * The following code deals with checking/setting positional 
#   * parameters ...
#   * Note: script will abort if too many arguments are specified.
 
if [ "$full" != "yes" ] ; then
 
#     * Prompt for a positional parameter value(s) if none was/were
#     * specified...
 
  while [ -z "$procnam" ] ; do
    echo "please enter script (or file) name ? > \\c"
    read procnam
  done
 
  prmtrl="$procnam"
  Teor=
else
  if [ -d "${lpath}/." ] ; then
   cd $lpath
   prmtrl=`echo *`
   if [ "$prmtrl" = 'echo *' ] ; then
    unset prmtrl
   fi
  fi
  Teor=" ; echo '    ' "
fi
 
#   ****   Task of the script...   ****
 
# * cycle through parameter(s) in "prmtrl"...
 
procnam=''
 
for procnam in $prmtrl
 do
 
# * if script file exist, non-empty with read permission use it ...
# * otherwise display an error message.
 
 if [ -n "${procnam}" -a ! -d "${procnam}/." ] ; then
  if [ -f $lpath/$procnam ] ; then
   if [ -s $lpath/$procnam -a -r $lpath/$procnam ] ; then
    # eval "( sed -n -e 's/^#id / /p' $lpath/$procnam $Teor ) | more "
    # eval "( sed -n -e 's/^#id / /p' $lpath/$procnam $Teor ) | pg "
      eval "( sed -n -e 's/^#id / /p' $lpath/$procnam $Teor ) | pg -e "
   fi
  else
    if [ -n "$CCRNSTD" -a "$lpath" = "$CCRNSTD" ] ; then
     if [ -n "${BEDIR}" -a -d "${BEDIR}/." ] ; then
        eval "haid $set_opt lpath=$BEDIR full=$full $procnam "
     else
        if [ -n "${SUBPROC}" -a -d "${SUBPROC}/." ] ; then
         eval "haid $set_opt lpath=$SUBPROC full=$full $procnam "
        fi
     fi
    else
     if [ -n "${BEDIR}" -a "$lpath" = "$BEDIR" -a -n "${SUBPROC}" ] ; then
        eval "haid $set_opt lpath=$SUBPROC full=$full $procnam "
     else
      if [ -n "$procnam" -a ! -d "${procnam}/." ] ; then
        eval "echo $procnam file is not found !"
#       exit 2
      fi
     fi
    fi
  fi
 fi
done

if [ "$full" = "yes" ] ; then
  if [ -n "$CCRNSTD" -a "$lpath" = "$CCRNSTD"  ] ; then
     if [ -n "${BEDIR}" -a -d "${BEDIR}/." ] ; then
      eval "haid $set_opt lpath=$BEDIR full=$full $procnam "
     else
      if [ -n "${SUBPROC}" -a -d "${SUBPROC}/." ] ; then
       eval "haid $set_opt lpath=$SUBPROC full=$full $procnam "
      fi
     fi
  fi
  if [ -n "${BEDIR}" -a "$lpath" = "$BEDIR"  ] ; then
     if [ -n "${SUBPROC}" -a -d "${SUBPROC}/." ] ; then
      eval "haid $set_opt lpath=$SUBPROC full=$full $procnam "
     fi
  fi
fi
