#! /bin/sh
#
# Copyright (C) 2004, 2005, 2006 Mekensleep <licensing@mekensleep.com>
#                                24 rue vieille du temple, 75004 Paris
#
# This software's license gives you freedom; you can copy, convey,
# propagate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version of the AGPL.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3".  If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
#  Loic Dachary <loic@gnu.org>
#
# http://wiki.debian.org/LSBInitScripts
#
### BEGIN INIT INFO
# Provides:          python-poker-network
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      mysql
# Should-Stop:       mysql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: poker-network server and bots
# Description:       Multi player online poker server with
#                    bots running according to /etc/poker-network
### END INIT INFO

#
# Run bots that will connect to the poker server as instructed
# in /etc/poker-network/poker.bot.xml
#
RUN_BOTS=false

# Include python-poker-network defaults if available
if [ -f /etc/default/python-poker-network ] ; then
    . /etc/default/python-poker-network
fi

twistd=/usr/bin/twistd
reactor=poll

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
name=poker-network
desc='poker server '

python=/usr/bin/python
pv=$(python -c 'import sys; print sys.version[:3]')

lockfile=/etc/poker-network/lockfile

serverpidfile=/var/run/poker-network-server.pid
serverlogfile=/var/log/poker-network-server.log
serverplugin=pokerserver
#serverconfig=/etc/poker-network/poker.server2.xml

botpidfile=/var/run/poker-network-bot.pid
botlogfile=/var/log/poker-network-bot.log
botscript=/usr/share/pyshared/pokernetwork/pokerbot.py
if ! [ -f $botscript ] ; then
    botscript=/usr/lib/python$pv/site-packages/pokernetwork/pokerbot.py
fi
more_args=--no_save

test -x ${twistd} || exit 0
test -r ${botscript} || exit 0

set -e

case "$1" in
  start)
	echo -n "Starting ${desc}: "
	if [ -f $lockfile ]; then
	    echo " not configured, abort."
	    exit 0;
	fi
	${python} ${twistd} \
	    --pidfile=${serverpidfile} \
	    --logfile=${serverlogfile} ${more_args} \
	    --reactor=${reactor} \
	    ${serverplugin} #--config=${serverconfig}
        if $RUN_BOTS ; then
	    ${python} ${twistd} \
	        --pidfile=${botpidfile} --python ${botscript} \
	        --logfile=${botlogfile} ${more_args} \
	        --reactor=${reactor}
        else
            echo "Bots are disabled, edit /etc/default/python-poker-network to enable them."
        fi
	echo ${name}
	;;
  stop)
	echo -n "Stopping poker server ..."
	start-stop-daemon --stop --quiet --retry forever/-2/3 --pidfile=${serverpidfile} || true
	echo " done."
        if $RUN_BOTS ; then
	    echo -n "Stopping poker bots ..."
	    start-stop-daemon --stop --quiet --retry forever/-2/3 --pidfile=${botpidfile} || true
	    echo "done"
        fi
	;;
  rebot)
        if $RUN_BOTS ; then
	    echo -n "Stopping poker bots ..."
	    start-stop-daemon --stop --quiet --retry forever/-2/3 --pidfile=${botpidfile} || true
	    echo "done"
	    echo -n "Wait 10 minutes ..."
            sleep 600
	    echo "done"
	    echo -n "Starting poker bots ..."
	    ${python} ${twistd} \
	        --pidfile=${botpidfile} --python ${botscript} \
	        --logfile=${botlogfile} ${more_args} \
	        --reactor=${reactor}
	    echo "done"
        else
            echo "Bots are disabled"
        fi
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	$0 stop
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|rebot}" >&2
	exit 1
	;;
esac

exit 0
