#!/bin/sh
#
# poker-bot    poker-network bots
#
# chkconfig:   - 66 34
# description: Multi player online poker server with \
#              bots running according to /etc/poker-network
# processname: poker-bot
# config:      /etc/poker-network/poker.bot.xml
# pidfile:     /var/run/poker-bot.pid

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

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

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

selinuxwrapper=/usr/bin/poker-bot-selinux

python=/usr/bin/python
twistd=/usr/bin/twistd
reactor=poll

opt_args=--no_save

exec="/usr/lib/python2.5/site-packages/pokernetwork/pokerbot.py"
prog=poker-bot

pidfile=/var/run/poker-network/$prog.pid
logfile=/var/log/poker-network/$prog.log

serverpidfile=/var/run/poker-network/poker-server.pid

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

# Possible return values:
# 0 = service is running
# 1 = pid file exists, but process is dead
# 2 = pid file gone, process is gone, but lockfile exists
# 3 = service not running
isrunning() {
    local base=pokerbot
    local pid

    if [ -f ${pidfile} ] ; then
        read pid < ${pidfile}
        ps $pid | grep $base 2>&1 > /dev/null
        running=$?
        if [ "$running" = 0 ] ; then
            return 0
        fi

        if [ -n "$pid" ]; then
            return 1
        fi
    fi
    if [ -f ${lockfile} ]; then
            return 2
    fi

    return 3
}

poker_status() {
    isrunning
    retval=$?
    case $retval in
        0)  echo "$prog is running."
            ;;
        1)  echo "$prog stopped but pid file exists..."
            ;;
        2)  echo "$prog stopped but subsys locked..."
            ;;
        3)  echo "$prog is stopped."
            ;;
    esac
}

start() {
    echo -n $"Starting $prog: "
    isrunning
    retval=$?
    case $retval in
        0)  echo -n $"$prog already running"
            failure
            echo
            return 1
            ;;
        1)  echo -n $"$prog stopped but pid file exists..."
            ;;
        2)  echo -n $"$prog stopped but subsys locked..."
            ;;
    esac

    if [ ! -f ${serverpidfile} ]; then
        echo -n $"poker-server must be running"
        failure
        echo
        return 1
    fi

    daemon --user=poker ${selinuxwrapper} \
            --pidfile=${pidfile} --logfile=${logfile} \
            --quiet ${opt_args} --reactor=${reactor}

    retval=$?
    if [ $retval -eq 0 ]; then
        success
        touch $lockfile
    else
        failure
    fi
    echo

    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

fdr_status() {
    status $prog
}

case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        poker_status
        ;;
    condrestart|try-restart)
        [ ! -f $lockfile ] || restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
        exit 2
esac
