description "Palladium Core Daemon"

start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]

env PALLADIUMD_BIN="/usr/bin/palladiumd"
env PALLADIUMD_USER="palladium"
env PALLADIUMD_GROUP="palladium"
env PALLADIUMD_PIDDIR="/var/run/palladiumd"
# upstart can't handle variables constructed with other variables
env PALLADIUMD_PIDFILE="/var/run/palladiumd/palladiumd.pid"
env PALLADIUMD_CONFIGFILE="/etc/palladium/palladium.conf"
env PALLADIUMD_DATADIR="/var/lib/palladiumd"

expect fork

respawn
respawn limit 5 120
kill timeout 600

pre-start script
    # this will catch non-existent config files
    # palladiumd will check and exit with this very warning, but it can do so
    # long after forking, leaving upstart to think everything started fine.
    # since this is a commonly encountered case on install, just check and
    # warn here.
    if ! grep -qs '^rpcpassword=' "$PALLADIUMD_CONFIGFILE" ; then
        echo "ERROR: You must set a secure rpcpassword to run palladiumd."
        echo "The setting must appear in $PALLADIUMD_CONFIGFILE"
        echo
        echo "This password is security critical to securing wallets "
        echo "and must not be the same as the rpcuser setting."
        echo "You can generate a suitable random password using the following "
        echo "command from the shell:"
        echo
        echo "bash -c 'tr -dc a-zA-Z0-9 < /dev/urandom | head -c32 && echo'"
        echo
        echo "It is recommended that you also set alertnotify so you are "
        echo "notified of problems:"
        echo
        echo "ie: alertnotify=echo %%s | mail -s \"Palladium Alert\"" \
            "admin@foo.com"
        echo
        exit 1
    fi

    mkdir -p "$PALLADIUMD_PIDDIR"
    chmod 0755 "$PALLADIUMD_PIDDIR"
    chown $PALLADIUMD_USER:$PALLADIUMD_GROUP "$PALLADIUMD_PIDDIR"
    chown $PALLADIUMD_USER:$PALLADIUMD_GROUP "$PALLADIUMD_CONFIGFILE"
    chmod 0660 "$PALLADIUMD_CONFIGFILE"
end script

exec start-stop-daemon \
    --start \
    --pidfile "$PALLADIUMD_PIDFILE" \
    --chuid $PALLADIUMD_USER:$PALLADIUMD_GROUP \
    --exec "$PALLADIUMD_BIN" \
    -- \
    -pid="$PALLADIUMD_PIDFILE" \
    -conf="$PALLADIUMD_CONFIGFILE" \
    -datadir="$PALLADIUMD_DATADIR" \
    -disablewallet \
    -daemon

