#!/bin/bash
# Derive one seat's full environment and exec the live stack.
# Seat N: display :1N, signaling 8144+N, ICE 40000+200(N-1)..+199, private
# prefix, capture/control files, pulse sink, and logs.
set -euo pipefail

SEAT=${1:?usage: w3cs-seat-launch.sh <seat-number 1..9>}
case "$SEAT" in
  [1-9]) ;;
  *) echo "seat must be 1..9" >&2; exit 2 ;;
esac
IDX=$((SEAT - 1))

export W3_D3D9_DISPLAY=":1$SEAT"
export W3CS_RELAY_PORT=$((8144 + SEAT))
export W3CS_ICE_MIN=$((40000 + 200 * IDX))
export W3CS_ICE_MAX=$((40199 + 200 * IDX))
export W3_D3D9_CAPTURE_FILE="/tmp/w3cs-command-stream-seat$SEAT.bin"
export W3_D3D9_STREAM_FILE="Z:\\tmp\\w3cs-command-stream-seat$SEAT.bin"
export W3CS_GAME_CONTROL="/tmp/w3cs-game-control-seat$SEAT"
export W3_D3D9_CONTROL_FILE="Z:\\tmp\\w3cs-game-control-seat$SEAT"
export W3CS_SEAT_STATE="/tmp/w3cs-seat-state-seat$SEAT.json"
export WINEPREFIX="/opt/war3-runtime/classic/prefix-seat$SEAT"
export W3CS_PREFIX64="/opt/war3-runtime/classic-131/prefix64-seat$SEAT"
export W3CS_PULSE_SINK="w3cs$SEAT"
export W3CS_LOG_SUFFIX="-seat$SEAT"
# One raster thread per seat: with rasterization disabled llvmpipe is nearly
# idle, and seat count matters more than per-seat headroom on this box.
export LP_NUM_THREADS=${LP_NUM_THREADS:-1}

# Old engines (1.14/1.24) ignore every resolution stamp and size their outer
# window to exactly 0.75 x desktop, with the D3D backbuffer as its client
# area (frame 8px wide, 34px tall; measured 1920x1440 -> 1432x1046 and
# 1024x768 -> 760x542). Sizing the seat's Xvfb root so that this law lands
# on 1024x768 keeps every lane at the session resolution without touching
# the engines: 0.75 x 1376 = 1032 outer -> 1024 client. 1.28.5 reads the
# registry stamp and fits fine on the smaller root.
export W3_D3D9_DISPLAY_WIDTH=${W3_D3D9_DISPLAY_WIDTH:-1376}
export W3_D3D9_DISPLAY_HEIGHT=${W3_D3D9_DISPLAY_HEIGHT:-1070}

exec /home/ubuntu/w3cs-lab/lab-live.sh
