#!/bin/ksh

# Create PitStop Fax Menus (fax.menu.html and all.faxes.html)
# for INTERNAL PitStop

# Author:  Cynthia Roush
# Date:    03/19/96

# Changes:
#   04/22/96  CR   Incorporate search and html faxes, and make it
#                  look more like Jim's format.
#   05/06/96  CR   Change from fax_search.sh to gen_search.sh.
#   06/11/96  CR   Change submit button and remove bottom black line.
#   08/07/96  CR   Change gen_search.sh to gen_search.pl.
#   10/08/96  CR   Some changes to fax.menu page.
#   11/26/96  CR   Pass longname as second param because that is what
#                  show_html_fax.sh needs for fax tool button.  Keep
#                  shortname as first param because this is faster
#                  for show_html_fax.sh to find fax to display.
# ------------------------------------------------------------

. /u/hsim/other/fax.data/scripts/setvars
TMP=${TMPDIR}/pitstop.fax.$$
TGT1=${HTMLDIR}/pitstopi.faxmenu.html
TGT2=${HTMLDIR}/pitstopi.allfaxes.html
TGT3=${HTMLDIR}/pitstopi.allfaxes_fn.html

cd $TMPDIR

# Build PitStop TOP fax menu ###########

date=`date "+%B %d, %Y"`

echo "<html>" > $TMP
echo "<title>AIX Technical Fax Documents</title>" >> $TMP
echo "<img src=/intdev/gifs/faxes.gif>" >> $TMP
echo "<p>$date" >> $TMP
echo "<p><center>" >> $TMP
echo "<img src="/intdev/gifs/black_line.gif" border="0"></center>" >> $TMP
echo "<form method=get action=/int-bin/gen_search.pl>" >> $TMP
echo "<input type=hidden name=dbname value=faxdb>" >> $TMP
echo "<font size="+3">Search Faxes</font><br>" >> $TMP
#echo "<table>" >> $TMP
#echo "<tr><td><input name=query size=40></td><td><input type=submit value=Submit></td>" >> $TMP
#echo "<td><a href=/intdev/faxes/menus/all.faxes.html><img border=0 src=/intdev/gifs/browse.blk.button.gif></a></td></table>" >> $TMP
# Without tables for plain browsers
echo "<p><input name=query size=50><input type=submit value=Submit>" >> $TMP
echo "</form>" >> $TMP
echo "<center>" >> $TMP
echo "<img src="/intdev/gifs/black_line.gif" border="0"></center>" >> $TMP
echo "<p>" >> $TMP
echo "<p><font size=+3>Browse Faxes</font>" >> $TMP
echo "<ul>" >> $TMP
echo "<li> <a href=all.faxes.html>Browse by Title</a>" >> $TMP
echo "<li> <a href=all.faxes_fn.html>Browse by File Name</a>" >> $TMP
echo "</ul><center>" >> $TMP
echo "<img src="/intdev/gifs/black_line.gif" border="0"></center>" >> $TMP
echo "<p>" >> $TMP

echo "<p><font size=+3>Ten Newest Faxes</font>" >> $TMP
echo "<ul>" >> $TMP

SRC=${HTMLDIR}/ten.newest.for.pitstop

while read FAXNAME
do
   # Get title, etc., from db
   grep "^ $FAXNAME" $FAXDB > tmp$$
   TITLE=`cut -c$TTL_COLS tmp$$ | sed "s/ *\(.*[^ ]\) *$/\1/"`
   cut -c$VM_COLS tmp$$ | read VMNAME
 
   # Repace "+" with "plusplus" to avoid misinterpretation on http server.
   # PitStop's psc_fax_show.sh will translate back to "+".
   #FAXNAME=`echo $FAXNAME | sed "s/+/plusplus/g"`
 
   echo "<li> <a href=/int-bin/show_html_fax.sh?$VMNAME+$FAXNAME>$TITLE</a>" >> $TMP

done < $SRC

echo "</ul>" >> $TMP

echo "<p><font size=+3>Top Ten Faxes</font>" >> $TMP
echo "<ul>" >> $TMP

SRC=${HTMLDIR}/top.ten.for.pitstop

