#!/bin/sh
#
# maxscale:   The MariaDB Corporation MaxScale database proxy
#
# description:  MaxScale provides database specific proxy functionality
#
# processname: maxscale
#
### BEGIN INIT INFO
# Provides: maxscale
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: The maxscale database proxy
# Description: MaxScale is a database proxy server that can be used to front end
# database clusters offering different routing, filtering and protocol choices
### END INIT INFO

#############################################
# MaxScale HOME, PIDFILE, LIB
#############################################

export MAXSCALE_HOME=/usr/local/skysql/maxscale
export MAXSCALE_PIDFILE=$MAXSCALE_HOME/log/maxscale.pid
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MAXSCALE_HOME/lib

###############################
# LSB Exit codes (non-Status)
###############################
_RETVAL_GENERIC=1
_RETVAL_NOT_INSTALLED=5
_RETVAL_NOT_RUNNING=7

###############################
# LSB Status action Exit codes
###############################
_RETVAL_STATUS_OK=0
_RETVAL_STATUS_NOT_RUNNING=3

# Sanity checks.
[ -x $MAXSCALE_HOME/bin/maxscale ] || exit $_RETVAL_NOT_INSTALLED

# Source function library.
. /etc/rc.d/init.d/functions

# we can rearrange this easily
processname=maxscale
servicename=maxscale

RETVAL=0

start() {
    echo -n $"Starting MaxScale: "
    my_check=`status -p $MAXSCALE_PIDFILE $MAXSCALE_HOME/bin/maxscale`
    CHECK_RET=$?
    [ $CHECK_RET -eq 0 ] && echo -n " found $my_check" && success && CHECK_RET=0

    daemon --pidfile $MAXSCALE_PIDFILE $MAXSCALE_HOME/bin/maxscale >& /dev/null

    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename

    if [ $CHECK_RET -ne 0 ]; then
	sleep 2
        my_check=`status -p $MAXSCALE_PIDFILE $MAXSCALE_HOME/bin/maxscale`
        CHECK_RET=$?
        [ $CHECK_RET -eq 0 ] && echo -n $my_check && success || failure
    fi

    # Return rigth code
    if [ $RETVAL -ne 0 ]; then
       failure
       RETVAL=$_RETVAL_NOT_RUNNING
    fi

    echo

    return $RETVAL
}

stop() {
    echo -n $"Stopping MaxScale: "
    killproc -p $MAXSCALE_PIDFILE -TERM

    RETVAL=$?

    echo

    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$servicename

    # Return rigth code
    if [ $RETVAL -ne 0 ]; then
       RETVAL=$_RETVAL_NOT_RUNNING
    fi

    return $RETVAL
}

reload() {
    echo -n $"Reloading MaxScale: "

    killproc -p $MAXSCALE_PIDFILE $MAXSCALE_HOME/bin/maxscale -HUP
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
	start
        ;;
    stop)
        stop
        ;;
    status)
	# return 0 on success
	# return 3 on any error

    	echo -n $"Checking MaxScale status: "
        status -p $MAXSCALE_PIDFILE 'MaxScale'
        RETVAL=$?

	if [ $RETVAL -ne 0 ]; then
		echo -ne "\033[1A"
		[ $RETVAL -eq 1 ] && warning || failure
		echo -ne "\033[1B"

		RETVAL=$_RETVAL_STATUS_NOT_RUNNING
	else
		echo -ne "\033[1A"
		success
		echo -ne "\033[1B"
		RETVAL=$_RETVAL_STATUS_OK
	fi

	exit $RETVAL
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$servicename ]; then
            stop
            start
        fi
        ;;
    reload)
	reload
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        ;;
esac
exit $RETVAL
