#!/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 The DBT Tools Authors
#

usage()
{
	echo "$(basename "$0") is the PostgreSQL database statistics rst report generator"
	echo ""
	echo "Usage:"
	echo "  $(basename "$0") [OPTION]"
	echo ""
	echo "Options:"
	echo "  -d DBNAME   dbname to generate report about"
	echo "  -i PATH     path to input files"
	echo "  -t TITLE    title of the report"
}

image_links()
{
	find "${INDIR}/db" -name '*.png' | while IFS= read -r CHART; do
		CHART="$(basename "$CHART")"
		echo ".. image:: ../${CHART}"
		echo "   :target: ../${CHART}"
		echo "   :width: 100%"
		echo ""
	done
}

while getopts "d:hi:t:" opt; do
	case $opt in
	d)
		DBNAME=$OPTARG
		;;
	i)
		INDIR=$OPTARG
		;;
	h)
		usage
		exit 1
		;;
	t)
		TITLE=$OPTARG
		;;
	\?)
		usage
		exit 1
		;;
	esac
done

if [ "$INDIR" = "" ]; then
	error "Error: input was not specified with -i"
	exit 1
fi

mkdir -p "${INDIR}/db/${DBNAME}"

cat << __EOF__ > "${INDIR}/db/${DBNAME}/index.rst"
================================================================================
$TITLE Database Charts
================================================================================

$(image_links)
__EOF__
