#!/bin/sh
# Installs Dumi into /Applications:  curl -fsSL https://dumitasks.com/install.sh | sh
# What it does, all of it: downloads the disk image, copies Dumi.app out of it, opens it.
# (A file fetched by curl isn't quarantined, so macOS doesn't stop the not-yet-notarized build.)
set -eu

[ "$(uname -s)" = "Darwin" ] || { echo "Dumi is a macOS app."; exit 1; }
[ "$(uname -m)" = "arm64" ] || { echo "Dumi needs a Mac with Apple silicon."; exit 1; }
major=$(sw_vers -productVersion | cut -d. -f1)
[ "$major" -ge 14 ] || { echo "Dumi needs macOS 14 or later (this is $(sw_vers -productVersion))."; exit 1; }

URL=${DUMI_DMG_URL:-https://dumitasks.com/downloads/Dumi.dmg}
DEST=${DUMI_DEST:-/Applications}
TMP=$(mktemp -d "${TMPDIR:-/tmp}/dumi-install.XXXXXX")
MNT="$TMP/mnt"
cleanup() {
  hdiutil detach "$MNT" -quiet 2>/dev/null || true
  rm -rf "$TMP"
}
trap cleanup EXIT

echo "Downloading Dumi…"
curl -fsSL "$URL" -o "$TMP/Dumi.dmg"
mkdir -p "$MNT"
hdiutil attach "$TMP/Dumi.dmg" -nobrowse -readonly -quiet -mountpoint "$MNT"

# Quit a running copy so it isn't replaced underneath itself; its data file is left alone.
if pgrep -x Dumi >/dev/null 2>&1; then
  osascript -e 'quit app id "com.futitisme.dumi"' >/dev/null 2>&1 || pkill -x Dumi || true
  sleep 1
fi
rm -rf "$DEST/Dumi.app"
ditto "$MNT/Dumi.app" "$DEST/Dumi.app"
xattr -dr com.apple.quarantine "$DEST/Dumi.app" 2>/dev/null || true

open "$DEST/Dumi.app"
echo "Dumi is in $DEST — look at the top of your screen. ⌥Space opens it from anywhere."
