#!/bin/sh
# Lovely Screenshots installer — https://lovelyscreenshots.com
# Downloads the latest release, installs to /Applications, clears the
# quarantine flag (the build is code-signed but not notarized — indie beta),
# and launches the app. Source: https://github.com/kusmiderdev/lovely-screenshots-releases
set -e

REPO="kusmiderdev/lovely-screenshots-releases"
APPDIR="${LOVELY_APPDIR:-/Applications}"
APP="$APPDIR/Lovely Screenshots.app"

echo "→ Finding the latest Lovely Screenshots release…"
URL=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
  | grep -o '"browser_download_url": *"[^"]*\.zip"' | head -1 | cut -d'"' -f4)
if [ -z "$URL" ]; then
  echo "✗ Could not find a release. Grab it manually: https://github.com/$REPO/releases"
  exit 1
fi

TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
echo "→ Downloading $(basename "$URL")…"
curl -fL --progress-bar "$URL" -o "$TMP/app.zip"

echo "→ Installing to $APPDIR…"
ditto -xk "$TMP/app.zip" "$TMP/unpacked"
mkdir -p "$APPDIR"
rm -rf "$APP"
mv "$TMP/unpacked/Lovely Screenshots.app" "$APP"

# One-time: the beta is signed with a stable developer cert but not notarized
# by Apple, so Gatekeeper would show a scary prompt. You just asked for this
# install explicitly — clearing quarantine skips the theater.
xattr -cr "$APP" 2>/dev/null || true

echo "→ Launching…"
open "$APP"
echo ""
echo "✓ Lovely Screenshots installed — look for the camera in your menu bar."
echo "  Capture area: ⌘⇧4 · all modes: ⌘⇧5 · it's free, Pro unlocks extras."
echo "  Ideas? https://lovelyscreenshots.com/requests"
