#! /bin/sh -e

#MESSAGE#

. /lib/lsb/init-functions

NAME=#APPNAME#
HOST=#HOSTNAME#
SITE=#SITENAME#

HOME=/srv/${NAME}-production
PORT=#PROXYPORT#
BASEURI="http://#HOSTNAME#:#PROXYPORT#/"

STARTOPTIONS="${STARTOPTIONS} -v"
# STARTOPTIONS="${STARTOPTIONS} --quiet"

STOPOPTIONS="${STARTOPTIONS} -v"
# STOPOPTIONS="${STOPOPTIONS} --quiet"
STOPOPTIONS="${STOPOPTIONS} --retry 2"

OPTIONS="${OPTIONS} --store ${HOME}"
OPTIONS="${OPTIONS} --static /usr/share/${NAME}-data"
#OPTIONS="${OPTIONS} --home ${HOME}"			# Not implemented
OPTIONS="${OPTIONS} --logs /var/log/apache2/#SITENAME#"
OPTIONS="${OPTIONS} --log-mode Development"
OPTIONS="${OPTIONS} --http-port #PROXYPORT#"
OPTIONS="${OPTIONS} --base-uri ${BASEURI}"
OPTIONS="${OPTIONS} --enable-analytics"
OPTIONS="${OPTIONS} +RTS -IO -RTS"

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

case "$1" in
  start)
    # Check for daemon presence
    test -x /usr/bin/${NAME}-server || exit 0
    log_begin_msg "Starting ${NAME}-production..."
    mkdir -p ${HOME}
    start-stop-daemon --start -b --pidfile /var/run/${NAME}-production --make-pidfile -d ${HOME} --exec /usr/bin/${NAME}-server ${STARTOPTIONS} -- ${OPTIONS}
    log_end_msg $?
    ;;
  stop)
    log_begin_msg "Stopping #APPNAME#-production..."
    start-stop-daemon --stop --pidfile /var/run/${NAME}-production --oknodo ${STOPOPTIONS} # --exec /usr/bin/#APPNAME# -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
