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

# Opens Orcha in a dedicated desktop window (Chrome/Edge/Chromium app mode).
# The company machine stays in the cloud. Closing this window does not stop it.

APP_URL="${ORCHA_URL:-https://app.orcha-e33ff1.cofounder.company/os}"

open_app() {
  local bin="$1"
  exec "$bin" --app="$APP_URL" --new-window
}

if command -v google-chrome >/dev/null 2>&1; then
  open_app "google-chrome"
elif command -v google-chrome-stable >/dev/null 2>&1; then
  open_app "google-chrome-stable"
elif command -v chromium >/dev/null 2>&1; then
  open_app "chromium"
elif command -v chromium-browser >/dev/null 2>&1; then
  open_app "chromium-browser"
elif command -v microsoft-edge >/dev/null 2>&1; then
  open_app "microsoft-edge"
elif [ -x "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
  open_app "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
elif [ -x "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" ]; then
  open_app "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"
elif command -v open >/dev/null 2>&1; then
  open -n -a "Safari" "$APP_URL"
else
  echo "Open this URL in your browser, then use Install app / Add to Dock:"
  echo "$APP_URL"
  exit 1
fi