while read FAXNAME
do
   # Get title, etc., from db
   grep "^ $FAXNAME" $FAXDB > tmp$$
   TITLE=`cut -c$TTL_COLS tmp$$ | sed "s/ *\(.*[^ ]\) *$/\1/"`
   cut -c$VM_COLS tmp$$ | read VMNAME

   # Repace "+" with "plusplus" to avoid misinterpretation on http server.
   # PitStop's psc_fax_show.sh will translate back to "+".
   #FAXNAME=`echo $FAXNAME | sed "s/+/plusplus/g"`
 
   echo "<li> <a href=/int-bin/show_html_fax.sh?$VMNAME+$FAXNAME>$TITLE</a>" >> $TMP

done < $SRC

echo "</ul>" >> $TMP
echo "</html>" >> $TMP

mv $TMP $TGT1
chmod a+r $TGT1

# Build PitStop all-fax menu ###########

# remove comments, links
sed "/^#/d" $FAXDB > tmp$$
sed "/<linked/d" tmp$$ > tmp1$$
#sed "/! limited/d" tmp1$$ > tmp$$
mv tmp1$$ tmp$$
sed "/! notVM/d" tmp$$ > tmp1$$
mv tmp1$$ tmp$$

# sort by title
sort -t ! -o tmp1$$ -k $TTL_FIELD tmp$$ 
mv tmp1$$ src.dt.$$

while read LINE
do

  # Get variables from LINE, which was read from group.names
  COMPGRP=`echo $LINE | sed "s/^\(...\).*/\1/"`
  TITLE=`echo $LINE | sed "s/^... *\(.*\)$/\1/"`

  # Write title to contents list
  echo "<li><a href=\"#$TITLE\">$TITLE</a>" >> contents$$

  # Separate component groups
  grep "! $COMPGRP !" src.dt.$$ > group$$

  # Separate file name and doc title, revision date and 4FAX#
  cut -c$FN_COLS group$$ > fn$$
  cut -c$VM_COLS group$$ > vm$$
  cut -c$TTL_COLS group$$ > ttl$$
  #cut -c$NUM_COLS group$$ > num$$

  # Remove blanks around file names 
  /u/hsim/execs/endblanks fn$$
  /u/hsim/execs/endblanks vm$$
  sed "s/^ //" fn$$ > tmp$$
  mv tmp$$ fn$$
  sed "s/^ //" vm$$ > tmp$$
  mv tmp$$ vm$$

  # copy title column because need it twice
  cp ttl$$ ttl2$$

  # add html syntax
  sed "s/.*/<li> <a href=\/int-bin\/show_html_fax.sh\?&/" vm$$ > tmp$$
  # Replace "+" with "plusplus".  Will be translated back by psc_fax_show.sh.
  #sed "s/+/plusplus/g" tmp$$ > fn$$
  mv tmp$$ vm$$
  sed "s/.*/+&>/" fn$$ > tmp$$
  mv tmp$$ fn$$

  #mv ttl$$ tmp$$
  #sed "s/ *$/+/" tmp$$ | sed "s/^ */+/" | sed "s/ /!/g" > ttl$$

  # remove blanks around 4fax number and add "+all"
  #sed "s/ *$/+all>/" num$$ > tmp$$
  #sed "s/^ *//" tmp$$ > num$$

  sed "s/ *\(.*[^ ]\) *$/\1<\/a>/" ttl2$$ > tmp$$
  mv tmp$$ ttl2$$

  # put file names and doc titles and dates together
  paste -d "\0" vm$$ fn$$ ttl2$$ > all$$

  # add rest of link syntax

  echo "<p><font size=+3> <a name=\"$TITLE\">$TITLE</a></h3>" >> final.dt.$$
  echo "<ul>" >> final.dt.$$
  cat all$$ >> final.dt.$$
  echo "</ul>" >> final.dt.$$
  
done < $DBDIR/group.names


# Add header and footer information
/u/hsim/execs/endblanks final.dt.$$
echo "<title>All Faxes By Category</title>" > $TMP
echo "<img src=/intdev/gifs/faxes.gif>" >> $TMP
echo "<p>$date" >> $TMP
echo "<p><center>" >> $TMP
echo "<img src="/intdev/gifs/black_line.gif" border="0"></center>" >> $TMP
echo "<p>" >> $TMP
echo "<p><font size=+3>Contents</font>" >> $TMP
echo "<ul>" >> $TMP
cat contents$$ >> $TMP
echo "</ul>" >> $TMP
echo "<hr>" >> $TMP
cat final.dt.$$ >> $TMP
echo "</html>" >> $TMP

