#!/bin/bash
# run.sh — wrapper that BBS software (Synchronet, Mystic, etc.) calls
# instead of invoking the binary directly. Sets the PATH and env that
# the door needs.
#
# Deploy on the BBS server (NOT on the dev machine):
#   scp run.sh sixeldoor-static user@bbs:/sbbs/xtrn/ytube/
#   ssh user@bbs
#     sudo mv /sbbs/xtrn/ytube/sixeldoor-static /sbbs/xtrn/ytube/sixeldoor
#     sudo chmod +x /sbbs/xtrn/ytube/sixeldoor /sbbs/xtrn/ytube/run.sh
#     sudo vim /sbbs/xtrn/ytube/run.sh   # edit SIXELDOOR_HOSTNAME below!
#
# Then in scfg:
#   Command Line: /sbbs/xtrn/ytube/run.sh %f

# ---- EDIT THIS ----
# The public hostname or IP that your users reach the BBS at. On SyncTERM
# 1.10+ this is only needed as a fallback, for terminals without the native
# audio mixer (older SyncTERM, or any other sixel client) — the door prints
# a URL as http://$SIXELDOOR_HOSTNAME:$PORT/a/<token> for those users to
# open on their phone or a second window, so it MUST resolve from outside
# your LAN.
SIXELDOOR_HOSTNAME="bbs.example.com"

# Port for the fallback audio HTTP server. Must be open in your firewall.
SIXELDOOR_AUDIO_PORT=18080

# Optional: target frame rate (defaults: sixel 5, JXL 10). Uncomment and
# lower ONLY if video plays in slow motion / finishes noticeably after the
# audio on this connection — measure the actual delivered fps from
# sixeldoor.log first (frame count from "player: stopped after N frames" /
# wall-clock playback time) and set the number here to match. Setting
# either one HIGHER than this deployment can actually deliver just
# reproduces the same slow-motion problem — see README's "Video running in
# slow motion" section before changing these.
# SIXELDOOR_SIXEL_FPS=5
# SIXELDOOR_JXL_FPS=10
# -------------------

# Search common install locations for yt-dlp, ffmpeg, and Deno (the JS
# runtime yt-dlp needs to reliably resolve YouTube formats — see README).
# Synchronet often starts doors with a minimal PATH, so we set an explicit
# one here; note this does NOT source your shell rc files, so anything an
# installer only added there (Deno's does, by default) needs to be added
# here explicitly too. /usr/local/bin is listed first on purpose: the door
# also checks it first internally, so a libjxl-enabled ffmpeg dropped there
# (see README's "Getting a libjxl-enabled ffmpeg") automatically wins over
# a distro package in /usr/bin without editing anything else.
export PATH="/usr/local/bin:/usr/bin:/bin:$HOME/.local/bin:$HOME/.deno/bin:$PATH"

# If you set up bgutil-ytdlp-pot-provider to reduce YouTube HTTP 403s (see
# README's "Fixing intermittent HTTP 403 errors"), it runs as its own
# always-on service on localhost:4416 — nothing to add here, yt-dlp finds
# it on its own once the plugin's installed and the service is running.

export SIXELDOOR_HOSTNAME
export SIXELDOOR_AUDIO_PORT
export SIXELDOOR_SIXEL_FPS
export SIXELDOOR_JXL_FPS

# Exec the door binary with the dropfile argument Synchronet passes us.
exec "$(dirname "$0")/sixeldoor" "$@"
