#! /bin/sh
# Copyright (c) 2001-2004 SAP AG Germany.
#
# Author: <maxdb@lists.mysql.com>
#
# init.d/maxdb
#
#   and symbolic its link
#
# /sbin/rcmaxdb
#
# System startup script for the nessus backend nessusd
#
### BEGIN INIT INFO
# Provides: MaxDB 
# Required-Start: $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start MaxDB communication server.
### END INIT INFO

if [ -f /etc/rc.config ]; then
	. /etc/rc.config
fi

# Determine the base and follow a runlevel link name.
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

# Force execution if not called by a runlevel directory.
test $link = $base && START_MaxDB=yes
test "$START_MaxDB" = yes || exit 0


# where to find x_server executable
IND_PROG_DBROOT=""
if [ -f /etc/opt/sdb ]; then
    IND_PROG_DBROOT=`grep '^IndepPrograms=' /etc/opt/sdb | sed 's:IndepPrograms=::g'`
else
	exit 5
fi
X_SERVER=$IND_PROG_DBROOT/bin/x_server
X_PATH=$IND_PROG_DBROOT/bin


MaxDB_BIN=$X_SERVER
test -x $MaxDB_BIN || exit 5

MaxDB_BIN=$X_PATH/dbmcli
test -x $MaxDB_BIN || exit 5

# find program fuser
FUSER=/bin/fuser
test -x $FUSER || FUSER=/sbin/fuser
test -x $FUSER || FUSER=/usr/sbin/fuser


# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#. /etc/rc.status

# First reset status of this service
#rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.


# where to find x_server executable
IND_PROG_DBROOT=""
if [ -f /etc/opt/sdb ]; then
    IND_PROG_DBROOT=`grep '^IndepPrograms=' /etc/opt/sdb | sed 's:IndepPrograms=::g'`
fi
X_SERVER=$IND_PROG_DBROOT/bin/x_server
X_PATH=$IND_PROG_DBROOT/bin


RETVAL=1
case "$1" in
    start)
        echo -n "Starting MaxDB services: "
        if [ ! -z "$X_SERVER" ]; then
	    $X_SERVER start>/dev/null 2>&1

	    # to enable auto start/stop testdb remove following comments

#	    DBMCLI=$X_PATH/dbmcli
#	    if [ ! -x $DBMCLI ]; then
#	           echo "dbmcli not found" >&2
#	           exit 5
#	    fi
#	    _o=`$DBMCLI -d TST76 << __EOD__ 2>&1
#		user_logon dbm,dbm
#		db_online
#__EOD__`
#	    _test=`echo $_o | grep ERR`
#       if [ "$_test" = "" ]; then
#			echo -n " database TST76 started"
#	    else
#           echo "cannot start TST76: $_o" >&2
#	    	exit 7
#	    fi

	    RETVAL=0
 
        fi
        touch /var/lock/subsys/maxdb
        ;;
    stop)
        echo -n "Shutting down MaxDB services: "
        if [ ! -z "$X_SERVER" ]; then
	    
	    # to enable auto start/stop testdb remove following comments
        
#		DBMCLI=$X_PATH/dbmcli
#        if [ ! -x $DBMCLI ]; then
#			echo "dbmcli not found" >&2
#            exit 5
#        fi
#        _o=`$DBMCLI -d TST76 << __EOD__ 2>&1
#		user_logon dbm,dbm
#		db_offline
#__EOD__`
#	    _test=`echo $_o | grep ERR`
#        if [ "$_test" = "" ]; then
#			echo -n "database TST76 stopped"
#        else
#			echo "cannot stop TST76: $_o" >&2
#            exit 1 
#        fi

		$X_SERVER stop > /dev/null  2>&1

	    RETVAL=0
        fi
        rm -f /var/lock/subsys/maxdb
        ;;
    status)
        if [ ! -z "$X_PATH" ]; then
            if [ -x $FUSER ]; then 
	    	_o=`$FUSER $IND_PROG_DBROOT/pgm/vserver`
            	if [ $? -eq 0 ]; then
            	    echo "communication server is running"
					
					# to enable auto start/stop testdb remove following comments
					
#					DBMCLI=$X_PATH/dbmcli
#           		if [ ! -x $DBMCLI ]; then
#            			echo "dbmcli not found" >&2
#            		    exit 5
#            		fi
#            		_o=`$DBMCLI -d TST76 << __EOD__
#						user_logon dbm,dbm
#						db_state
#__EOD__`
#					_test=`echo $_o | grep ERR`
#					if [ "$_test" = "" ]; then
#						_state=`echo $_o | sed s/.*State\ *// | sed s/\ .*//`
#						echo "database TST76 is $_state"
#					else
#						echo "cannot get state of TST76: $_o" >&2
#					fi
					RETVAL=0
				else
                	echo "communication server is not running"
                	RETVAL=0
         		fi
            else
	 			echo "status unkown  - fuser not found" >&2
			fi	
		else
            echo "status unkown - x_server not found" >&2
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: maxdb {start|stop|status|reload|restart}"
        exit 1
        ;;
esac

exit $RETVAL	