mv $TMP $TGT2
rm -f *$$
chmod a+r $TGT2


# Build PitStop all-fax menu (browse by file name) ###########

# remove comments, links
sed "/^#/d" $FAXDB > tmp$$
sed "/<linked/d" tmp$$ > tmp1$$
#sed "/! limited/d" tmp1$$ > tmp$$
mv tmp1$$ tmp$$
sed "/! notVM/d" tmp$$ > tmp1$$
mv tmp1$$ tmp$$

# sort by file name
sort -t ! -o tmp1$$ -k $FN_FIELD tmp$$ 
mv tmp1$$ src.dt.$$

while read LINE
do

  # Get variables from LINE, which was read from group.names
  COMPGRP=`echo $LINE | sed "s/^\(...\).*/\1/"`
  # This is TITLE of group, not fax
  TITLE=`echo $LINE | sed "s/^... *\(.*\)$/\1/"`

  # Write title to contents list (list of groups)
  echo "<li><a href=\"#$TITLE\">$TITLE</a>" >> contents$$

  # Separate component groups
  grep "! $COMPGRP !" src.dt.$$ > group$$

  # Separate file name and doc title, revision date and 4FAX#
  cut -c$FN_COLS group$$ > fn$$
  cut -c$VM_COLS group$$ > vm$$
  #cut -c$TTL_COLS group$$ > ttl$$
  #cut -c$NUM_COLS group$$ > num$$

  # Remove blanks around file names 
  /u/hsim/execs/endblanks fn$$
  /u/hsim/execs/endblanks vm$$
  sed "s/^ //" fn$$ > tmp$$
  mv tmp$$ fn$$
  sed "s/^ //" vm$$ > tmp$$
  mv tmp$$ vm$$

  # copy title column because need it twice
  #  cp ttl$$ ttl2$$
  cp fn$$ fn2$$

  # add html syntax
  sed "s/.*/<li> <a href=\/int-bin\/show_html_fax.sh\?&/" vm$$ > tmp$$
  # Replace "+" with "plusplus".  Will be translated back by psc_fax_show.sh.
  #sed "s/+/plusplus/g" tmp$$ > fn$$
  mv tmp$$ vm$$
  sed "s/.*/+&>/" fn2$$ > tmp$$
  mv tmp$$ fn2$$

  #mv ttl$$ tmp$$
  #sed "s/ *$/+/" tmp$$ | sed "s/^ */+/" | sed "s/ /!/g" > ttl$$

  # remove blanks around 4fax number and add "+all"
  #sed "s/ *$/+all>/" num$$ > tmp$$
  #sed "s/^ *//" tmp$$ > num$$

  sed "s/ *\(.*[^ ]\) *$/\1<\/a>/" fn$$ > tmp$$
  mv tmp$$ fn$$

  # put file names and doc titles and dates together
  paste -d "\0" vm$$ fn2$$ fn$$ > all$$

  # add rest of link syntax

  echo "<p><font size=+3> <a name=\"$TITLE\">$TITLE</a></h3>" >> final.dt.$$
  echo "<ul>" >> final.dt.$$
  cat all$$ >> final.dt.$$
  echo "</ul>" >> final.dt.$$
  
done < $DBDIR/group.names


# Add header and footer information
/u/hsim/execs/endblanks final.dt.$$
echo "<title>All Faxes By Category</title>" > $TMP
echo "<img src=/intdev/gifs/faxes.gif>" >> $TMP
echo "<p>$date" >> $TMP
echo "<p><center>" >> $TMP
echo "<img src="/intdev/gifs/black_line.gif" border="0"></center>" >> $TMP
echo "<p>" >> $TMP
echo "<p><font size=+3>Contents</font>" >> $TMP
echo "<ul>" >> $TMP
cat contents$$ >> $TMP
echo "</ul>" >> $TMP
echo "<hr>" >> $TMP
cat final.dt.$$ >> $TMP
echo "</html>" >> $TMP
###############
mv $TMP $TGT3
rm -f *$$
chmod a+r $TGT3
