#! /bin/sh -e

#MESSAGE#

. /lib/lsb/init-functions

# Check for daemon presence
test -x /usr/bin/#APPNAME#-server || exit 0
test -f /etc/default/#APPNAME#-testing || exit 0

NAME=#APPNAME#

HOME=/srv/${NAME}-testing
PORT=#TESTINGPORT#

#STARTOPTIONS="${STARTOPTIONS} --quiet"
STARTOPTIONS="${STARTOPTIONS} -d ${HOME}"

STOPOPTIONS="${STOPOPTIONS} --quiet"

OPTIONS="${OPTIONS} --store ${HOME}/${NAME}-state"
#OPTIONS="${OPTIONS} --home ${HOME}"			# Not implemented
OPTIONS="${OPTIONS} --http-port ${PORT}"
OPTIONS="${OPTIONS} --data-dir /usr/share/${NAME}-data"
OPTIONS="${OPTIONS} --log-dir /var/log/happstack.com/"
OPTIONS="${OPTIONS} --log-mode Development"

# Include defaults if available
echo "Loading /etc/default/${NAME}-testing..." 1>&2
. /etc/default/${NAME}-testing

case "$1" in
  start)
    log_begin_msg "Starting ${NAME}-testing..."
    mkdir -p ${HOME}
    ln -s /usr/share/#NAME#-data/theme/* /usr/share/javascript/jquery /usr/share/javascript/jquery-ui /srv/${NAME}-testing/#TESTINGPORT#/ 2>/dev/null || true
    start-stop-daemon -v --start -b --quiet --pidfile /var/run/${NAME}-testing --make-pidfile -d /srv/${NAME}-testing --exec /usr/bin/${NAME}-server -- ${OPTIONS} --http-port #TESTINGPORT# --store /srv/${NAME}-testing/_local/${NAME}_state --static /srv/${NAME}-testing/#TESTINGPORT# --no-validate +RTS -IO
    log_end_msg $?
    ;;
  stop)
    log_begin_msg "Stopping ${NAME}-testing..."
    start-stop-daemon --stop --quiet --pidfile /var/run/${NAME}-testing --oknodo --retry 2 # --exec /usr/bin/${NAME}-server  # with this option enabled stop will fail during an upgrade if the executable has already been removed
    log_end_msg $?
    ;;
  *)
    log_success_msg "Usage: ${0} {start|stop}"
    exit 1
esac

exit 0
