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

# Kapowarr is started through the upstream entrypoint, which is what implements
# PUID/PGID (groupmod -o / usermod -o, then gosu). ha_entrypoint.sh replaced it
# as the container entrypoint so that cont-init.d and nginx could run too, so it
# is called again here rather than reimplemented. Both usermod and groupmod are
# given -o upstream, so a PUID that collides with an existing account is not an
# error.
#
# --UrlBase is what makes ingress work: Kapowarr renders absolute urls, Home
# Assistant strips its own ingress prefix before forwarding, and nginx adds it
# back by rewriting this fixed prefix. See rootfs/etc/nginx/servers/ingress.conf.
#
# --Host and --Port are passed for the same reason, even though they are already
# the upstream defaults: all three are stored in the database, and Kapowarr reads
# the stored value when the flag is absent. Without them, a host or port changed
# in Settings > General would survive every restart and upgrade while nginx and
# the healthcheck stayed pointed at 127.0.0.1:5656 -- a permanent 502 with no way
# back except editing the database. Passing all three makes the whole hosting
# section self repairing.
#
# Upstream applies these three only on a startup, never on the restarts Kapowarr
# performs itself, so they are re-applied once per container start and do not
# fight the user in between.

bashio::log.info "Starting Kapowarr (served on the /kapowarr path, see the addon documentation)"

exec /app/entrypoint.sh python3 /app/Kapowarr.py \
    --DatabaseFolder /config \
    --LogFolder /config/logs \
    --Host 0.0.0.0 \
    --Port 5656 \
    --UrlBase /kapowarr
