#!/usr/bin/env bash
# install.sh — Set up Powernet and optionally build a standalone executable.
#
# Usage:
#   ./install.sh          # install dependencies only (runs with uv)
#   ./install.sh --build  # install deps + build a PyInstaller .app/.bin

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "=== Powernet installer ==="

# ── 1. Ensure uv is available ──────────────────────────────────────────────
if ! command -v uv &>/dev/null; then
    echo "[*] uv not found — installing via pip..."
    pip install --quiet uv
fi

# ── 2. Create venv + install dependencies ─────────────────────────────────
echo "[*] Installing dependencies..."
uv sync

echo "[*] Dependencies installed."

# ── 3. Run the game (default) ─────────────────────────────────────────────
if [[ "$1" != "--build" ]]; then
    echo ""
    echo "To run the game:"
    echo "  uv run python src/main.py"
    echo ""
    echo "To build a standalone executable:"
    echo "  ./install.sh --build"
    exit 0
fi

# ── 4. Build standalone executable with PyInstaller ───────────────────────
echo "[*] Installing PyInstaller..."
uv pip install pyinstaller

echo "[*] Building standalone executable..."

uv run pyinstaller \
    --noconfirm \
    --onedir \
    --windowed \
    --name "Powernet" \
    --add-data "src/assets:assets" \
    --paths "src" \
    "src/main.py"

echo ""
echo "=== Build complete ==="
echo "Executable: dist/Powernet/Powernet"
echo ""
