#! /bin/sh
# Copyright (c) 2007 Novell Inc / SUSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Peter Poeml <poeml@suse.de>
#
### BEGIN INIT INFO
# Provides:                     mysql-proxy
# Required-Start:               $local_fs $remote_fs $network
# Should-Start:                 $named $time amavis
# Required-Stop:                $null
# Should-Stop:                  $null
# Default-Start:                3 5
# Default-Stop:                 0 1 2 6
# Short-Description:            MySQL Proxy
# Description:                  Start the MySQL Proxy
### END INIT INFO

if [ -s /etc/sysconfig/mysql-proxy ]; then

        . /etc/sysconfig/mysql-proxy
fi

: ${MYSQL_PROXY_BIN:=/usr/sbin/mysql-proxy}
: ${MYSQL_PROXY_PID:=/var/run/mysql-proxy.pid}

: ${MYSQL_PROXY_LUA_PATH:=/usr/share/mysql-proxy/?.lua}
export LUA_PATH="$MYSQL_PROXY_LUA_PATH"

. /etc/rc.status
rc_reset

case "$1" in
    start)
        echo -n "Starting service MySQL Proxy"
        startproc -p $MYSQL_PROXY_PID $MYSQL_PROXY_BIN --pid-file $MYSQL_PROXY_PID $MYSQL_PROXY_ARGS
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down service MySQL Proxy"
        killproc -p $MYSQL_PROXY_PID -TERM $MYSQL_PROXY_BIN
        rc_status -v
        ;;
    try-restart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        $0 stop
        $0 start
        rc_status
        ;;
    reload|force-reload)
        echo -n "Reload service MySQL Proxy"
        kill -HUP `cat $MYSQL_PROXY_PID* 2>/dev/null` 2> /dev/null || true
        rc_status -v
        ;;
    status)
        echo -n "Checking for service MySQL Proxy: "
        checkproc -p $MYSQL_PROXY_PID $MYSQL_PROXY_BIN
        rc_status -v
        ;;
    probe)
        test /etc/sysconfig/mysql-proxy -nt $MYSQL_PROXY_PID \
        && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
esac
rc_exit

