#! /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
DBSTARTCMD=/usr/lib/maxdb/programs/bin/dbmstart
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 -a -x $DBSTARTCMD -a -x $DBSTOPCMD || 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)
        if [ "$BACKGROUND" = "Y" -o "$BACKGROUND" = "YES" ]; then
            echo -n "Starting $DESC (in background):"
        else
            echo -n "Starting $DESC:"
        fi
        for dbInfo in $DATABASES; do
            getDbInfo $dbInfo
            if [ -n $database ]; then
                echo -n " $database"
                if [ "$BACKGROUND" = "Y" -o "$BACKGROUND" = "YES" ]; then
                    $DBSTARTCMD -d $database > /dev/null &
                else
                    $DBSTARTCMD -d $database > /dev/null
                fi
            fi
        done
        echo "."
	;;
  stop)
	echo -n "Stopping all $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 "."
	;;
  restart|force-reload)
	echo -n "Stopping all $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
