#! /bin/sh
#
# GeneWeb	Start the GeneWeb HTTP server.
#

# Do not change the values below
GENEWEBSHARE=/usr/share/geneweb
GENEWEBDOC=/usr/share/doc/geneweb/doc
GENEWEBDB=/var/lib/geneweb
GENEWEBUSER=geneweb
DAEMON=/usr/bin/gwd
WRAPPER=/usr/lib/geneweb/gwd.wrapper
NAME=geneweb
LOGFILE=/var/log/$NAME.log

# Defaults
# The port which the daemon listens to
PORT=2317
# The default language
LANG=en
# Run Mode : if anything else than "daemon", no daemon will be
# launched automatically
RUN_MODE="Always on"
# Additionnal options
OPTIONS=""

# Reads config file (will override defaults above)
[ -r /etc/default/geneweb ] && . /etc/default/geneweb

# Export variables so that they may be used by the wrapper script
export LANG PORT LOGFILE NAME DAEMON GENEWEBDB GENEWEBDOC GENEWEBSHARE OPTIONS

trap "" 1

test -f $DAEMON || exit 0

# We start (or stop) the daemon only when configured
# for running in daemon mode
if [ "$RUN_MODE" != "Always on" ]; then
    exit 0
fi

start_stop()
{
    case "$1" in
        start)
	    # Deal with log files generated by bogus Debian package
	    # (bug #146748)
	    if [ -f /var/log/.log ]
	    then
	       mv /var/log/.log $LOGFILE
	    fi
            echo -n "Starting GeneWeb server:"
            echo -n " gwd" ; umask 007 ; start-stop-daemon -b --start --quiet \
		--chuid $GENEWEBUSER --exec $WRAPPER
            echo "  done."
            ;;

        stop)
            echo -n "Stopping GeneWeb server: "
            echo -n " gwd" ; start-stop-daemon --stop --quiet --exec $DAEMON -- \
	        -hd$GENEWEBSHARE -dd$GENEWEBDOC -bd$GENEWEBDB -p$PORT \
		-lang$LANG -log$LOGFILE -daemon
            echo "  done."
            ;;

        restart | force-reload)
            start_stop stop
	    sleep 2
            start_stop start
            ;;

        *)
            echo "Usage: /etc/init.d/$NAME {start|stop|restart}"
            exit 1
            ;;
    esac
}

start_stop "$@"

exit 0

