@echo off
rem run.bat  -  Windows wrapper that Synchronet (or Mystic) calls instead
rem of invoking sixeldoor.exe directly. Sets PATH so yt-dlp.exe and
rem ffmpeg.exe are found, plus the env vars the door needs.
rem
rem Deploy on the Windows BBS server (NOT on the dev machine):
rem   copy run.bat sixeldoor.exe  C:\sbbs\xtrn\ytube\
rem   notepad  C:\sbbs\xtrn\ytube\run.bat    <-- edit SIXELDOOR_HOSTNAME
rem
rem Then in your BBS config:
rem   Synchronet (scfg):  C:\sbbs\xtrn\ytube\run.bat %f
rem   Mystic (External Editor): C:\mystic\doors\ytube\run.bat
rem     (no token needed — Mystic exposes %MTEMP% pointing at the node
rem      temp dir where DOOR32.SYS lives, and we auto-discover from there.)

rem ----------- EDIT THESE -----------

rem Your BBS's public hostname or IP. On SyncTERM 1.10+ this is only used
rem as a fallback for terminals without the native audio mixer (older
rem SyncTERM, or any other sixel client) — those users open audio URLs like
rem   http://%SIXELDOOR_HOSTNAME%:%SIXELDOOR_AUDIO_PORT%/a/<token>
rem on their phone, so it MUST resolve from outside your LAN.
set SIXELDOOR_HOSTNAME=bbs.example.com

rem Port for the fallback audio HTTP server. Must be open in Windows
rem Firewall (netsh advfirewall firewall add rule...).
set SIXELDOOR_AUDIO_PORT=18080

rem Where you unpacked ffmpeg and put yt-dlp. Adjust to match your install.
rem Keep trailing semicolons. If you just dropped the exes next to
rem sixeldoor.exe you can leave both lines pointing at "%~dp0".
set "FFMPEG_DIR=%~dp0"
set "YTDLP_DIR=%~dp0"

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

rem ----------- /edit zone -----------

set "PATH=%FFMPEG_DIR%;%YTDLP_DIR%;%PATH%"

rem yt-dlp needs a JS runtime (Deno by default) on PATH to reliably resolve
rem some YouTube formats' signatures — without one, downloads intermittently
rem fail with HTTP 403. If Deno's installer put deno.exe somewhere not
rem already covered above (e.g. a per-user install path), add it to PATH
rem here too. See README's Runtime dependencies section, and yt-dlp's own
rem current setup instructions: https://github.com/yt-dlp/yt-dlp/wiki/EJS

rem Most ffmpeg builds (including whatever you may have grabbed quickly)
rem don't include a libjxl encoder, which this door prefers over sixel when
rem available (smaller AND better-quality video frames). To enable it,
rem download ffmpeg-master-latest-win64-gpl.zip from
rem https://github.com/BtbN/FFmpeg-Builds/releases and drop bin\ffmpeg.exe
rem into FFMPEG_DIR above — no other change needed. Not required; sixel
rem fallback works fine without it. See README's "Getting a libjxl-enabled
rem ffmpeg" section.

rem If you've set up bgutil-ytdlp-pot-provider to reduce intermittent
rem YouTube HTTP 403s (see README's "Fixing intermittent HTTP 403 errors"),
rem it runs as its own always-on background process/service listening on
rem localhost:4416 — nothing to add here, yt-dlp finds it automatically
rem once it's running.

rem Mystic exposes %MTEMP% — the node temp dir where Mystic drops
rem DOOR32.SYS. Promote it to DOOR32 so the door finds the file even
rem when Mystic's command line doesn't pass a path token. Synchronet
rem leaves MTEMP unset, so this is a Mystic-only no-op there.
if defined MTEMP if exist "%MTEMP%\door32.sys" set "DOOR32=%MTEMP%\door32.sys"
if defined MTEMP if exist "%MTEMP%\DOOR32.SYS" set "DOOR32=%MTEMP%\DOOR32.SYS"

rem Launch the door. Synchronet passes the dropfile path as %1 (%f);
rem Mystic relies on MTEMP/DOOR32 above. Either way the door figures it out.
"%~dp0sixeldoor.exe" %*
