#!/bin/bash
#
# mysql-proxy	shell script takes care of starting and stopping
#		the MySQL-proxy subsystem (mysql-proxy).
#
# chkconfig: - 64 36
# description:	MySQL-Proxy database server.
# processname: mysql-proxy
# config: /etc/mysql-proxy.cnf
# pidfile: /var/run/mysql-proxy.pid

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

# Source networking configuration.
. /etc/sysconfig/network


prog="mysql-proxy"

configfile="/etc/mysql-proxy.cnf"
errlogfile="/var/log/mysql-proxy.log"
pidfile="/var/run/mysql-proxy.pid"

start(){
    touch "$errlogfile"
    echo -n $"Starting $prog: "
    /usr/bin/mysql-proxy \
        --defaults-file="$configfile" \
        --daemon \
        --pid-file="$pidfile" \
        --log-file="$errlogfile" >/dev/null 2>&1
    ret=$?
    if [ $ret -eq 0 ]; then 
        touch /var/lock/subsys/mysql-proxy
        success
    else
        failure
    fi
    echo
    return $ret
}

stop(){
    MYSQLPID=`cat "$pidfile"  2>/dev/null `
    if [ -n "$MYSQLPID" ]; then
        /bin/kill "$MYSQLPID" >/dev/null 2>&1
        ret=$?
        if [ $ret -eq 0 ]; then
            rm -f /var/lock/subsys/mysql-proxy
            action $"Stopping $prog: " /bin/true
        else
            action $"Stopping $prog: " /bin/false
        fi
    else
        action $"Stopping $prog: " /bin/false
    fi
    return $ret
}
 
restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/mysql-proxy ] && restart || :
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status mysql-proxy
        ;;
    restart)
        restart
        ;;
    condrestart)
        condrestart
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|condrestart|restart}"
    exit 1
esac

exit $?
