#! /bin/sh
#
# maxdb-server: startup file for MaxDB databases
#
# ATTENTION: the 'stop' action stops *all* running databases not only those
#            given in etc/default/maxdb-server; this is necessary for this
#            script to make sense during shutdown
#
# (c) 2004 by Martin Kittel.
# Based on the example file written by
# Miquel van Smoorenburg <miquels@cistron.nl> and
# Ian Murdock <imurdock@gnu.ai.mit.edu>.
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin

DBMCLI=/usr/bin/dbmcli
DBSTOPCMD=/usr/lib/maxdb/programs/bin/dbmstop

NAME=maxdb-server
DESC="MaxDB databases"

# if dbmcli is not installed then we cannot start anything
test -x $DBMCLI || exit 0

# currently MaxDB's own task handling can't work with NPTL
if [ "`uname -r | cut -b-3`" = "2.6" ]; then
    export LD_ASSUME_KERNEL=2.2.5
fi


function getDbInfo()
{
    database=`cut -d: -f1 <<EOF
$1
EOF`
    db_user=`cut -d: -f2 <<EOF
$1
EOF`
}


# Include maxdb defaults if available
if [ -f /etc/default/maxdb-server ] ; then
	. /etc/default/maxdb-server
fi

set -e

case "$1" in
  start)
        echo -n "Starting $DESC:"
        for dbInfo in $DATABASES; do
            getDbInfo $dbInfo
            if [ -n $database ]; then
                # FIXME: administrator password should not have to be passed
                # on command line
                echo -n " $database"; $DBMCLI -d $database -u $db_user db_online > /dev/null
            fi
        done
        echo "."
	;;
  stop)
	echo -n "Stopping $DESC:"
        if [ -x $DBSTOPCMD ]; then
            # stop all running databases
            for database in `dbmcli db_enum | awk '/running/ { print $1 }' | sort | uniq`; do
                if [ -n $database ]; then
                    echo -n " $database"; $DBSTOPCMD -d $database > /dev/null
                fi
            done
        else
            # we can only stop those databases we have the passwords for
            for dbInfo in $DATABASES; do
                getDbInfo $dbInfo
                if [ -n $database ]; then
                # FIXME: administrator password should not have to be passed
                # on command line
                    echo -n " $database"; $DBMCLI -d $database -u $db_user db_offline > /dev/null
                fi
            done
        fi
        echo "."
	;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
  #;;
  restart|force-reload)
	echo -n "Stopping $DESC:"
        if [ -x $DBSTOPCMD ]; then
            # stop all running databases
            for database in `dbmcli db_enum | awk '/running/ { print $1 }' | sort | uniq`; do
                if [ -n $database ]; then
                    echo -n " $database"; $DBSTOPCMD -d $database > /dev/null
                fi
            done
        else
            # we can only stop those databases we have the passwords for
            for dbInfo in $DATABASES; do
                getDbInfo $dbInfo
                if [ -n $database ]; then
                # FIXME: administrator password should not have to be passed
                # on command line
                    echo -n " $database"; $DBMCLI -d $database -u $db_user db_offline > /dev/null
                fi
            done
        fi
        echo "."
        echo -n "Starting $DESC:"
        for dbInfo in $DATABASES; do
            getDbInfo $dbInfo
            if [ -n $database ]; then
                # FIXME: administrator password should not have to be passed
                # on command line
                echo -n " $database"; $DBMCLI -d $database -u $db_user db_online > /dev/null
            fi
        done
        echo "."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
