#!/bin/bash
#
# $Id: logreporter,v 1.1 2002/02/07 18:31:10 syatskevich Exp $
#
#    .
#

RPTDIR="/usr/lib/loganalyzer/reports"

print_help () {
	echo ":"
	echo "        logreporter [report [\"<  >\"]]"
	echo
	echo " :"

	for report in $RPTDIR/* ; do
		if [ -x $report ]; then
			report=`basename $report`
			echo "        $report"
		fi
	done
}

if [ "x$1" = "x" ]; then
	print_help
	exit 0
fi

ID=`id -ru`
if [ "x$ID" != "x0" ]; then
	echo "    root"
	exit 1
fi

if [ ! -f "/var/lock/subsys/postgresql" ]; then
	echo "PostgreSQL-  .  $0 "
	exit 1
fi

if [ ! -r "/etc/sysconfig/loganalyzer" ]; then
	echo "   .  $0 "
	exit 1
fi

# $USER, $PASSWD, $DBASE
. /etc/sysconfig/loganalyzer

report () {
	if [ -x "$RPTDIR/$1" ]; then
		#   
		if [ -r "/etc/sysconfig/loganalyzer.modules" ]; then
			. /etc/sysconfig/loganalyzer.modules
		fi

		#  bash 
		export PSQL="psql -P border=2 -q -U $USER -d $DBASE"
		export PSQLQ="psql -q -A -t -U $USER -d $DBASE"

		#   
		export USER PASSWD DBASE

		export FILTER="$2"

		$RPTDIR/$1
	else
		echo ">>>>>>>  logreporter' >>>>>>>"
		echo " $1      "
		echo ">>>>>>>>>>>>> logreporter >>>>>>>>>>>>>"
		echo
		print_help
	fi
}

case "$1" in
	*)
		report "$1" "$2"
		;;
esac
