#!/bin/bash
#
# Doubleslash installer for macOS
# Usage:  curl -fsSL https://doubleslash.tafil.app/install.sh | bash
#
# What this does (and nothing more):
#   1. Downloads the official Doubleslash.dmg from doubleslash.tafil.app
#   2. Verifies the download is a valid disk image
#   3. Copies Doubleslash.app into /Applications
#   4. Clears the quarantine flag so Gatekeeper doesn't block the
#      (ad-hoc signed, indie) build — no data ever leaves your Mac
#   5. Launches the app
#
# The full source of this script is readable at:
#   https://doubleslash.tafil.app/install.sh

set -euo pipefail

DMG_URL="https://doubleslash.tafil.app/download/Doubleslash.dmg"
APP_NAME="Doubleslash.app"
DEST="/Applications/${APP_NAME}"
TMP_DIR="$(mktemp -d /tmp/doubleslash-install.XXXXXX)"
DMG_PATH="${TMP_DIR}/Doubleslash.dmg"
MOUNT_POINT="${TMP_DIR}/mnt"

cleanup() {
    if [ -d "${MOUNT_POINT}" ]; then
        hdiutil detach "${MOUNT_POINT}" -quiet >/dev/null 2>&1 || true
    fi
    rm -rf "${TMP_DIR}"
}
trap cleanup EXIT

bold()  { printf '\033[1m%s\033[0m\n' "$*"; }
info()  { printf '  \033[36m•\033[0m %s\n' "$*"; }
ok()    { printf '  \033[32m✓\033[0m %s\n' "$*"; }
fail()  { printf '  \033[31m✗ %s\033[0m\n' "$*" >&2; exit 1; }

echo ""
bold "Doubleslash installer"
echo ""

# 0. Sanity: macOS only, 13.0+
[ "$(uname)" = "Darwin" ] || fail "This installer is for macOS. Windows/Linux builds: https://doubleslash.tafil.app/#download"
MACOS_MAJOR="$(sw_vers -productVersion | cut -d. -f1)"
if [ "${MACOS_MAJOR}" -lt 13 ]; then
    fail "Doubleslash requires macOS 13 (Ventura) or later. You are on $(sw_vers -productVersion)."
fi

# 1. Download
info "Downloading Doubleslash.dmg…"
curl -fSL --progress-bar "${DMG_URL}" -o "${DMG_PATH}" || fail "Download failed. Check your connection and try again."

# 2. Verify it's a disk image before mounting
hdiutil imageinfo "${DMG_PATH}" >/dev/null 2>&1 || fail "Downloaded file is not a valid disk image. Please retry."
ok "Download verified"

# 3. Mount and copy
info "Installing to /Applications…"
mkdir -p "${MOUNT_POINT}"
hdiutil attach "${DMG_PATH}" -mountpoint "${MOUNT_POINT}" -nobrowse -quiet || fail "Could not mount the disk image."
[ -d "${MOUNT_POINT}/${APP_NAME}" ] || fail "Doubleslash.app not found inside the disk image."

# Quit a running copy before replacing it
osascript -e 'tell application "Doubleslash" to quit' >/dev/null 2>&1 || true
sleep 1

rm -rf "${DEST}"
ditto "${MOUNT_POINT}/${APP_NAME}" "${DEST}" || fail "Could not copy to /Applications (permission denied?)."
hdiutil detach "${MOUNT_POINT}" -quiet || true
ok "Installed to /Applications"

# 4. Clear quarantine so Gatekeeper doesn't block the indie build.
#    (Downloads made by curl usually never get the flag; this covers all cases.)
xattr -dr com.apple.quarantine "${DEST}" 2>/dev/null || true
ok "Gatekeeper quarantine cleared"

# 5. Launch
open "${DEST}"
echo ""
bold "Done. Doubleslash is running in your menu bar."
echo ""
echo "  Next steps:"
echo "    1. Grant Accessibility permission when prompted"
echo "       (System Settings → Privacy & Security → Accessibility)"
echo "    2. Type //note followed by a space in any app to capture your first thought"
echo ""
echo "  Uninstall anytime:  rm -rf /Applications/Doubleslash.app"
echo ""
