#! /bin/sh
# Copyright (c) 2012 SAP AG, Germany.
#
# Author: d036774
#
# init.d/maxdb
#
# and symbolic its link
#
#
### 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 server.
### END INIT INFO

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

# DB Name
DB_NAME=<%= @dbName %>

# SDB Admin user
SDBA=<%= @dbUser %>

# XUser
XUSER=<%= @xuser %>

# where to find x_server executable
IND_PROG_DBROOT=<%= @dbProgramDir %>

# Check whether x_server and dbmcli exists
X_PATH=<%= @dbProgramDirBin %>

X_SERVER=$X_PATH/x_server
test -x $X_SERVER || exit 5

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

# Source init functions
. /etc/rc.status

# 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.

RETVAL=1
case "$1" in
    start)
        echo "Starting MaxDB services: "

        $X_SERVER -F start 2>&1

        _o=$("${DBMCLI}" -U "${XUSER}" db_online)
         echo $_o

        test=`echo $_o | grep ERR`
        if [ "$_test" = "" ]; then
                echo " database $DB_NAME started"
        else
                echo " cannot start $DB_NAME: $_o" >&2
                exit 7
        fi

        RETVAL=0
        touch /var/lock/subsys/maxdb
    ;;

    stop)
        echo "Shutting down MaxDB services: "

        _o=$("${DBMCLI}" -U "${XUSER}" db_offline)
        _test=`echo $_o | grep ERR`
        if [ "$_test" = "" ]; then
                echo " database $DB_NAME stopped"
        else
                echo " cannot stop $DB_NAME: $_o" >&2
                exit 1
        fi

        $X_SERVER stop 2>&1
        RETVAL=0
        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"
                                 _o=$("${DBMCLI}" -U "${XUSER}" db_state)
                                 _test=$(echo "${_o}" | grep ERR)
                                 if [ "$_test" = "" ]; then
                                         _state=$(echo "${_o}" | sed s/.*State\ *// | sed s/\ .*//)
                                         echo " Database $DB_NAME is $_state"
                                         if grep -q "OFFLINE" <<< $_state; then 
                                                false
                                                rc_status -v
                                         else
                                                true
                                                rc_status -v
                                         fi
                                 else
                                         echo " cannot get state of $DB_NAME: $_o" >&2
                                 fi

                                 RETVAL=0
                        else
                                 echo "communication server is not running"
                                 false
                                 rc_status -v
                                 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
        RETVAL=0  # Nasty hack. Should evaluate stop/start return values
    ;;


    reload)
        $0 stop
        $0 start
        RETVAL=0  # Nasty hack. Should evaluate stop/start return values
    ;;

    *)
        echo "Usage: maxdb {start|stop|status|reload|restart}"
        exit 1
    ;;
esac

exit $RETVAL
