#!/bin/sh
#
# $Id: loganalyzer.init,v 1.3 2002/05/09 14:47:55 syatskevich Exp $
#
# description:     loganalyzer
# chkconfig: 345 86 14
#
#      iptables  postrgesql 
#   .

#   init-.
. /etc/rc.d/init.d/functions

PSQL="psql -q"

INITED="/var/log/loganalyzer/inited"
LOCK="/var/lock/subsys/loganalyzer"

IPT_INIT="/etc/rc.d/init.d/iptables"
IPT_CONF="/etc/sysconfig/iptables"
L_IPTABLES="/etc/sysconfig/loganalyzer.iptables"

LIBDIR="/usr/lib/loganalyzer"

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

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

if [ "x$USE_IPTABLES" = "xyes" -a ! -f "/var/lock/subsys/iptables" ]; then
	echo "iptables  .  $0 "
	exit 1
fi

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

CREATE_USER_QUERY="CREATE USER $USER WITH PASSWORD '$PASSWD' CREATEDB;"
DROP_USER_QUERY="DROP USER $USER;"

CREATEDB_QUERY="CREATE DATABASE $DBASE;"
DROPDB_QUERY="DROP DATABASE $DBASE;"

ADD_PLPGSQL_QUERY="
CREATE FUNCTION plpgsql_call_handler() RETURNS opaque
	AS 'plpgsql.so'
	LANGUAGE 'C';

CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
	HANDLER plpgsql_call_handler
	LANCOMPILER 'PL/pgSQL';"

start () {
	[ -f "$LOCK" ] && return

	if [ ! -f "$INITED" ]; then
		echo "    $DBASE."

		if ! grep "^$USER:" /etc/aliases /etc/passwd > /dev/null 2>&1; then
			echo "$USER: root" >> /etc/aliases
			newaliases
		fi

		#         
		$PSQL -U postgres -d template1 -c "$CREATE_USER_QUERY"
		$PSQL -U $USER    -d template1 -c "$CREATEDB_QUERY"

		#     
		$PSQL -U "$USER" -d "$DBASE" -f "$LIBDIR/tables.sql" > /dev/null
		[ -f "$DBASE_ARCH_DIR/$DBASE.bz2" ] && loganalyzer load "$DBASE_ARCH_DIR/$DBASE.bz2"

		#   (  plPgSQL)
		$PSQL -U postgres -d "$DBASE" -c "$ADD_PLPGSQL_QUERY"
		$PSQL -U "$USER"  -d "$DBASE" -f "$LIBDIR/triggers.sql" > /dev/null
	fi

	if [ "x$USE_IPTABLES" = "xyes" ]; then
		if [ ! -f "$INITED" -o ! -f "$IPT_CONF" -o "$IPT_CONF" -ot "$L_IPTABLES" ]; then
			echo "  iptables."

			$IPT_INIT stop
			. "$L_IPTABLES"
			$IPT_INIT save
			$IPT_INIT start
		fi
	fi

	[ -f "$INITED" ] || touch "$INITED"

	echo -n " loganalyzer: "
	echo ""

	touch "$LOCK"
}

stop () {
	[ -f "$INITED" -a -f "$LOCK" ] || return

	if [ "x$USE_IPTABLES" = "xyes" ]; then
		echo -n "    iptables: "
		$LIBDIR/fetchiptables
		echo ""
	fi

	rm -f "$LOCK"
}

drop () {
	if [ ! -f "$INITED" ]; then
		echo "   ";
		return
	fi

	if [ -f "$LOCK" ]; then
		echo "   ";
		return
	fi

	#  
	if [ -d "$DBASE_ARCH_DIR" ]; then
		/usr/bin/loganalyzer save "$DBASE_ARCH_DIR/$DBASE.bz2"
		chmod 600 "$DBASE_ARCH_DIR/$DBASE.bz2"
	fi

	#    
	$PSQL -U $USER    -d template1 -c "$DROPDB_QUERY"
	$PSQL -U postgres -d template1 -c "$DROP_USER_QUERY"

	if [ "x$USE_IPTABLES" = "xyes" ]; then
		#  iptables    
		$IPT_INIT stop
		$IPT_INIT save
		$IPT_INIT start
	fi

	rm -f "$INITED"
}

status () {
	echo -n " LogAnalyzer: "

	if [ -f "$LOCK" ]; then
		echo -n ""
	else
		echo -n ""
	fi

	if [ "x$USE_IPTABLES" != "x" ]; then
		echo -n ", iptables "
	else
		echo -n ", iptables  "
	fi

	echo
}

case "$1" in
	start)
		start
		;;

	stop|condstop)
		stop
		;;

	restart|reload|condrestart)
		stop
		start
		;;

	status)
		status
		;;

	drop)
		drop
		;;

	*)
		my=`basename $0`
		echo ": $my {start|stop|restart|status|drop}"
		exit 1
esac
