#!/bin/sh
# ===========================================================================
#  Polaris AI - the macOS installer
# ===========================================================================
#  Run this file and Polaris AI.app is installed in your Applications folder.
#
#  What it does, and nothing else:
#    * builds ~/Applications/Polaris AI.app - a real app bundle
#    * the app opens Polaris AI in its own window: no tabs, no address bar
#    * downloads the app icon and converts it to .icns for the bundle
#
#  Everything it writes lives inside that one bundle. Drag it to the Bin and
#  the app is gone again.
#
#  macOS may refuse to open this file the first time, because it came from the
#  internet: right-click it, choose "Open", then "Open" again. If it still will
#  not run, open Terminal here and type:  chmod +x "polaris-macos-install.command"
# ===========================================================================

set -eu

NAME='Polaris AI'
URL='https://polarisaiapp.com/app/'
ICON='https://polarisaiapp.com/static/favicon.ico'
VERSION='1.0.0'

BUNDLE="$HOME/Applications/$NAME.app"
CONTENTS="$BUNDLE/Contents"

printf '\n  %s - installing\n' "$NAME"
printf '  ----------------------------------------\n'

rm -rf "$BUNDLE"
mkdir -p "$CONTENTS/MacOS" "$CONTENTS/Resources"

# --- the launcher ---------------------------------------------------------
# A browser in app mode is what makes this a window rather than a tab. Safari
# has no such mode, so it is the last resort and opens a normal window.
cat > "$CONTENTS/MacOS/$NAME" <<'LAUNCHER'
#!/bin/sh
URL='https://polarisaiapp.com/app/'

for BROWSER in \
  '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' \
  '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge' \
  '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser' \
  '/Applications/Chromium.app/Contents/MacOS/Chromium'
do
  if [ -x "$BROWSER" ]; then
    exec "$BROWSER" --app="$URL"
  fi
done

exec /usr/bin/open "$URL"
LAUNCHER

chmod +x "$CONTENTS/MacOS/$NAME"

# --- what makes it an application -----------------------------------------
cat > "$CONTENTS/Info.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleName</key><string>Polaris AI</string>
  <key>CFBundleDisplayName</key><string>Polaris AI</string>
  <key>CFBundleIdentifier</key><string>com.polarisai.app</string>
  <key>CFBundleExecutable</key><string>Polaris AI</string>
  <key>CFBundleIconFile</key><string>Polaris</string>
  <key>CFBundlePackageType</key><string>APPL</string>
  <key>CFBundleShortVersionString</key><string>1.0.0</string>
  <key>CFBundleVersion</key><string>1.0.0</string>
  <key>LSMinimumSystemVersion</key><string>10.13</string>
  <key>NSHighResolutionCapable</key><true/>
</dict>
</plist>
PLIST

# --- the icon -------------------------------------------------------------
# `sips` is part of macOS, so nothing has to be installed first.
if curl -fsSL "$ICON" -o "$CONTENTS/Resources/polaris.png"; then
  if sips -s format icns "$CONTENTS/Resources/polaris.png" \
      --out "$CONTENTS/Resources/Polaris.icns" >/dev/null 2>&1; then
    rm -f "$CONTENTS/Resources/polaris.png"
    printf '  icon: installed\n'
  else
    printf '  icon: sips could not convert it; the bundle keeps a default one\n'
  fi
else
  printf '  icon: could not be downloaded; the bundle keeps a default one\n'
fi

# Tell Finder and Launchpad that a new app is here.
/usr/bin/touch "$BUNDLE"
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister \
  -f "$BUNDLE" >/dev/null 2>&1 || true

printf '\n  Done. %s is in your Applications folder.\n' "$NAME"
printf '  Open it from Launchpad, or with:  open "%s"\n\n' "$BUNDLE"
