#!/bin/sh
# Entrypoint for the Conjurer share container: renders the vhost, seeds the
# share index, then runs the two periodic jobs alongside Apache.
#
# Cron is deliberately not used. Both scripts are already written to be safe to
# re-run (the scanner writes atomically, the revoker persists its log position),
# so plain sleep loops give the same result with one less moving part and with
# their output on the container's stdout where `docker logs` can see it.
set -eu

: "${CONJURER_SHARE_SERVER_NAME:=localhost}"
: "${CONJURER_SHARE_SCAN_PATH:=/mnt/shares}"
: "${CONJURER_SHARE_DB:=/srv/share/db/share_scan.json}"
: "${CONJURER_SHARE_DIR:=/var/www/html/share}"
: "${CONJURER_APACHE_LOG:=/var/log/apache2/share_access.log}"
: "${CONJURER_SHARE_SCAN_INTERVAL:=3600}"
: "${CONJURER_SHARE_REVOKE_INTERVAL:=60}"
: "${CONJURER_SHARE_TTL_SECONDS:=3600}"
export CONJURER_SHARE_SCAN_PATH CONJURER_SHARE_DB CONJURER_SHARE_DIR \
       CONJURER_APACHE_LOG CONJURER_SHARE_TTL_SECONDS

# Render the vhost (same placeholder pattern as icecast.xml.tpl).
sed "s|__SERVER_NAME__|${CONJURER_SHARE_SERVER_NAME}|g" \
    /app/share-vhost.conf.tpl > /etc/apache2/sites-available/000-default.conf

mkdir -p "$CONJURER_SHARE_DIR" \
         "$(dirname "$CONJURER_SHARE_DB")" \
         "$(dirname "$CONJURER_APACHE_LOG")" \
         /var/run/apache2
chown -R www-data:www-data "$CONJURER_SHARE_DIR"

# revoke_shares.py seeks to a stored byte offset, so the log must exist before
# the first pass or it starts by reporting an error.
touch "$CONJURER_APACHE_LOG"

# Seed the index immediately when absent - otherwise /get_share_list returns
# nothing until the first scheduled scan, which looks like a broken feature.
if [ ! -f "$CONJURER_SHARE_DB" ]; then
    echo "[share] no index at $CONJURER_SHARE_DB - running initial scan of $CONJURER_SHARE_SCAN_PATH"
    python3 /app/scan_shares.py || echo "[share] initial scan failed, continuing"
fi

# Periodic index rebuild.
(
    while true; do
        sleep "$CONJURER_SHARE_SCAN_INTERVAL"
        echo "[share] rescanning $CONJURER_SHARE_SCAN_PATH"
        python3 /app/scan_shares.py || echo "[share] scan failed"
    done
) &
SCAN_PID=$!

# Periodic revocation of links whose TTL expired after first download.
(
    while true; do
        sleep "$CONJURER_SHARE_REVOKE_INTERVAL"
        python3 /app/revoke_shares.py || echo "[share] revoke failed"
    done
) &
REVOKE_PID=$!

trap 'kill "$SCAN_PID" "$REVOKE_PID" 2>/dev/null || true' INT TERM

echo "[share] serving $CONJURER_SHARE_DIR as ${CONJURER_SHARE_SERVER_NAME}/share"
echo "[share] scan every ${CONJURER_SHARE_SCAN_INTERVAL}s, revoke sweep every ${CONJURER_SHARE_REVOKE_INTERVAL}s, TTL ${CONJURER_SHARE_TTL_SECONDS}s after first download"

exec apache2ctl -D FOREGROUND
