#!/bin/bash
# Subtitler installer. Downloads the app, installs it to /Applications,
# and clears the download quarantine so it opens without a Gatekeeper block.
set -e
DOMAIN="https://subtitler.heliconsolutions.net"
BLUE='\033[1;34m'; GRN='\033[1;32m'; NC='\033[0m'

printf "${BLUE}Subtitler${NC}  installing...\n"

# Apple Silicon + macOS 15 check
if [ "$(uname -m)" != "arm64" ]; then
  echo "This build needs an Apple Silicon Mac (M1 or newer)."; exit 1
fi
if [ "$(sw_vers -productVersion | cut -d. -f1)" -lt 15 ]; then
  echo "Subtitler needs macOS 15 (Sequoia) or newer."; exit 1
fi

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
printf "  downloading app...\n"
curl -fsSL "$DOMAIN/Subtitler.zip" -o "$TMP/Subtitler.zip"

printf "  installing to /Applications...\n"
rm -rf /Applications/Subtitler.app
ditto -x -k "$TMP/Subtitler.zip" /Applications/

xattr -dr com.apple.quarantine /Applications/Subtitler.app 2>/dev/null || true

printf "${GRN}  done.${NC} opening Subtitler...\n"
printf "  (first video downloads the speech model once, about 500 MB,\n"
printf "   and macOS asks to add English + German translation languages)\n"
open /Applications/Subtitler.app

# Anonymous install count (privacy-friendly Plausible, no personal data).
# Best-effort and non-blocking: a short timeout, and failures are ignored.
curl -s -m 5 -X POST https://plausible.io/api/event \
  -H "Content-Type: application/json" \
  -H "User-Agent: Subtitler-Installer/1.0 (macOS; arm64)" \
  --data '{"name":"install","domain":"subtitler.heliconsolutions.net","url":"https://subtitler.heliconsolutions.net/install.sh"}' \
  >/dev/null 2>&1 || true
