#!/bin/sh
# doq host installer — downloads the latest release from get.doq.computer and
# installs it as a login-persistent menu-bar (macOS) / tray (Linux) app.
#
#   curl -fsSL https://get.doq.computer | sh
#
# This script is itself served at https://get.doq.computer/ (the root route),
# alongside the per-platform archives, by the doq-get Worker (../get-worker)
# backed by the private `doq-releases` R2 bucket — so it works without the source
# repo being public.
#
# The macOS app is ad-hoc signed (not notarized). curl-downloaded files aren't
# quarantined, so it launches without the Gatekeeper prompt and asks for Screen
# Recording + Accessibility as "doq" — exactly like a local dev build. See
# docs/auth.md. Re-running upgrades in place; an existing config.json is kept.
set -eu

BASE="${DOQ_BASE:-https://get.doq.computer}"
RELAY="https://relay.doq.computer:443"

OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
  Darwin) ASSET="doq-macos-latest.zip" ;;
  Linux)
    case "$ARCH" in
      x86_64 | amd64) ASSET="doq-linux-x86_64-latest.tar.gz" ;;
      *) echo "doq: unsupported Linux arch: $ARCH" >&2; exit 1 ;;
    esac ;;
  *) echo "doq: unsupported OS: $OS" >&2; exit 1 ;;
esac

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
echo "==> Downloading $ASSET..."
curl -fsSL "$BASE/$ASSET" -o "$TMP/dl" \
  || { echo "doq: download failed ($BASE/$ASSET) — has a release been published yet?" >&2; exit 1; }

write_config() { # $1 = config dir
  mkdir -p "$1"
  [ -f "$1/config.json" ] || printf '{\n  "relay": "%s"\n}\n' "$RELAY" > "$1/config.json"
}

if [ "$OS" = Darwin ]; then
  APP="$HOME/Applications/doq.app"
  echo "==> Installing $APP..."
  mkdir -p "$HOME/Applications"
  rm -rf "$APP"
  ditto -x -k "$TMP/dl" "$TMP/x"
  cp -R "$TMP/x/doq.app" "$APP"
  xattr -dr com.apple.quarantine "$APP" 2>/dev/null || true

  write_config "$HOME/Library/Application Support/doq"

  AGENT="$HOME/Library/LaunchAgents/com.desktop-moq.host.plist"
  mkdir -p "$HOME/Library/LaunchAgents" "$HOME/Library/Logs"
  cat > "$AGENT" <<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>Label</key><string>com.desktop-moq.host</string>
  <key>ProgramArguments</key><array><string>$HOME/Applications/doq.app/Contents/MacOS/host</string></array>
  <key>RunAtLoad</key><true/>
  <key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>
  <key>EnvironmentVariables</key><dict><key>RUST_LOG</key><string>info</string></dict>
  <key>StandardOutPath</key><string>$HOME/Library/Logs/doq-host.log</string>
  <key>StandardErrorPath</key><string>$HOME/Library/Logs/doq-host.log</string>
  <key>ProcessType</key><string>Interactive</string>
</dict>
</plist>
PLIST
  U="$(id -u)"
  launchctl bootout "gui/$U/com.desktop-moq.host" 2>/dev/null || true
  launchctl bootstrap "gui/$U" "$AGENT"

  echo ""
  echo "doq installed — a monitor icon should appear in the menu bar."
  echo "Approve the Screen Recording + Accessibility prompts (shown as \"doq\")."
  echo "Then sign in at https://my.doq.computer and Claim This Machine from the tray."
else
  BIN="$HOME/.local/bin/doq-host"
  echo "==> Installing $BIN..."
  tar -xzf "$TMP/dl" -C "$TMP"
  install -Dm755 "$TMP/host" "$BIN"
  [ -f "$TMP/doq-icon-256.png" ] && \
    install -Dm644 "$TMP/doq-icon-256.png" "$HOME/.local/share/icons/hicolor/256x256/apps/doq.png"
  if [ -f "$TMP/doq-host.desktop.in" ]; then
    mkdir -p "$HOME/.local/share/applications"
    sed "s|@BIN@|$BIN|g" "$TMP/doq-host.desktop.in" > "$HOME/.local/share/applications/doq-host.desktop"
    update-desktop-database "$HOME/.local/share/applications" 2>/dev/null || true
  fi

  write_config "${XDG_CONFIG_HOME:-$HOME/.config}/doq"

  if [ -f "$TMP/doq-host.service" ]; then
    install -Dm644 "$TMP/doq-host.service" "$HOME/.config/systemd/user/doq-host.service"
    systemctl --user daemon-reload 2>/dev/null || true
    systemctl --user enable --now doq-host.service 2>/dev/null || true
  fi

  echo ""
  echo "doq host installed and running."
  echo "  binary: $BIN"
  echo "  status: systemctl --user status doq-host   logs: journalctl --user -u doq-host -f"
  echo "Sign in at https://my.doq.computer and Claim This Machine from the tray."
fi
