#! /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/x_start
DBSTOPCMD=/usr/lib/maxdb/programs/bin/x_stop

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

function checkNPTL()
{
    ARCH=`uname -m`
    case "$ARCH" in
        i386|i486|i586|i686)
            DISABLE_NPTL="YES"
            ;;
    esac

    if getconf GNU_LIBPTHREAD_VERSION | grep NPTL > /dev/null 2>&1; then
        HAS_NPTL="YES"
    fi

    if [ "$DISABLE_NPTL" != "YES" -a "$HAS_NPTL" != "YES" ]; then
        echo "MaxDB requires NPTL support but this system appears not to have NPTL support."
        echo "Consider upgrading your kernel to version 2.6 and make sure that"
        echo "LD_ASSUME_KERNEL is not set."
        echo "Exiting."
        exit -1
    fi
    
    if [ "$DISABLE_NPTL" = "YES" -a "$HAS_NPTL" = "YES" ]; then
        export LD_ASSUME_KERNEL=2.4.1
    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)
        checkNPTL

        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 $database > /dev/null &
                else
                    $DBSTARTCMD $database > /dev/null
                fi
            fi
        done
        echo "."
	;;
  stop)
	echo -n "Stopping all $DESC:"
        # 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 $database > /dev/null
            fi
        done
        echo "."
	;;
  restart|force-reload)
        checkNPTL

	echo -n "Stopping all $DESC:"
        # 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 $database > /dev/null
            fi
        done
        echo "."
        echo -n "Starting $DESC:"
        for dbInfo in $DATABASES; do
            getDbInfo $dbInfo
            if [ -n $database ]; then
                $DBSTARTCMD $database > /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
