#!/usr/bin/env bash
# Install the independent emergency encoder on a dedicated standby VM.
#
# This does NOT start or enable the service and it never accepts a stream key on the command line.
# An operator first writes /etc/viewregatta/emergency-standby.env with mode 0600, verifies the exact
# backup ingest address, and only then enables the encoder and watchdog.
set -euo pipefail

REPO="${REPO:-/opt/viewregatta/TRAP}"
MEDIA_DIR="${MEDIA_DIR:-/var/lib/viewregatta/emergency}"
SERVICE_USER="${SERVICE_USER:-viewregatta}"
PACKAGE_DIR="${PACKAGE_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"

if [[ -f "$PACKAGE_DIR/emergency_stream.py" ]]; then
    EMERGENCY_SOURCE="$PACKAGE_DIR/emergency_stream.py"
    LOGO_SOURCE="$PACKAGE_DIR/logo-source.png"
    UNIT_SOURCE="$PACKAGE_DIR/emergency-standby.service"
    CHECK_SOURCE="$PACKAGE_DIR/check_emergency_standby.sh"
    WATCH_SOURCE="$PACKAGE_DIR/watch_emergency_standby.sh"
    WATCH_UNIT_SOURCE="$PACKAGE_DIR/emergency-standby-watchdog.service"
    WATCH_TIMER_SOURCE="$PACKAGE_DIR/emergency-standby-watchdog.timer"
    RUNBOOK_SOURCE="$PACKAGE_DIR/trwc-emergency-broadcast-runbook.md"
else
    EMERGENCY_SOURCE="$REPO/v3/broadcast/live/emergency_stream.py"
    LOGO_SOURCE="$REPO/v3/broadcast/core/web/assets/logo-source.png"
    UNIT_SOURCE="$REPO/v3/broadcast/deploy/emergency-standby.service"
    CHECK_SOURCE="$REPO/v3/broadcast/deploy/check_emergency_standby.sh"
    WATCH_SOURCE="$REPO/v3/broadcast/deploy/watch_emergency_standby.sh"
    WATCH_UNIT_SOURCE="$REPO/v3/broadcast/deploy/emergency-standby-watchdog.service"
    WATCH_TIMER_SOURCE="$REPO/v3/broadcast/deploy/emergency-standby-watchdog.timer"
    RUNBOOK_SOURCE="$REPO/docs/trwc-emergency-broadcast-runbook.md"
fi
if [[ ! -f "$EMERGENCY_SOURCE" || ! -f "$LOGO_SOURCE" || ! -f "$UNIT_SOURCE" ||
      ! -f "$CHECK_SOURCE" || ! -f "$WATCH_SOURCE" ||
      ! -f "$WATCH_UNIT_SOURCE" || ! -f "$WATCH_TIMER_SOURCE" ]]; then
    echo "the emergency encoder package is incomplete under PACKAGE_DIR=$PACKAGE_DIR" >&2
    exit 2
fi

if ! id "$SERVICE_USER" >/dev/null 2>&1; then
    sudo useradd --system --home-dir /nonexistent --shell /usr/sbin/nologin "$SERVICE_USER"
fi
sudo install -d -o "$SERVICE_USER" -g "$SERVICE_USER" -m 0750 "$MEDIA_DIR"
sudo install -d -o root -g "$SERVICE_USER" -m 0750 /etc/viewregatta
sudo install -D -o root -g root -m 0644 "$EMERGENCY_SOURCE" \
    "$REPO/v3/broadcast/live/emergency_stream.py"
sudo install -D -o root -g root -m 0644 "$LOGO_SOURCE" \
    "$REPO/v3/broadcast/core/web/assets/logo-source.png"
sudo install -D -o root -g root -m 0644 "$UNIT_SOURCE" \
    "$REPO/v3/broadcast/deploy/emergency-standby.service"
sudo install -D -o root -g root -m 0755 "$CHECK_SOURCE" \
    "$REPO/v3/broadcast/deploy/check_emergency_standby.sh"
sudo install -D -o root -g root -m 0755 "$WATCH_SOURCE" \
    "$REPO/v3/broadcast/deploy/watch_emergency_standby.sh"
sudo install -D -o root -g root -m 0644 "$WATCH_UNIT_SOURCE" \
    "$REPO/v3/broadcast/deploy/emergency-standby-watchdog.service"
sudo install -D -o root -g root -m 0644 "$WATCH_TIMER_SOURCE" \
    "$REPO/v3/broadcast/deploy/emergency-standby-watchdog.timer"
if [[ -f "$RUNBOOK_SOURCE" ]]; then
    sudo install -D -o root -g root -m 0644 "$RUNBOOK_SOURCE" \
        "$REPO/docs/trwc-emergency-broadcast-runbook.md"
fi
sudo apt-get update -qq
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq ffmpeg fonts-dejavu-core

BUILD_DIR="$(mktemp -d)"
trap 'rm -rf "$BUILD_DIR"' EXIT
python3 "$REPO/v3/broadcast/live/emergency_stream.py" build \
    --output "$BUILD_DIR/technical_hold.mp4"
python3 "$REPO/v3/broadcast/live/emergency_stream.py" verify \
    "$BUILD_DIR/technical_hold.mp4"
sudo install -o "$SERVICE_USER" -g "$SERVICE_USER" -m 0440 \
    "$BUILD_DIR/technical_hold.mp4" "$MEDIA_DIR/technical_hold.mp4"

sudo install -o root -g root -m 0644 \
    "$UNIT_SOURCE" \
    /etc/systemd/system/emergency-standby.service
sudo install -o root -g root -m 0644 \
    "$WATCH_UNIT_SOURCE" \
    /etc/systemd/system/emergency-standby-watchdog.service
sudo install -o root -g root -m 0644 \
    "$WATCH_TIMER_SOURCE" \
    /etc/systemd/system/emergency-standby-watchdog.timer
sudo systemctl daemon-reload

if [[ ! -f /etc/viewregatta/emergency-standby.env ]]; then
    sudo install -o root -g root -m 0600 /dev/null /etc/viewregatta/emergency-standby.env
fi

cat <<'EOF'
Emergency standby installed but NOT started.

1. Put only these values in /etc/viewregatta/emergency-standby.env (mode 0600):
     YOUTUBE_BACKUP_INGEST=<the Backup server URL from YouTube Live Control Room>
     YOUTUBE_STREAM_KEY=<the raw stream key>

2. Verify that the ingest hostname/path is the BACKUP, not the primary.
3. Start:  sudo systemctl enable --now emergency-standby emergency-standby-watchdog.timer
4. Check:  sudo systemctl status emergency-standby --no-pager
5. Health: python3 /opt/viewregatta/TRAP/v3/broadcast/live/emergency_stream.py status \
              --health /run/viewregatta/emergency-standby.json

Never place the environment file in the repository or print it in a terminal recording.
EOF
