#!/usr/bin/env bash
# AI Battery installer — https://aibattery.dev/install.sh
# Generated by scripts/build.js from scripts/app-facts.json; do not edit by hand.
#
# Installs AI Battery 3.1.0 into /Applications without Homebrew:
# checks macOS 13+ on Apple Silicon, downloads the release zip, verifies its SHA-256,
# unpacks it, clears the quarantine flag and launches the app.
#
# Usage:  curl -fsSL https://aibattery.dev/install.sh | bash
#         curl -fsSL https://aibattery.dev/install.sh | bash -s -- --no-launch --dir "$HOME/Applications"
# Exit codes: 0 installed · 2 unsupported system · 3 checksum mismatch · 4 download or unpack failed
# Runbook with the follow-up steps (Gatekeeper, Keychain, sign-in): https://aibattery.dev/install
set -euo pipefail

VERSION="3.1.0"
ZIP_URL="https://github.com/KyleNesium/AIBattery/releases/download/v3.1.0/AIBattery.zip"
ZIP_SHA256="c79702b7eae1f0a4903c21550c7499dc4a15819ed1c79c05fb3a87389688b357"
APP_NAME="AIBattery.app"
PROCESS_NAME="AIBattery"
APP_DIR="/Applications"
LAUNCH=1

log() { printf 'aibattery-install: %s\n' "$*" >&2; }
usage() { sed -n '2,13p' "$0" 2>/dev/null || true; }

while [ $# -gt 0 ]; do
  case "$1" in
    --no-launch) LAUNCH=0 ;;
    --dir) [ $# -ge 2 ] || { log "--dir needs a folder"; exit 1; }; APP_DIR="$2"; shift ;;
    -h|--help) usage; exit 0 ;;
    *) log "unknown option: $1"; exit 1 ;;
  esac
  shift
done

if [ "$(uname -s)" != "Darwin" ]; then
  log "AI Battery is a macOS app (this is $(uname -s))"; exit 2
fi
ARCH="$(uname -m)"
if [ "$ARCH" != "arm64" ]; then
  log "AI Battery ships for Apple Silicon only; this Mac is $ARCH"; exit 2
fi
OS_VERSION="$(sw_vers -productVersion)"
if [ "${OS_VERSION%%.*}" -lt 13 ]; then
  log "macOS 13 (Ventura) or newer is required; this Mac runs $OS_VERSION"; exit 2
fi

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

log "downloading AI Battery $VERSION"
if ! curl -fsSL --retry 3 "$ZIP_URL" -o "$TMP/AIBattery.zip"; then
  log "download failed: $ZIP_URL"; exit 4
fi

ACTUAL_SHA256="$(shasum -a 256 "$TMP/AIBattery.zip" | awk '{print $1}')"
if [ "$ACTUAL_SHA256" != "$ZIP_SHA256" ]; then
  log "checksum mismatch — expected $ZIP_SHA256, got $ACTUAL_SHA256; nothing was installed"; exit 3
fi
log "checksum verified"

mkdir -p "$APP_DIR"
if [ -d "$APP_DIR/$APP_NAME" ]; then
  if pgrep -x "$PROCESS_NAME" >/dev/null 2>&1; then
    log "quitting the running copy"; pkill -x "$PROCESS_NAME" || true; sleep 1
  fi
  rm -rf "$APP_DIR/$APP_NAME"
fi
if ! ditto -x -k "$TMP/AIBattery.zip" "$APP_DIR"; then
  log "unpack failed"; exit 4
fi
[ -d "$APP_DIR/$APP_NAME" ] || { log "unpack produced no $APP_NAME in $APP_DIR"; exit 4; }
xattr -cr "$APP_DIR/$APP_NAME" 2>/dev/null || true

INSTALLED="$(defaults read "$APP_DIR/$APP_NAME/Contents/Info.plist" CFBundleShortVersionString 2>/dev/null || echo "$VERSION")"
if [ "$LAUNCH" -eq 1 ]; then
  open "$APP_DIR/$APP_NAME" || log "could not launch automatically; open $APP_DIR/$APP_NAME yourself"
fi

log "installed AI Battery $INSTALLED to $APP_DIR/$APP_NAME"
log "next, in the UI: if macOS blocks the app, System Settings > Privacy & Security > Open Anyway; click Always Allow on the Keychain prompt; click the star in the menu bar to sign in"
echo "AIBATTERY_INSTALL_RESULT status=ok version=$INSTALLED path=$APP_DIR/$APP_NAME launched=$LAUNCH"
