#! /bin/sh
#
# pre- and post-processing for ss, R. Perry
#
# requires cpp (from gcc), and gnuplot for plotting

CPP="cpp -x c -std=c99"

# CPPFLAGS will be set to -DHTML=1 for HTML and Table options
#
CPPFLAGS=""

# location of ss executable:
#
SS="/usr/local/bin/ss"

if [ ! -x "$SS" ]; then
  echo "${0}: $SS not found" 1>&2
  exit 1
fi

usage()
{
  echo "Usage: SS [-H|--HTML|--html] [-T|--Table|--table] [-t|--title title]
  [-Dmacro[=defn]...] [-p|-p2|--plot|--plot2d|-p3|--plot3D] [-x|--xlabel xlabel]
  [-y|--ylabel ylabel] [-z|--zlabel zlabel] [ss options...] [file...]" 1>&2
  $SS -h
}

ss2gif()
{
  labels="set title \"$title\"\nset xlabel \"$xlabel\"\nset ylabel \"$ylabel\"\nset zlabel \"$zlabel\""
  (printf "set term gif\nset output\n$labels\n$plot '-' notitle with linespoints\n";
   awk 'BEGIN { p=0; } { if(p) print; else if( substr($0,1,1) == "\t") p=1; }') |
   gnuplot
}

ss2html()
{
  if [ -z "$table" ]; then
    echo "<!doctype html public \"-//W30//DTD W3 HTML 2.0//EN\">
      <html><head><title>$title</title></head>
      <body bgcolor=white text=black>" # <h3>$title</h3>
  fi

  # uncomment to escape HTML tags:
  # sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' |
  #
  awk '-F	' 'BEGIN { tab=0; headers=1; print "<pre>&nbsp;"; }
    END { print "</table>"; } {
  if( tab)
  {
    if( headers && substr($0,1,1) == "\t") # start new table
    {
      print "<tr>";
      for( i = 1; i <= NF; ++i)
	print "<th align=\"right\">&nbsp;"$i"</th>";
      print "</tr>";
    }
    else
    {
      if( headers) x="<b>"$1"</b>"; else x=$1;
      print "<tr><td align=\"right\">"x"</td>";
      for( i = 2; i <= NF; ++i)
        print "<td align=\"right\">"$i"</td>";
      print "</tr>";
    }
  }
  else
  {
    if( substr($0,1,1) == "\t")
    {
      tab = 1;
      print "</pre>\n<table border=1>\n";
      if( $2 == "") headers=0;
      else
      {
        print "<tr>";
        for( i = 1; i <= NF; ++i)
	  print "<th align=\"right\">&nbsp;"$i"</th>";
        print "</tr>";
      }
    }
    else
    {
      print;
    }
  }
}'

  if [ -z "$table" ]; then
    echo "</body></html>"
  fi
}

options=""
files=""
html="0"
title="SS"
table=""
xlabel="x"
ylabel="y"
zlabel="z"
plot="0"

while true
do
  case "$1" in
    --version) $SS --version; exit 0;;
    -r|--rows) options="$options -r $2"; shift 2;;
    -c|--cols) options="$options -c $2"; shift 2;;
    -h|--help) usage; exit 0;;
    -d|--debug) options="$options -d"; shift;;
    -v|--verbose) options="$options -v"; shift;;
    -H|--HTML|--html) html="1"; CPPFLAGS="$CPPFLAGS -DHTML=1"; shift;;
    -T|--Table|--table) table="1"; html="1"; CPPFLAGS="$CPPFLAGS -DHTML=1"; shift;; # ZZ no html <head> <body>
    -p|-p2|--plot|--plot2D) plot="plot"; shift;;
    -p3|--plot3D) plot="splot"; shift;;
    -t|--title) title="$2"; shift 2;;
    -x|--xlabel) xlabel="$2"; shift 2;;
    -y|--ylabel) ylabel="$2"; shift 2;;
    -z|--zlabel) zlabel="$2"; shift 2;;
    -) files="$files -"; shift;; # stdin
    -D*) CPPFLAGS="$CPPFLAGS $1"; shift;;
    -*) echo "SS: bad option: $1" 1>&2; usage; exit 1;;
    "") break;;
    *) files="$files $1"; shift;; # no whitespace in file names
  esac
done

# echo "options = $options, files = $files"

# if no files then use stdin
#
if [ -z "$files" ]; then
  files="-"
fi

(for f in $files
do
  case "$f" in
    -) echo "# 1 \"<stdin>\""; cat;;
    *) echo "# 1 \"$f\""; cat "$f";;
  esac
done) | $CPP $CPPFLAGS |
case "$html$plot" in
  00) $SS $options;;
  10) $SS $options | ss2html;;
  0plot|0splot) $SS $options | ss2gif;;
  *) echo "SS: bad options" 1>&2; usage; exit 1;;
esac
