#!/usr/bin/env /lib/runit/invoke-run

set -e

. /etc/ark/monitor-config

# logging only captures stdout, so merge stderr in with it
exec 2>&1

if [ -f /etc/ark/activity-fireball/env ]; then
    . /etc/ark/activity-fireball/env
fi

SCAMPER_REMOTE=${SCAMPER_REMOTE:-"fireball.caida.org:3474"}
SCAMPER_PPS=${SCAMPER_PPS:-100}

# fetch the fireball-specific scamper configuration for this node (a cron
# job will keep it up to date if it changes later)
if [ -x /usr/bin/ark-fireball-config ]; then
    # exit 0 on success+change, exit 1 on success+nochange
    # if this fails then the node will continue with the minimal safe config
    # that it is distributed with the package
    /usr/bin/ark-fireball-config || true
fi

# TODO add any and all dnp files?
if [ -e /etc/ark/activity-fireball/default.dnp ]; then
    SCAMPER_DNP="-O dnp=/etc/ark/activity-fireball/default.dnp"
else
    SCAMPER_DNP=""
fi

echo "Starting ark-activity-fireball..."
exec /usr/bin/scamper \
         -g /etc/ark/activity-fireball/scamper-config \
         -R "${SCAMPER_REMOTE}" \
         -p "${SCAMPER_PPS}" \
         -M "${ARK_FQDN}" \
         ${SCAMPER_DNP} \
         -O "cafile=/etc/ssl/certs/ca-certificates.crt" \
         -O "client-certfile=/etc/ark/ssl/${ARK_FQDN}.crt" \
         -O "client-privfile=/etc/ark/ssl/${ARK_FQDN}.key"
