#!/usr/bin/env bash
set -euo pipefail

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

BUN="${BUN:-}"
if [[ -z "$BUN" ]]; then
  if command -v bun &>/dev/null; then
    BUN="bun"
  elif [[ -x "$HOME/.bun/bin/bun" ]]; then
    BUN="$HOME/.bun/bin/bun"
  else
    echo "error: bun not found. Install from https://bun.sh or set BUN=/path/to/bun" >&2
    exit 1
  fi
fi

echo "Using bun: $("$BUN" --version) ($BUN)"

cd "$FRONTEND_DIR"

if [[ ! -d node_modules ]]; then
  echo "Installing dependencies..."
  "$BUN" install
fi

echo "Building frontend..."
"$BUN" run build

echo "Done. Output: $FRONTEND_DIR/dist/"
