#! /bin/csh -f

set usage = "$0 -maxlen MAX -title TITLE < INFILE.idx > OUTFILE.html"

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( ( $#argv >= 2 ) && ("x$1" == "x-maxlen" ) ) then
    set maxlen = "$2"; shift; shift;
  else if ( ( $#argv >= 2 ) && ("x$1" == "x-title" ) ) then
    set title = "$2"; shift; shift;
  else 
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 0 ) then
  echo "usage: ${usage}"; exit 1
endif

cat html-header.html \
  | sed -e 's"@TITLE@"'"${title}"'"g' \
  | sed -e 's/@DATE@/'"`date +'%Y-%m-%d %H:%M:%S'`"'/g'

echo "<small>"
echo "<pre>"

cat \
  | gawk \
      -v maxlen=${maxlen} \
      ' function pr(sec, loc, tr, str, defn) \
          { printf "%-8s %-12s %s %s  %s\n", \
              sec, loc, tr, str, defn; \
          } \
        \
        function rp(ch, n, str) \
          { n-=length(str); \
            while (n > 0) { n--; str = (str ch); } \
            return str; \
          } \
        \
        BEGIN { \
          FS="|"; oloc=""; \
          pr("sec", "location", "T:", \
             ("<b>" rp(" ", maxlen, "text") "</b>"), \
             "object (class) <em>comments</em>"); \
          pr(rp("-", 8), rp("-", 12), "--", rp("-", maxlen), rp("-", 30)); \
        } \
        /./ { \
          loc = ($3 "." $4 "." $5); \
          if ((loc!=oloc)&&(oloc!="")) { printf "\n"; } \
          pr( (loc != oloc ? $2 : ""), \
              (loc != oloc ? loc : ""), \
              ($6 ":"), ("<b>" rp(" ", maxlen, $7) "</b>"), \
              ($10 " (" $9 ") <em>" $11 "</em>")); \
          oloc = loc; \
        } \
      '

echo "</pre>"
echo "</small>"

cat html-trailer.html
