#!/bin/sh
# ===========================================================================
#  Polaris AI - the Linux installer
# ===========================================================================
#  Run this file and Polaris AI appears in your applications menu.
#
#  What it does, and nothing else:
#    * writes ~/.local/bin/polaris-ai                  (the launcher)
#    * writes ~/.local/share/applications/polaris-ai.desktop   (menu entry)
#    * downloads the app icon into ~/.local/share/icons
#    * adds a copy of the entry to your Desktop, if you have one
#
#  Everything it writes is inside ~/.local, plus that one desktop file. Remove
#  them and the app is gone again.
#
#  Run it with:  sh polaris-linux-install.sh
# ===========================================================================

set -eu

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

BIN_DIR="$HOME/.local/bin"
APPS_DIR="$HOME/.local/share/applications"
ICON_DIR="$HOME/.local/share/icons/hicolor/512x512/apps"
LAUNCHER="$BIN_DIR/polaris-ai"
ICON_PATH="$ICON_DIR/polaris-ai.png"
ENTRY="$APPS_DIR/polaris-ai.desktop"

# curl is on most systems and wget on most of the rest, so both are tried
# before giving up on the icon - which is the only thing downloaded.
download() {
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$1" -o "$2"
  elif command -v wget >/dev/null 2>&1; then
    wget -qO "$2" "$1"
  else
    return 1
  fi
}

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

mkdir -p "$BIN_DIR" "$APPS_DIR" "$ICON_DIR"

# --- the launcher ---------------------------------------------------------
# A browser in app mode is what makes this a window rather than a tab. The list
# covers the usual Chromium packages; anything else falls back to xdg-open.
cat > "$LAUNCHER" <<'LAUNCHER'
#!/bin/sh
URL='https://polarisaiapp.com/app/'

for BROWSER in \
  google-chrome google-chrome-stable chromium chromium-browser \
  microsoft-edge microsoft-edge-stable brave-browser vivaldi
do
  if command -v "$BROWSER" >/dev/null 2>&1; then
    exec "$BROWSER" --app="$URL"
  fi
done

exec xdg-open "$URL"
LAUNCHER

chmod +x "$LAUNCHER"
printf '  installed: %s\n' "$LAUNCHER"

# --- the icon -------------------------------------------------------------
if download "$ICON" "$ICON_PATH"; then
  printf '  installed: %s\n' "$ICON_PATH"
else
  rm -f "$ICON_PATH"
  printf '  icon: could not be downloaded; the menu will show a default one\n'
fi

# --- the menu entry -------------------------------------------------------
cat > "$ENTRY" <<DESKTOP
[Desktop Entry]
Type=Application
Version=1.0
Name=$NAME
Comment=Your private AI assistant
Exec=$LAUNCHER
Icon=polaris-ai
Terminal=false
Categories=Utility;Network;Office;
StartupNotify=true
StartupWMClass=$NAME
DESKTOP

chmod +x "$ENTRY"
printf '  installed: %s\n' "$ENTRY"

# --- a copy on the Desktop, when there is one -----------------------------
DESKTOP_DIR="$(xdg-user-dir DESKTOP 2>/dev/null || printf '%s' "$HOME/Desktop")"

if [ -n "$DESKTOP_DIR" ] && [ -d "$DESKTOP_DIR" ]; then
  cp "$ENTRY" "$DESKTOP_DIR/polaris-ai.desktop"
  chmod +x "$DESKTOP_DIR/polaris-ai.desktop"
  # GNOME only launches a desktop file it has been told to trust.
  if command -v gio >/dev/null 2>&1; then
    gio set "$DESKTOP_DIR/polaris-ai.desktop" metadata::trusted true \
      >/dev/null 2>&1 || true
  fi
  printf '  installed: %s\n' "$DESKTOP_DIR/polaris-ai.desktop"
fi

# --- let the menu find it without a re-login ------------------------------
if command -v update-desktop-database >/dev/null 2>&1; then
  update-desktop-database "$APPS_DIR" >/dev/null 2>&1 || true
fi

printf '\n  Done. %s is in your applications menu.\n' "$NAME"
case ":$PATH:" in
  *":$BIN_DIR:"*) printf '  Or run it with:  %s\n\n' "$LAUNCHER" ;;
  *) printf '  Add %s to PATH to run it from a terminal.\n\n' "$BIN_DIR" ;;
esac
