#!/bin/bash
# install-bbs-deps.sh
#
# Run this on the BBS SERVER (not the dev machine) to install every runtime
# dependency sixeldoor needs. Safe to re-run — idempotent.
#
#   sudo ./install-bbs-deps.sh
#
# Tested on Ubuntu 22.04, 24.04 and Debian 12.

set -e

if [ "$EUID" -ne 0 ]; then
    echo "error: must run as root (try:  sudo $0 )" >&2
    exit 1
fi

say() { printf '\n\033[1;36m==\033[0m %s\n' "$*"; }
ok()  { printf '   \033[1;32m✓\033[0m %s\n' "$*"; }
warn(){ printf '   \033[1;33m!\033[0m %s\n' "$*"; }

# ---------- detect package manager ----------
if command -v apt-get >/dev/null 2>&1; then
    PM=apt
elif command -v dnf >/dev/null 2>&1; then
    PM=dnf
elif command -v yum >/dev/null 2>&1; then
    PM=yum
else
    echo "error: no supported package manager (apt/dnf/yum) found" >&2
    exit 1
fi
ok "package manager: $PM"

# ---------- 1. ffmpeg ----------
say "installing ffmpeg"
case "$PM" in
    apt)
        export DEBIAN_FRONTEND=noninteractive
        apt-get update -qq
        apt-get install -y -qq ffmpeg python3-pip python3-venv curl
        ;;
    dnf|yum)
        $PM install -y ffmpeg python3-pip curl
        ;;
esac
ok "ffmpeg: $(ffmpeg -version 2>/dev/null | head -1 | cut -d' ' -f1-3)"

# ---------- 2. purge stale yt-dlp if present ----------
say "removing stale distro yt-dlp (if any)"
case "$PM" in
    apt)
        if dpkg -l yt-dlp 2>/dev/null | grep -q '^ii'; then
            apt-get remove -y -qq yt-dlp
            ok "removed apt yt-dlp"
        else
            ok "no apt yt-dlp to remove"
        fi
        ;;
    dnf|yum)
        if rpm -q yt-dlp >/dev/null 2>&1; then
            $PM remove -y yt-dlp
            ok "removed rpm yt-dlp"
        else
            ok "no rpm yt-dlp to remove"
        fi
        ;;
esac

# ---------- 3. install current yt-dlp from PyPI ----------
say "installing latest yt-dlp from PyPI"
# PEP 668 "externally managed" on newer Ubuntu/Debian: need --break-system-packages.
PIP_ARGS="--upgrade"
if python3 -c 'import sys; exit(0 if sys.version_info >= (3,11) else 1)'; then
    PIP_ARGS="$PIP_ARGS --break-system-packages"
fi
pip3 install $PIP_ARGS yt-dlp >/dev/null
YT_PATH=$(command -v yt-dlp)
YT_VER=$(yt-dlp --version)
ok "yt-dlp $YT_VER installed at $YT_PATH"

# ---------- 4. sanity-check both tools are on the default PATH ----------
say "sanity checks"
for bin in ffmpeg yt-dlp; do
    p=$(command -v "$bin" || true)
    if [ -z "$p" ]; then
        warn "$bin not in PATH (this shouldn't happen)"
    else
        ok "$bin -> $p"
    fi
done

# Double-check ffmpeg has mp3 encoder (needed for /a/ audio streams)
if ffmpeg -hide_banner -encoders 2>/dev/null | grep -q libmp3lame; then
    ok "libmp3lame encoder present (audio will work)"
else
    warn "libmp3lame missing — audio will fail. install ffmpeg-full or build with lame."
fi

# ---------- 5. print next steps ----------
cat <<EOF

$(tput bold 2>/dev/null)All runtime dependencies installed.$(tput sgr0 2>/dev/null)

Remaining steps are sysop decisions — you do these:

  1. Copy the binary onto this server (run on your dev box):
       scp sixeldoor-static user@$(hostname):/sbbs/xtrn/ytube/sixeldoor
     Then make it executable:
       chmod +x /sbbs/xtrn/ytube/sixeldoor

  2. Create /sbbs/xtrn/ytube/run.sh (wrapper that sets PATH + env):

       #!/bin/bash
       export PATH="/usr/local/bin:/usr/bin:/bin:\$PATH"
       export SIXELDOOR_HOSTNAME=$(hostname -f 2>/dev/null || hostname)
       export SIXELDOOR_AUDIO_PORT=18080
       exec /sbbs/xtrn/ytube/sixeldoor "\$@"

     chmod +x /sbbs/xtrn/ytube/run.sh

  3. Open port 18080 in your firewall (for browser-delivered audio).

  4. Register in Synchronet scfg:
       External Programs -> Online Programs -> (Add)
       Command Line:    /sbbs/xtrn/ytube/run.sh %f
       BBS Drop File:   DOOR32.SYS
       Place Drop File: Node Directory
       Intercept I/O:   Yes

  5. Tail the door's log on first use:
       tail -f /tmp/sixeldoor.log
EOF
