#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
BROWSERPILOT_INSTALL_URL="https://browserpilot.seedsource.dev/install"
# Ensure execute permissions (may be missing if archive was built on Windows)
chmod +x bin/browserpilot-installer 2>/dev/null || true
chmod +x bin/browserpilot-core 2>/dev/null || true
if [[ -x "bin/browserpilot-installer" ]]; then
  exec "bin/browserpilot-installer" --register-native-host --ensure-launcher --install-url "$BROWSERPILOT_INSTALL_URL"
fi
if [[ -x "bin/browserpilot-core" ]]; then
  "bin/browserpilot-core" install-native-host
  echo "BrowserPilot native host registered through the compiled local core."
  echo "Continue the signed install or update flow at $BROWSERPILOT_INSTALL_URL"
  exit 0
fi
if [[ ! -x "bin/browserpilot-core" ]]; then
  echo "BrowserPilot installer scaffold requires a compiled local core binary at bin/browserpilot-core."
  exit 1
fi
