#!/bin/bash
# Fotis Georgatos, 03 November 2012, University of Luxembourg
# released under MIT/GPL licenses

TMPDIR=/tmp # Redefine or uncomment this as per your needs

# Select which source to use for information
# BINARY="qtop $*" # uncomment this and use next one for OAR
BINARY="qtop4oar $*"

ANSI2HTML="ansi2html.sh"
CACHEFILE="$TMPDIR/qtop_cache"

# WHERE="$TMPDIR"                            # Modify this to your destination folder, eg. a webserver directory
WHERE="/var/www/`hostname -f`/htdocs/qtop" # modify this according to the scheme relevant in your case
mkdir -p $WHERE

export BANNER='
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HEAD><TITLE>qtop output</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META http-equiv="refresh" content="60">
</HEAD><BODY><H1>qtop</H1><PRE>'

(echo $BANNER ;$BINARY -c OFF -o 18 |sed 's/|}|$/}/g;s/|e|$/e/g' ) >$TMPDIR/qtop_bw.html
(              $BINARY -c ON  -o 18 |sed 's/|}|$/}/g;s/|e|$/e/g' ) | tee $CACHEFILE | $ANSI2HTML --bg=dark >$TMPDIR/qtop.html
mv $TMPDIR/qtop.html $WHERE
mv $TMPDIR/qtop_bw.html $WHERE

# the following commits are not atomic but they are meant to allow to choose easily preferred colorscheme
counter=1
for p in linux tango xterm solarized; do
  (echo '<H1>qtop</H1>' ;cat $CACHEFILE | $ANSI2HTML --bg=dark --palette=$p) > $WHERE/qtop_$p.html
  (echo $BANNER         ;cat $CACHEFILE | $ANSI2HTML --bg=dark --palette=$p) > $WHERE/qtop$counter.html
  counter=`echo $counter+1|bc`
done
