#!/bin/bash
# Installs NotchTasks into /Applications and opens it:  curl -fsSL https://notchtasks.web.app/install.sh | bash
set -euo pipefail

URL="${NOTCHTASKS_DMG:-https://notchtasks.web.app/assets/NotchTasks.dmg}"
APP=/Applications/NotchTasks.app

# Linux: the tar.gz for this CPU is unpacked into ~/.local/share/notchtasks/app and started by ~/.local/bin/notchtasks.
linux_install() {
  case "$(uname -m)" in
    x86_64) arch=x64 ;;
    aarch64 | arm64) arch=arm64 ;;
    *) echo "NotchTasks has no Linux build for $(uname -m)." >&2; exit 1 ;;
  esac
  base="$HOME/.local/share/notchtasks"
  app="$base/app"
  bin="$HOME/.local/bin/notchtasks"
  work=$(mktemp -d)
  trap 'rm -rf "$work" "$base/app.new"' EXIT

  echo "Downloading NotchTasks…"
  feed=$(curl -fsSL https://notchtasks.web.app/version-linux.json)
  # the feed is JSON with an entry per arch; python3 reads it, sed is the fallback (the feed is ours, one entry per line)
  if command -v python3 >/dev/null 2>&1; then
    info=$(printf '%s' "$feed" | python3 -I -c 'import json,sys; a=json.load(sys.stdin)[sys.argv[1]]; print(a["url"]); print(a["sha256"])' "$arch") || info=
  else
    info=$(printf '%s' "$feed" | tr -d ' \n' | sed -n "s/.*\"$arch\":{\"url\":\"\([^\"]*\)\",\"sha256\":\"\([0-9a-f]*\)\".*/\1\n\2/p")
  fi
  url=$(sed -n 1p <<< "$info")
  sha=$(sed -n 2p <<< "$info")
  case "$url" in https://*) ;; *) echo "The update feed has no download for $arch." >&2; exit 1 ;; esac
  printf '%s' "$sha" | grep -Eq '^[0-9a-f]{64}$' || { echo "The update feed has no checksum for $arch." >&2; exit 1; }

  curl -fL --progress-bar "$url" -o "$work/NotchTasks.tar.gz"
  printf '%s  %s\n' "$sha" "$work/NotchTasks.tar.gz" | sha256sum -c --quiet - || { echo "The download does not match its checksum." >&2; exit 1; }

  # unpacked beside the old copy (same folder, so the moves below are renames), then swapped in
  mkdir -p "$base"
  rm -rf "$base/app.new"
  mkdir "$base/app.new"
  tar -xzf "$work/NotchTasks.tar.gz" -C "$base/app.new" --strip-components=1

  # a running copy is replaced: stop it first, so the new one is the one that starts
  pkill -f "$app/notchtasks" 2>/dev/null || true
  sleep 1
  rm -rf "$base/app.old"
  if [ -d "$app" ]; then mv "$app" "$base/app.old"; fi
  mv "$base/app.new" "$app" || { [ -d "$base/app.old" ] && mv "$base/app.old" "$app"; exit 1; }
  rm -rf "$base/app.old"

  # --no-sandbox: a tar.gz install has no setuid chrome-sandbox, and Ubuntu 24.04 blocks the unprivileged user
  # namespaces Chromium would use instead, so its sandbox cannot start here
  mkdir -p "$HOME/.local/bin"
  cat > "$bin" <<EOF
#!/bin/sh
exec "$app/notchtasks" --no-sandbox "\$@"
EOF
  chmod +x "$bin"

  # the menu entry and icon; the icon is unpacked on disk by electron-builder (asarUnpack)
  icon="$HOME/.local/share/icons/hicolor/256x256/apps/notchtasks.png"
  mkdir -p "$(dirname "$icon")" "$HOME/.local/share/applications"
  cp "$app/resources/app.asar.unpacked/assets/icon.png" "$icon"
  cat > "$HOME/.local/share/applications/notchtasks.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=NotchTasks
Comment=Your AI agents, in the notch
Exec=$bin
Icon=notchtasks
Terminal=false
Categories=Development;
StartupWMClass=NotchTasks
EOF

  nohup "$bin" >/dev/null 2>&1 </dev/null &
  echo "NotchTasks is installed. Look at the top of your screen."
}

if [ "$(uname)" = Linux ]; then
  linux_install
  exit 0
fi

[ "$(uname)" = Darwin ] || { echo "NotchTasks runs on macOS." >&2; exit 1; }
major=$(sw_vers -productVersion | cut -d. -f1)
[ "$major" -ge 14 ] || { echo "NotchTasks needs macOS 14 or later (this is $(sw_vers -productVersion))." >&2; exit 1; }

work=$(mktemp -d)
cleanup() { hdiutil detach "$work/mnt" -quiet 2>/dev/null || true; rm -rf "$work"; }
trap cleanup EXIT

echo "Downloading NotchTasks…"
curl -fL --progress-bar "$URL" -o "$work/NotchTasks.dmg"
hdiutil attach "$work/NotchTasks.dmg" -nobrowse -quiet -mountpoint "$work/mnt"
[ -d "$work/mnt/NotchTasks.app" ] || { echo "The download is not a NotchTasks disk image." >&2; exit 1; }

# a running copy is replaced: ask it to quit first, so the new one is the one that opens
osascript -e 'quit app "NotchTasks"' 2>/dev/null || true
# the new copy goes in beside the old one and takes its place only when complete: a failed copy leaves the old app working
ditto "$work/mnt/NotchTasks.app" "$APP.new"
rm -rf "$APP"
mv "$APP.new" "$APP"

# where the visitor came from (the site adds NT_SRC when its link had ?src=); kept for the app's first launch only
src=$(printf '%s' "${NT_SRC:-}" | tr 'A-Z' 'a-z' | tr -cd 'a-z0-9_-' | cut -c1-20)
if [ -n "$src" ] && ! defaults read com.notchtasks.mac install.src >/dev/null 2>&1; then
  defaults write com.notchtasks.mac install.src "$src"
fi

open "$APP"
echo "NotchTasks is installed. Look at the top of your screen."
