#!/bin/sh
DATE="03/10/1997"
MYCOMMAND=/usr/local/share/vecfem3/vempfade
#**************************************************************************
#**                                                                     ***
#**        Copyrights University of Karlsruhe, 1996                     ***
#**        Program by L. Grosz                                          ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
if [ $# -lt 1 ]; then
   echo "illegal number of arguments:" >&2
   echo "call:  vemhint [-html] <file_name>" >&2
   exit 1
fi
#---------------------------------
#
#** find the vecfem variables
#
if [ "$MYCOMMAND" = "" ] ; then
  . vempfade
  echo vempfade from `dirname $(which vempfade)`
else
  VEMPATH=`dirname $MYCOMMAND`
  SC=`basename $MYCOMMAND`
  if test -r $VEMPATH/vempfade ; then
     . $VEMPATH/vempfade
  else
    HELP=`which $SC`
    VEMPATH=`dirname $HELP`
    if test -r $VEMPATH/vempfade ; then
      . $VEMPATH/vempfade
    else
      . vempfade
    fi
  fi
fi
#
#---------------------------------
EXT=$$
if [ $# -lt 2 ]
  then
    FILE=$1
    FORM=ROFF
  else
    FILE=$2
    FORM=HTML
fi

if test ! -r "$FILE"
  then
  echo ">> input file "$FILE" was not found !" >&2
  echo ">> abend !" >&2
  exit 1
fi
#
#------------------------------------------------------------------------
#
#**  count the number of error messages:
#
sed 's/ *$//' $FILE | grep '>>VEMCD' | sort -u | sed 's/ //g' > /tmp/error.$EXT
NM=`wc -l < /tmp/error."$EXT"`
#
#------------------------------------------------------------------------
#
#**  create headers:
#
if [ $FORM = ROFF ]
then
 echo ".ds LH vemhint" > /tmp/output.$EXT
 echo ".TL" >> /tmp/output.$EXT
 echo "List of VECFEM error messages in file "$FILE >> /tmp/output.$EXT
 echo ".in 3" >> /tmp/output.$EXT
 echo "by VECFEM analyze protocol, version: $DATE" >> /tmp/output.$EXT
 echo "Copyrights by University of Karlsruhe 1996" >> /tmp/output.$EXT
 if [ $NM -lt 1 ]
 then
   echo "no VECFEM error message was found in "$FILE" !" >> /tmp/output.$EXT 
 fi

else

 echo "<HTML><TITLE>Analysis of Solver Protocol" > /tmp/output.$EXT
 echo "</TITLE><BODY>" >> /tmp/output.$EXT
 echo "by VECFEM analyze protocol, version: $DATE<BR>" >> /tmp/output.$EXT
 echo "Copyrights by University of Karlsruhe 1996<BR>" >> /tmp/output.$EXT
 echo "<H1>List of VECFEM error message</H1>"  >> /tmp/output.$EXT
 echo "Protocol is "$FILE"." >> /tmp/output.$EXT
 if [ $NM -lt 1 ]
 then
   echo "<H3>no VECFEM error message was found !</H3>" >> /tmp/output.$EXT 
 fi

fi
#
#------------------------------------------------------------------------
#
#**  create nroff file:
#
if [ ! $NM -lt 1 ]
then
 for ERRMSG in `cat /tmp/error."$EXT"`
 do
  $VECFEM_AWK 'BEGIN{FS=":"
 	     ACTION=0
 	     FOUND=0
 	     VEMCD="'$ERRMSG'"
 	     NLST=split(VEMCD,ERRLST)
 	     printf".NH\nError Code VEMCD=%s:%s\n.LP\n",ERRLST[2],ERRLST[3]}
 
      /^\.SS/{if ($2 == ERRLST[2] && $3 == ERRLST[3]){
                NVAR=split($0,ERRVAR)
                if (NLST<NVAR){NVAR=NLST}
                FOUND=1
                getline
                while ($1!=".SS VEMCD") {
                  cc=2
                  while (cc<=NVAR){gsub(ERRVAR[cc],ERRLST[cc]);cc++}
                  if (index($0,".IP experts")) {ACTION=1}
                  print $0
                  getline}
                exit }}
	END{if (FOUND==0)  {printf "%s\n","no message was found !"}
            if (ACTION==1) {print ".IP action: 14\nContact the VECFEM programmers.\n"}
     }' $VECFEM_ROOT/man7/vemlst.7  >> /tmp/output.$EXT
 done
 
fi 
#
#------------------------------------------------------------------------
#
#**  
#
if [ $FORM = ROFF ]
then
  sed 's/<A .*">//g
       s/<\/A>//g' /tmp/output.$EXT > /tmp/output2.$EXT
else
  ADD=`echo $VECFEM_ROOT/xvem/help/ | sed 's/\\//\\\\\//g'`
  sed 's/\.NH/<H2>/g
       s/\.LP/<\/H2>/g
       s/\\fI.*\\fR/<U>&<\/U>/g
       s/\\fB.*\\fR/<B>&<\/B>/g
       s/\\fR//g
       s/\\fI//g
       s/\\fB//g
       s/HREF="/HREF="'$ADD'/g
       s/\.IP.*scrip.*/<H4>Description<\/H4>/g
       s/\.IP.*easo.*/<H4>Reason<\/H4>/g
       s/\.IP.*ctio.*/<H4>Action<\/H4>/g' /tmp/output.$EXT > /tmp/output2.$EXT
  echo "</BODY></HTML>" >> /tmp/output2.$EXT
fi
#
#------------------------------------------------------------------------
#
#**  return created files
#
if [ $FORM = ROFF ]
then
  nroff -ms /tmp/output2.$EXT 
else
  cat /tmp/output2.$EXT 
fi
rm /tmp/*.$EXT
