#!/usr/bin/env bash
# One-shot: display + WM + audio sink + game + menu driver + YouTube push.
#
# GPU BOX: set GPU=1 to drop software-rendering forcing and use NVENC.
#   GPU=1 ./go_live.sh
set -u
BASE="$(cd "$(dirname "$0")" && pwd)"
source "$BASE/stream.env"
export DISPLAY=":${DISPLAY_NUM}"
RW="${RENDER_WIDTH:-854}"; RH="${RENDER_HEIGHT:-480}"
GPU="${GPU:-0}"
mkdir -p "$BASE/logs" "$BASE/cache/dxvk" "$BASE/cache/mesa"

# ---------- display ----------
if ! xdpyinfo -display "$DISPLAY" >/dev/null 2>&1; then
  setsid nohup Xvfb "$DISPLAY" -screen 0 "${RW}x${RH}x24" -nolisten tcp \
    </dev/null >"$BASE/logs/xvfb.log" 2>&1 &
  sleep 3
fi
# a WM is REQUIRED: without it SDL never gets focus and ignores all keyboard input
if ! pgrep -x openbox >/dev/null; then
  setsid nohup env DISPLAY="$DISPLAY" openbox </dev/null >"$BASE/logs/openbox.log" 2>&1 &
  sleep 3
fi

# ---------- audio (null sink; do NOT use anullsrc, it streams at 0 kbps) ----------
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
export PULSE_SERVER="unix:${XDG_RUNTIME_DIR}/pulse/native"
pulseaudio --check 2>/dev/null || { pulseaudio --start --exit-idle-time=-1 >/dev/null 2>&1; sleep 2; }
pactl list short sinks 2>/dev/null | grep -q tf2sink || \
  pactl load-module module-null-sink sink_name=tf2sink >/dev/null 2>&1

# ---------- configs ----------
cp -f "$BASE/cfg/autoexec.cfg"  "$BASE/tf2/tf/cfg/autoexec.cfg"
cp -f "$BASE/cfg/server.cfg"    "$BASE/tf2/tf/cfg/server.cfg"
cp -f "$BASE/cfg/ctf_2fort.cfg" "$BASE/tf2/tf/cfg/ctf_2fort.cfg"
# nav mesh: bots spawn but never move without it
[ -f "$BASE/assets/ctf_2fort.nav" ] && cp -n "$BASE/assets/ctf_2fort.nav" "$BASE/tf2/tf/maps/ctf_2fort.nav"
# blank MOTD text (panel still shows, but empty)
: > "$BASE/tf2/tf/motd.txt"; : > "$BASE/tf2/tf/motd_text.txt"
: > "$BASE/tf2/tf/cfg/motd_default.txt"; : > "$BASE/tf2/tf/cfg/motd_text_default.txt"
echo 440 > "$BASE/tf2/steam_appid.txt"
rm -f "$BASE/tf2/tf/console.log"

# ---------- menu driver + stream ----------
setsid nohup bash "$BASE/menu_driver.sh" </dev/null >"$BASE/logs/menu.log" 2>&1 &
setsid nohup bash "$BASE/run_capture.sh" </dev/null >"$BASE/logs/capture.log" 2>&1 &

# ---------- game ----------
SW_ENV=()
if [ "$GPU" != "1" ]; then
  # force software rendering (CPU-only box). REMOVE THESE ON A GPU BOX.
  SW_ENV=(LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe)
fi

cd "$BASE/tf2"
exec "$BASE/sniper/run-in-sniper" -- env \
  DISPLAY="$DISPLAY" SteamAppId=440 SteamGameId=440 FONTCONFIG_PATH=/etc/fonts \
  XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" PULSE_SERVER="$PULSE_SERVER" \
  SDL_AUDIODRIVER=pulse PULSE_SINK=tf2sink \
  DXVK_STATE_CACHE=1 DXVK_STATE_CACHE_PATH="$BASE/cache/dxvk" \
  MESA_SHADER_CACHE_DIR="$BASE/cache/mesa" MESA_SHADER_CACHE_MAX_SIZE=4G \
  "${SW_ENV[@]}" \
  ./tf.sh -game tf -steam -insecure -console \
  -novid -nojoy -nosteamcontroller \
  -fullscreen -w "$RW" -h "$RH" +mat_setvideomode "$RW" "$RH" 0 \
  -dxlevel 90 -condebug \
  +sv_lan 1 +sv_cheats 1 +maxplayers 16 \
  +exec autoexec +map ctf_2fort \
  >"$BASE/logs/tf2.log" 2>&1
