#!upstart
description "ServiceDock"
author      "Marc Wäckerlin"

start on (local-filesystems and net-device-up)
stop on runlevel [!2345]

respawn

script
    echo $$ > /var/run/servicedock.pid
    # there are some useful defaults
    # do not edit this file, overwrite values in /etc/default/servicedock
    EXEC_SERVICEDOCK="/usr/bin/nodejs /usr/share/servicedock/nodejs/servicedock"
    SERVICEDOCK_LOG="/var/log/servicedock.log"
    SERVICEDOCK_USER="servicedock"
    SERVICEDOCK_PORT=""
    [ -r /etc/default/servicedock ] && . /etc/default/servicedock
    if test -n "${SERVICEDOCK_USER}"; then
        exec sudo -u "${SERVICEDOCK_USER}" ${EXEC_SERVICEDOCK} ${SERVICEDOCK_PORT} >> ${SERVICEDOCK_LOG} 2>&1
    else
        exec ${NODEJS_BIN} >> ${SERVICEDOCK_LOG} 2>&1
    fi
end script

pre-start script
    SERVICEDOCK_LOG="/var/log/servicedock.log"
    [ -r /etc/default/servicedock ] && . /etc/default/servicedock
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> ${SERVICEDOCK_LOG}
end script

pre-stop script
    SERVICEDOCK_LOG="/var/log/servicedock.log"
    [ -r /etc/default/servicedock ] && . /etc/default/servicedock
    rm /var/run/servicedock.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> ${SERVICEDOCK_LOG}
end script
