#!/bin/bash
# Parity public beta installer for macOS.
# Usage: curl -fsSL https://parity-7rw.pages.dev/install.sh | bash
set -euo pipefail

BASE_URL="${PARITY_RELEASE_BASE:-https://parity-7rw.pages.dev}"
VERSION="${PARITY_VERSION:-0.1.0}"
MIN_MACOS=13
INSTALL_DIR="${PARITY_INSTALL_DIR:-$HOME/.local/bin}"
CONFIG_DIR="${PARITY_CONFIG_DIR:-$HOME/.parity}"
OCR_DIR="$CONFIG_DIR/bin"
DRY_RUN=0
INSTALL_OCR=1

usage() {
  cat <<'USAGE'
Parity public beta installer

  curl -fsSL https://parity-7rw.pages.dev/install.sh | bash

Options:
  --install-dir <dir>   Binary location (default: ~/.local/bin)
  --version <version>   Require a specific release (default: 0.1.0)
  --no-ocr              Skip the optional Apple Vision OCR helper
  --dry-run             Print actions without changing anything
  -h, --help            Show this help

Environment: PARITY_INSTALL_DIR, PARITY_CONFIG_DIR, PARITY_RELEASE_BASE
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --install-dir) INSTALL_DIR="${2:?}"; shift 2 ;;
    --version) VERSION="${2:?}"; shift 2 ;;
    --no-ocr) INSTALL_OCR=0; shift ;;
    --dry-run) DRY_RUN=1; shift ;;
    -h|--help) usage; exit 0 ;;
    *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
  esac
done

say() { printf '\033[1;36m›\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m!\033[0m %s\n' "$*"; }
fail() { printf '\033[1;31m✗\033[0m %s\n' "$*" >&2; exit 1; }

[[ "$(uname -s)" == "Darwin" ]] || fail "Parity requires macOS."

MACOS_MAJOR=$(sw_vers -productVersion | cut -d. -f1)
[[ "$MACOS_MAJOR" -ge "$MIN_MACOS" ]] || fail "macOS $MIN_MACOS or newer is required (found $(sw_vers -productVersion))."

case "$(uname -m)" in
  arm64|aarch64) ARCH="aarch64" ;;
  x86_64|amd64) ARCH="x86_64" ;;
  *) fail "Unsupported architecture: $(uname -m)" ;;
esac

ARCHIVE="parity-$VERSION-$ARCH-apple-darwin.tar.gz"
ARCHIVE_URL="$BASE_URL/download/$ARCHIVE"
CHECKSUM_URL="$ARCHIVE_URL.sha256"

say "Parity $VERSION · macOS $(sw_vers -productVersion) · $ARCH"
say "Installing into $INSTALL_DIR"

if [[ "$DRY_RUN" == "1" ]]; then
  say "Dry run: would download $ARCHIVE_URL"
  if [[ "$INSTALL_OCR" == "1" ]]; then
    say "Dry run: would verify SHA-256 and install parity + parity-ocr"
  else
    say "Dry run: would verify SHA-256 and install parity"
  fi
  exit 0
fi

TMP=$(mktemp -d "${TMPDIR:-/tmp}/parity-install.XXXXXX")
cleanup() { rm -rf "$TMP"; }
trap cleanup EXIT

PROTO="=https"
case "$BASE_URL" in http://*) PROTO="=https,http" ;; esac

say "Downloading $ARCHIVE"
curl -fL --proto "$PROTO" --tlsv1.2 --retry 3 --progress-bar "$ARCHIVE_URL" -o "$TMP/$ARCHIVE" \
  || fail "Download failed: $ARCHIVE_URL"

curl -fsSL --proto "$PROTO" --tlsv1.2 --retry 3 "$CHECKSUM_URL" -o "$TMP/$ARCHIVE.sha256" \
  || fail "Could not download the checksum."

EXPECTED=$(awk '{print $1}' "$TMP/$ARCHIVE.sha256")
ACTUAL=$(shasum -a 256 "$TMP/$ARCHIVE" | awk '{print $1}')
[[ -n "$EXPECTED" && "$EXPECTED" == "$ACTUAL" ]] || fail "Checksum mismatch. The download may be corrupted."
say "Checksum verified"

tar -xzf "$TMP/$ARCHIVE" -C "$TMP"
[[ -x "$TMP/parity" ]] || fail "The archive does not contain a parity binary."

if command -v codesign >/dev/null 2>&1; then
  if codesign --verify --strict "$TMP/parity" >/dev/null 2>&1; then
    say "Developer ID signature verified"
  else
    warn "The binary is not signed by a recognized Developer ID."
  fi
fi

mkdir -p "$INSTALL_DIR"
install -m 0755 "$TMP/parity" "$INSTALL_DIR/parity"
say "Installed $INSTALL_DIR/parity"

if [[ "$INSTALL_OCR" == "1" && -x "$TMP/parity-ocr" ]]; then
  mkdir -p "$OCR_DIR"
  install -m 0755 "$TMP/parity-ocr" "$OCR_DIR/parity-ocr"
  say "Installed $OCR_DIR/parity-ocr"
fi

if ! command -v parity >/dev/null 2>&1; then
  warn "$INSTALL_DIR is not on your PATH."
  printf '  Add this to your shell profile:\n\n    export PATH="%s:$PATH"\n\n' "$INSTALL_DIR"
fi

cat <<EOF

Parity is installed. Next:
  1. Point it at a Messages API-compatible model endpoint.
  2. Run: parity chat

  export PARITY_BASE_URL="http://localhost:8081"
  export PARITY_AUTH_TOKEN="your-token"
  export PARITY_MODEL="qwen3.8-flash-next-q2_0"
  export PARITY_MAX_CONTEXT_TOKENS="131072"

This is beta software. It can run shell commands and edit files. Keep backups.
Feedback: mailto:nicolas@holographics.co?subject=Parity%20beta
EOF
