#!/bin/sh
#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright (C) 2015      Mark Wong
# Copyright (C) 2015      2ndQuadrant, Ltd.
#

if [ $# -ne 3 ]; then
	echo "usage: `basename $0` <title> <table list file> <chart dir>"
	exit 1
fi

TITLE=$1
TABLELISTFILE=$2
PNGDIR=$3

TABLEMETRICS=`(cd $PNGDIR && ls *.png) 2> /dev/null | sed -e "s/table-stat-.*-//" \
		| sed -e "s/.png$//" | sort | uniq`

# Create HTML page listing a table's stats.

for TABLE in `cat $TABLELISTFILE`; do
	TABLECHARTFILE="$PNGDIR/$TABLE-charts.html"
	cat > $TABLECHARTFILE << __EOF__
<html>
<head>
<title>$TITLE $TABLE Table Charts</title>
</head>
<body>
<h1>$TITLE $TABLE Table Charts</h1>
__EOF__

	for CHART in `(cd $PNGDIR && ls -v table-stat-$TABLE-*.png) 2> /dev/null`; do
		cat >> $TABLECHARTFILE << __EOF__
<img src="`basename $CHART`" width="90%" />
__EOF__
	done

	cat >> $TABLECHARTFILE << __EOF__
</body>
</html>
__EOF__
done

# Create HTML page showing all tables for a metric.

for METRIC in $TABLEMETRICS; do
	METRICCHARTFILE="$PNGDIR/t_$METRIC-charts.html"
	cat > $METRICCHARTFILE << __EOF__
<html>
<head>
<title>$TITLE Table $METRIC Charts</title>
</head>
<body>
<h1>$TITLE Table $METRIC Charts</h1>
__EOF__

	for TABLE in `cat $TABLELISTFILE`; do
		cat >> $METRICCHARTFILE << __EOF__
<img src="table-stat-$TABLE-$METRIC.png" width="90%" />
__EOF__
	done

	cat >> $METRICCHARTFILE << __EOF__
</body>
</html>
__EOF__
done
