#! /bin/bash
#******************************************************************************************
set -u

#************************************************************************* global constants
declare -r days="15-09-2005 23-11-2005 26-11-2005"
declare -r imageDir=images \
           varDir=var \
           leftDoc=obs.htm \
           rightDoc=exp.htm \
           frameDoc=index.htm

#****************************************************************************** writeLink()
function writeLink()
{
local -r img=$(basename $1)
local -r form=${2:-contours}
local expNo=${img##*_}; expNo=${expNo%.png}
echo -n  "    <td>"
if [ $form == contours ]; then
  echo "<span id=\"score1\">$expNo</span>(<a href=\"$imageDir/_$img\">pnts</a>)<br>"
  echo -n "      <a href=\"$imageDir/$img\"><img src=\"$imageDir/thumbnails/TN_$img\"></a>"
elif [ $form == points ]; then
    echo -n "<a onclick=\"showImg('$imageDir/$img')\">$expNo</a>" 
  else
    echo -n $img
  fi
echo "</td>"
}

#********************************************************************************* header()
function header()
{
local -r form=${1:-contours}
cat<<EOF
<html>

<head>
  <title>QPF</title>
  <base target="_blank">
  <link rel="styleSheet" type="text/css" href="$varDir/qpf.css">
  <script src="$varDir/qpf.js"></script>
</head>

EOF
if [ $form == contours ]; then echo "<body onLoad=\"initVert()\">"
else echo "<body onLoad=\"initBoth()\">"; fi

echo "<table width=\"100%\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">"
}

#****************************************************************************** dayHeader()
function dayHeader()
{
local -r day=$1 span=${2:-1}
if [ $span -eq 1 ]; then 
  echo "  <tr><th class=\"obs\">$day</th></tr>"
else
  echo "  <tr><th colSpan=\"$span\">$day</th></tr>"
  fi
}

#******************************************************************************* obsCells()
function obsCells()
{
local -r form=${1:-contours}
local prefix=""; [ $form == points ] && prefix=_
local -r obsImg=$(ls ./$imageDir/$prefix${day}_OBS.png)
dayHeader $day 
cat<<EOF
  <tr class="obs">
$(writeLink $obsImg $form)
  </tr>
EOF
}

#******************************************************************************* expCells()
function expCells()
{
local -r day=$1
local -r form=${2:-contours}
local prefix=""; [ $form == points ] && prefix=_
local -r images=$(ls ./$imageDir/$prefix${day}_*.png | sort)
local -r obsImg=$(ls ./$imageDir/$prefix${day}_OBS.png)
local -ri count=$(echo $images | wc -w)
dayHeader $day $(( $count-1 ))
echo "  <tr>"
for img in $images; do
  [ $img == $obsImg ] && continue
  writeLink $img $form
  done
echo "  </tr>"
}

#********************************************************************************* footer()
function footer()
{
local -r form=${1:-contours}
echo "</table>" $'\n'
[ $form == points ] && echo "<img src=\"$varDir/empty.png\">" $'\n'
cat<<EOF
<p>
Contours: <a target="_self" href="$leftDoc">${leftDoc%.*}</a>, 
<a target="_self" href="$rightDoc">${rightDoc%.*}</a><br>
Points: <a target="_self" href="_$leftDoc">${leftDoc%.*}</a>, 
<a target="_self" href="_$rightDoc">${rightDoc%.*}</a>
</p>

</body>
</html>
EOF
}

#****************************************************************************** createDoc()
function createDoc()
{
local -r kind=${1:-obs}
header 
for day in $days; do
  if [ $kind == obs ]; then obsCells; else expCells $day; fi
  done
footer
}

#**************************************************************************** createFrame()
function createFrame()
{
cat<<EOF
<!DOCTYPE HTML PUBLIC "-\\W3C\\DTD HTML 4.0 FRAMESET\\EN">
<html>
<head>
  <title>QPF results</title>
</head>
<frameset cols="300,*">
  <frame src="$leftDoc"  frameBorder="0" scrolling="auto">
  <frame src="$rightDoc" frameBorder="0" scrolling="auto">
</frameset>
</html>
EOF
}

#****************************************************************************** createDoc()
function createDoc()
{
local -r kind=${1:-obs} form=${2:-contours}
header $form
for day in $days; do
  if [ $kind == obs ]; then obsCells $form; else expCells $day $form; fi
  done
footer $form
}

#************************************************************************************* main
createDoc obs contours 1>$leftDoc; echo done $leftDoc
createDoc exp contours 1>$rightDoc; echo done $rightDoc
createFrame 1>$frameDoc; echo done $frameDoc
createDoc obs points 1>_$leftDoc; echo done _$leftDoc
createDoc exp points 1>_$rightDoc; echo done _$rightDoc
