#!/bin/sh
#
# cyphesis          This shell script takes care of starting and stopping
#                   the cyphesis service.
#
# chkconfig: - 90 10
# description: Cyphesis is a server for WorldForge online games.
# processname: cyphesis

if [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
fi

# Source cyphesis service configuration
if [ -f /etc/sysconfig/cyphesis ] ; then
        . /etc/sysconfig/cyphesis
else
        CYPHESISUSER=cyphesis
fi

CYPHESISPARAMS="--cyphesis:unixport=/var/run/cyphesis/cyphesis.sock --cyphesis:pythonport=/var/run/cyphesis/cypython.sock"

POSTGRESUSER=postgres

start() {
        # Start the daemon.

        # Make sure postgres superuser exists
        if ! runuser $POSTGRESUSER -c true >/dev/null 2>&1; then
            echo
            echo $"Could not check for running PostgreSQL database."
            return 1
        fi

        # Make sure postgres is running
        if ! runuser $POSTGRESUSER -c "psql -c \"\" template1" >/dev/null 2>&1; then
            echo $"PostgreSQL server is not running."
            return 1
        fi

        # Make sure the user we are going to run as exists
        if ! runuser $CYPHESISUSER -c true >/dev/null 2>&1; then
            echo $"Cannot find user $CYPHESISUSER to run cyphesis service."
            return 1
        fi

        # Make sure the user has a postgres account
        if ! runuser $CYPHESISUSER -c "psql -c \"\" template1" >/dev/null 2>&1; then
            echo -n $"Creating PostgreSQL account: "
            runuser $POSTGRESUSER -c "createuser -A -d -q -R $CYPHESISUSER" >/dev/null 2>&1
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                echo_success
            else
                echo_failure
                echo
                return 1
            fi
            echo
        fi

        # Make sure the database exists
        if ! runuser $CYPHESISUSER -c "psql -c \"\" cyphesis" >/dev/null 2>&1; then
            # Create the database
            echo -n $"Creating PostgreSQL database: "
            runuser $CYPHESISUSER -c "createdb -q cyphesis" >/dev/null 2>&1
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                echo_success
            else
                echo_failure
                echo
                return 1
            fi
            echo
            # Populate it with rules
            echo -n $"Loading database with rules: "
            runuser $CYPHESISUSER -c "cyloadrules $CYPHESISPARAMS" >/dev/null 2>&1
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                echo_success
            else
                echo_failure
                echo
                return 1
            fi
            echo
        fi

        echo -n $"Starting cyphesis: "

        # Run the server, in self daemonising mode
        runuser $CYPHESISUSER -c "/usr/bin/cyphesis --cyphesis:daemon=true $CYPHESISPARAMS" >/dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            echo_success
            touch /var/lock/subsys/cyphesis
        else
            echo_failure
            echo
            return $RETVAL
        fi
        echo

        if [ "$POPULATE_WORLD" == "1" ] ; then
            echo -n $"Populating cyphesis world: "

            # Populate the world with game data
            runuser $CYPHESISUSER -c "/usr/bin/cyclient $CYPHESISPARAMS" >/dev/null 2>&1
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                echo_success
            else
                echo_failure
            fi
            echo
        fi

        return $RETVAL
}

stop() {
        # Stop server
        echo -n $"Shutting down cyphesis: "
        killproc cyphesis
        RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ] ; then
            rm -f /var/lock/subsys/cyphesis
        fi

        return $RETVAL
}

case "$1" in
  start)
        start
        RETVAL=$?
        ;;
  stop)
        stop
        RETVAL=$?
        ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
  condrestart)
        if [ -f /var/lock/subsys/cyphesis ]; then
            stop
            start
            RETVAL=$?
        fi
        ;;
  status)
        status cyphesis
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 (start|stop|restart|condrestart|status)"
        exit 1
esac

exit $RETVAL
