#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

#Make sure to set a timeout.
timeout='--timeout 10 --retries 3'

#First obtain the directory to write the output.
html_dir=`enstore conf $timeout --show crons html_dir`
if [ ! -d "$html_dir" ]; then
    echo HTML directory $html_dir not found.
    exit 1
fi
#Then obtain the directory to find the enstore farmlets file.
farmlets_dir=`enstore conf $timeout --show crons farmlets_dir`
if [ ! -d "$farmlets_dir" ]; then
    echo Farmlets directory $farmlets_dir not found.
    exit 1
fi
gangfile=$farmlets_dir/enstore

if [ ! -f "$gangfile" ]; then
   echo "Farmlet $gangfile not found." >&2
   exit 1
elif [ ! -r "$gangfile" ]; then
   echo "Farmlet $gangfile not readable." >&2
   exit 1
fi

output=$html_dir/nodeinfo.html
#output=${1:-/dev/tty}
#if [ `echo $output | grep -c /dev/` -eq 0 ] ; then
#  rm -f $output
#fi
if [ -f "$output" ]; then
   rm -f $output
fi

black=#000000
white=#FFFFFF

red=#FF0000
blue=#0000FF
green=#00FF00

magenta=#FF00FF
cyan=#00FFFF
yellow=#FFFF00

silver=#C0C0C0
coral=#FF7F50
teal=#008080
turquoise=#40E0D0
navy=#000080

bgcolor=$white
text=$navy

color=$black

cat << EOF >>$output
<html> <head> <title>Enstore Node Uptime and Status Page</title> </head>
<body>
<meta http-equiv="Refresh" content="600">
<body bgcolor="$bgcolor" text="$text">
<h1><center>Enstore Node Uptime and Status Page</center></h1>
<hr>
<h1><center>Begin: `date`</center></h1>
<p>
<table bgcolor="$silver" nosave >
<tr>
<td align=center><b><font color="$color">Trying<br>Node</font></b></td>
<td align=center><b><font color="$color">Nodename</font></b></td>
<td align=center><b><font color="$color">In<br>Production?</font></b></td>
<td align=center><b><font color="$color">Bogomips</font></b></td>
<td align=center><b><font color="$color">Memory (MB)<br>Free/Total</font></b></td>
<td align=center><b><font color="$color">Swap (MB)<br>Free/Total</font></b></td>
<td align=center><b><font color="$color">Time<br>of Day</font></b></td>
<td align=center><b><font color="$color">Uptime</font></b></td>
<td align=center><b><font color="$color">Users</font></b></td>
<td align=center><b><font color="$color">Load<br>Average</font></b></td>
<td align=center><b><font color="$color">Kernel<br>Release</font></b></td>
<td align=center><b><font color="$color">Kernel Version</font></b></td>
</tr>
EOF

for i in `cat $gangfile`; do
  echo "<tr><td align=center><font color=\"$color\">$i</font></td>" >>$output
  # I know the next line uses current nodes $ENSTORE_DIR.  All are same, doesn't matter.
  enrsh $i $ENSTORE_DIR/sbin/Nodeinfo >> $output
  echo "</tr>">>$output
done

cat << EOF >>$output
</table>
<hr>
<h1><center>Done: `date`</center></h1>
<hr>
</body></html>
EOF
