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

WEBUI_ADDRESS=$(grep -Po "^WebUI\\\Address=\K(.*)" /config/qBittorrent/qBittorrent.conf)
if [[ -z ${WEBUI_ADDRESS} ]] || [[ ${WEBUI_ADDRESS} == "*" ]]; then
    WEBUI_ADDRESS="localhost"
fi

if [[ -z ${LSIO_NON_ROOT_USER} ]]; then
exec \
    s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z ${WEBUI_ADDRESS} ${WEBUI_PORT:-8080}" \
        s6-setuidgid abc /app/qbittorrent-nox --webui-port="${WEBUI_PORT:-8080}" ${TORRENTING_PORT+--torrenting-port=${TORRENTING_PORT}}
else
    s6-notifyoncheck -d -n 300 -w 1000 -c "nc -z ${WEBUI_ADDRESS} ${WEBUI_PORT:-8080}" \
        /app/qbittorrent-nox --webui-port="${WEBUI_PORT:-8080}" ${TORRENTING_PORT+--torrenting-port=${TORRENTING_PORT}}
fi
