#!/bin/bash
# Run this on the MacBook Pro, in the same folder as faster_whisper_server.py
# (copy both files over — scp/AirDrop/USB, whatever's easiest).
#
# Uses the Mac's built-in python3 + a venv, no Homebrew required.
set -e
cd "$(dirname "$0")"

ARCH=$(uname -m)
echo "Detected architecture: $ARCH"
if [ "$ARCH" = "arm64" ]; then
  echo "Apple Silicon — decent CPU throughput, try WHISPER_MODEL=medium first."
else
  echo "Intel Mac — CPU will be slower, start with WHISPER_MODEL=small if medium lags."
fi

if [ ! -d venv ]; then
  python3 -m venv venv
fi
venv/bin/pip install --upgrade pip
venv/bin/pip install faster-whisper fastapi uvicorn python-multipart

cat > start_server.sh <<'EOF'
#!/bin/bash
cd "$(dirname "$0")"
export WHISPER_DEVICE=cpu
export WHISPER_COMPUTE_TYPE=int8
export WHISPER_MODEL=${WHISPER_MODEL:-medium}
# caffeinate -s: keeps the system from sleeping while plugged into AC,
# but does NOT force the display on — screen still sleeps normally.
caffeinate -s venv/bin/python faster_whisper_server.py
EOF
chmod +x start_server.sh

echo
echo "Done. Next steps:"
echo "  1. Plug into power (caffeinate -s only works on AC, and you'll want it for sustained CPU load anyway)."
echo "  2. Leave the lid OPEN. Let the display sleep on its own timer - the server keeps running."
echo "  3. Run: ./start_server.sh"
echo "     (first run may prompt 'Allow incoming network connections for python3' - click Allow)"
echo "  4. Find this Mac's IP: System Settings > Wi-Fi (or Network) > Details > IP Address"
echo "  5. From Ubuntu: curl http://<MAC_IP>:8765/health"
echo "  6. Launch a worker pointed at it: venv/bin/python pipelines/no_caption_transcriber/05_transcribe.py --server-url http://<MAC_IP>:8765"
