#! /bin/sh
#
# Copyright (C) 2004, 2005, 2006 Mekensleep
#
# Mekensleep
# 24 rue vieille du temple
# 75004 Paris
#       licensing@mekensleep.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Authors:
#  Loic Dachary <loic@gnu.org>
#
# http://wiki.debian.org/LSBInitScripts
#
### BEGIN INIT INFO
# Provides:          poker-network
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# 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

lockfile=/etc/poker-network/lockfile

serverpidfile=/var/run/poker-network-server.pid
serverlogfile=/var/log/poker-network-server.log
serverscript=/usr/lib/python/site-packages/pokernetwork/pokerserver.py
if ! test -f $serverscript ; then
    serverscript=/usr/share/pycentral/python-poker-network/site-packages/pokernetwork/pokerserver.py
fi

botpidfile=/var/run/poker-network-bot.pid
botlogfile=/var/log/poker-network-bot.log
botscript=/usr/lib/python/site-packages/pokernetwork/pokerbot.py
if ! test -f $botscript ; then
    botscript=/usr/share/pycentral/python-poker-network/site-packages/pokernetwork/pokerbot.py
fi

more_args=--no_save

test -x ${twistd} || exit 0
test -r ${serverscript} || 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} --python ${serverscript} \
	    --logfile=${serverlogfile} --quiet ${more_args} \
	    --reactor=${reactor}
        if $RUN_BOTS ; then
	    ${python} ${twistd} \
	        --pidfile=${botpidfile} --python ${botscript} \
	        --logfile=${botlogfile} --quiet ${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} --quiet ${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
