#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# parse env vars to options
OPTIONS=""
if [[ ${NZBGET_USER} && ${NZBGET_USER-x} ]]; then
    OPTIONS+="-o ControlUsername=${NZBGET_USER} "
fi

if [[ ${NZBGET_PASS} && ${NZBGET_PASS-x} ]]; then
    OPTIONS+="-o ControlPassword=${NZBGET_PASS} "
fi

if [[ -z ${LSIO_NON_ROOT_USER} ]]; then
    exec \
        s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z localhost 6789" \
            s6-setuidgid abc /app/nzbget/nzbget -s -c /config/nzbget.conf \
            -o OutputMode=log ${OPTIONS}
else
    exec \
        s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z localhost 6789" \
            /app/nzbget/nzbget -s -c /config/nzbget.conf \
            -o OutputMode=log ${OPTIONS}
fi
