#! /bin/sh -e

#MESSAGE#

. /lib/lsb/init-functions

# Check for daemon presence
test -x /usr/bin/${NAME}-server || exit 0
test -f /etc/default/${NAME}-production || exit 0

NAME=#APPNAME#

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

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

STOPOPTIONS="${STOPOPTIONS} --quiet"
STOPOPTIONS="${STOPOPTIONS} --retry 2"

OPTIONS="${OPTIONS} --store /srv/${NAME}-production"
OPTIONS="${OPTIONS} --static /usr/share/${NAME}-data"
#OPTIONS="${OPTIONS} --home ${HOME}"			# Not implemented
OPTIONS="${OPTIONS} --logs /var/log/${HOME}.com"
OPTIONS="${OPTIONS} --log-mode Production"
OPTIONS="${OPTIONS} --http-port ${PORT}"
OPTIONS="${OPTIONS} --base-uri ${BASEURI}"
OPTIONS="${OPTIONS} --enable-analytics"
OPTIONS="${OPTIONS} +RTS -IO"

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

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