#!/bin/bash
# Installs Marquee into /Applications.
#
#   curl -fsSL https://marquee.franklinokolie.com/install.sh | bash
#
# What it does, step by step:
#   1. Checks you're on macOS 15 or later and picks the build for your Mac's chip.
#   2. Downloads that build's disk image from this site and checks its SHA-256 against release.json.
#   3. Quits Marquee if it's running, and copies Marquee.app into /Applications (replacing an older copy).
#   4. Removes the download "quarantine" flag from Marquee.app only, so macOS doesn't block it on first
#      launch. (Marquee isn't notarized by Apple yet.) Nothing else on your Mac is changed.
#   5. Opens Marquee.
#
# Everything runs inside main(), so a partly downloaded script does nothing.
set -euo pipefail

SITE="https://marquee.franklinokolie.com"
APP="/Applications/Marquee.app"
# Global, so the cleanup on exit can still see them once main() has returned.
TMP=""
MOUNT=""

bold() { printf '\033[1m%s\033[0m\n' "$*"; }
fail() { printf '\033[31mError:\033[0m %s\n' "$*" >&2; exit 1; }

main() {
  [ "$(uname -s)" = "Darwin" ] || fail "Marquee is a macOS app."

  TMP="$(mktemp -d)"
  trap '[ -n "$MOUNT" ] && hdiutil detach -quiet "$MOUNT" 2>/dev/null; rm -rf "$TMP"' EXIT

  bold "Checking the latest release…"
  curl -fsSL "$SITE/release.json" -o "$TMP/release.json" || fail "Couldn't reach $SITE."
  local version min_macos
  version="$(plutil -extract version raw -o - "$TMP/release.json")"
  min_macos="$(plutil -extract minimumMacOS raw -o - "$TMP/release.json")"

  local have_major need_major
  have_major="$(sw_vers -productVersion | cut -d. -f1)"
  need_major="${min_macos%%.*}"
  [ "$have_major" -ge "$need_major" ] || fail "Marquee needs macOS $need_major or later (this Mac has $(sw_vers -productVersion))."

  # Apple silicon, even if this shell is running under Rosetta.
  local arch="x64"
  if [ "$(uname -m)" = "arm64" ] || [ "$(sysctl -in sysctl.proc_translated 2>/dev/null)" = "1" ]; then arch="arm64"; fi
  local file sha256
  file="$(plutil -extract "files.$arch.file" raw -o - "$TMP/release.json")"
  sha256="$(plutil -extract "files.$arch.sha256" raw -o - "$TMP/release.json")"

  bold "Downloading Marquee $version for $([ "$arch" = arm64 ] && echo "Apple silicon" || echo Intel)…"
  curl -fL --progress-bar "$SITE/download/$file" -o "$TMP/$file" || fail "The download failed."
  [ "$(shasum -a 256 "$TMP/$file" | awk '{print $1}')" = "$sha256" ] || fail "The download doesn't match its checksum. Try again."

  MOUNT="$(hdiutil attach -nobrowse -readonly -noautoopen "$TMP/$file" | awk -F'\t' '/\/Volumes\//{print $NF}')"
  [ -d "$MOUNT/Marquee.app" ] || fail "Marquee.app wasn't found in the disk image."

  if pgrep -xq Marquee; then
    bold "Quitting the running Marquee…"
    osascript -e 'quit app "Marquee"' >/dev/null 2>&1 || true
    sleep 1
    pkill -x Marquee 2>/dev/null || true
  fi

  bold "Installing to /Applications…"
  # Admin accounts can write to /Applications; anyone else is asked for an admin password.
  local sudo=""
  [ -w /Applications ] || sudo="sudo"
  $sudo rm -rf "$APP"
  $sudo ditto "$MOUNT/Marquee.app" "$APP"
  $sudo xattr -dr com.apple.quarantine "$APP" 2>/dev/null || true

  bold "Done. Opening Marquee…"
  open "$APP"
  echo "Look for Marquee in your menu bar. When macOS asks, allow Accessibility access."
}

main "$@"
