#!/usr/bin/env bash
# Build script for RemoteLink Agent
# Run on the target platform (Windows/macOS/Linux)
#
# Requirements: Python 3.11+, pip, pyinstaller, (Windows) NSIS

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

echo "==> Installing dependencies…"
pip install -r requirements.txt
pip install pyinstaller

echo "==> Building with PyInstaller…"
pyinstaller agent.spec --clean --noconfirm

echo "==> Portable binary: dist/remotelink-agent"
ls -lh dist/remotelink-agent* 2>/dev/null || ls -lh dist/

# Windows: build NSIS installer
if [[ "$OSTYPE" == "msys"* ]] || [[ "$OS" == "Windows_NT" ]]; then
  if command -v makensis &>/dev/null; then
    echo "==> Building NSIS installer…"
    makensis installer.nsi
    echo "==> Installer: RemoteLink-Setup.exe"
  else
    echo "==> NSIS not found — skipping installer build."
    echo "    Download NSIS from https://nsis.sourceforge.io"
  fi
fi

echo "==> Done."
