#!/usr/bin/env bash
#
# build_udp_hls_native.sh — run this ON the build host itself
# (18.04, 20.04, 22.04, or 24.04). Installs the matching build
# dependencies, compiles udp_hls.c, and names the output file
# correctly for use with create_universal_bundle.sh /
# rebuild_universal_installer.sh.
#
# USAGE (run as root, or with sudo for the apt-get step):
#   sudo bash build_udp_hls_native.sh ./udp_hls.c
#
# Output: ./udp_hls-ubuntu<VERSION>-<CODENAME> in the current directory
# (e.g. ./udp_hls-ubuntu22.04-jammy), plus a printed sha256.
#
# Only supports 18.04/20.04/22.04/24.04 — these four all use the same
# API path (see udp_hls.c's UDPHLS_HAVE_CH_LAYOUT shim: bionic/focal
# use the pre-5.1 channel-layout API, jammy/noble use the newer one,
# and the shim picks the right one automatically from whatever
# libavutil headers are found on this host, based on its version).
# 16.04 (xenial) needs a fundamentally different build process
# (FFmpeg statically compiled from source, since xenial's system
# FFmpeg predates the API this code uses entirely) — see
# build_udp_hls_xenial_static.sh for that, separately.
#
set -euo pipefail

SRC_FILE="${1:-./udp_hls.c}"
[[ -f "$SRC_FILE" ]] || { echo "ERROR: source not found: $SRC_FILE" >&2; echo "Usage: $0 [path/to/udp_hls.c]" >&2; exit 1; }
[[ "$EUID" -eq 0 ]] || { echo "ERROR: run as root (sudo)" >&2; exit 1; }

log() { echo "[$(date '+%H:%M:%S')] $*"; }

# ---------------------------------------------------------------------
# Detect OS
# ---------------------------------------------------------------------
[[ -f /etc/os-release ]] || { echo "ERROR: /etc/os-release not found" >&2; exit 1; }
. /etc/os-release
CODENAME="${VERSION_CODENAME:-${UBUNTU_CODENAME:-}}"
log "Detected: $PRETTY_NAME (codename: ${CODENAME:-unknown})"

case "$CODENAME" in
    noble)  OUT_NAME="udp_hls-ubuntu24.04-noble" ;;
    jammy)  OUT_NAME="udp_hls-ubuntu22.04-jammy" ;;
    focal)  OUT_NAME="udp_hls-ubuntu20.04-focal" ;;
    bionic) OUT_NAME="udp_hls-ubuntu18.04-bionic" ;;
    xenial)
        echo "ERROR: xenial needs the separate static-FFmpeg build process" >&2
        echo "(build_udp_hls_xenial_static.sh) -- this script only covers" >&2
        echo "18.04/20.04/22.04/24.04, which share a compatible build path." >&2
        exit 1
        ;;
    *)
        echo "ERROR: unrecognized/untested codename: ${CODENAME:-unknown}" >&2
        echo "This script only covers bionic/focal/jammy/noble." >&2
        exit 1
        ;;
esac
log "Output will be named: $OUT_NAME"

# ---------------------------------------------------------------------
# Install build dependencies
# ---------------------------------------------------------------------
log "=== Installing build dependencies ==="
apt-get update -qq || echo "WARNING: apt-get update had errors (continuing — see earlier discussion of unrelated broken repos)"
DEBIAN_FRONTEND=noninteractive apt-get install -y \
    build-essential gcc file \
    libavcodec-dev libavutil-dev libswresample-dev libswscale-dev \
    libssl-dev libmpg123-dev

# ---------------------------------------------------------------------
# Compile
# ---------------------------------------------------------------------
log "=== Compiling ==="
gcc -O2 -pthread -o "$OUT_NAME" "$SRC_FILE" \
    -lssl -lcrypto -lmpg123 -lx264 $(pkg-config --cflags --libs libavformat libavcodec libavutil libswscale)
chmod 755 "$OUT_NAME"

# ---------------------------------------------------------------------
# Verify
# ---------------------------------------------------------------------
log "=== Verifying ==="
file "./$OUT_NAME"
echo ""
echo "Dynamic dependencies:"
ldd "./$OUT_NAME"
echo ""

set +e
RUN_OUTPUT="$(./"$OUT_NAME" --version 2>&1)"
set -e
if echo "$RUN_OUTPUT" | grep -qiE "error while loading shared librar|cannot open shared object"; then
    echo "ERROR: binary failed to launch — see output above" >&2
    exit 1
fi
log "Binary launches correctly."

echo ""
echo "=== Build complete ==="
echo "  Output: ./$OUT_NAME"
echo "  SHA256: $(sha256sum "./$OUT_NAME" | awk '{print $1}')"
echo ""
echo "Repeat this same script on your other build hosts (18.04/20.04/22.04/24.04),"
echo "then collect all 4 output files together and hand them to"
echo "create_universal_bundle.sh to package the final installer."
