#!/usr/bin/env bash
# ═══════════════════════════════════════════════════════════════
#  PastCut Relay — macOS installer  (double-click to run)
# ═══════════════════════════════════════════════════════════════
#
#  What it does:
#    1. Detects your Mac architecture (Apple Silicon / Intel)
#    2. Installs Homebrew + ffmpeg if missing
#    3. Downloads the relay binary from pastcut.ru
#    4. Removes macOS quarantine flag
#    5. Starts the relay immediately
#
#  After install, just run:   pastcut-relay
#  Or double-click this file again.
# ═══════════════════════════════════════════════════════════════
set -euo pipefail

RELAY_BASE="https://pastcut.ru/relay"
DEST_DIR="$HOME/.local/bin"
DEST="$DEST_DIR/pastcut-relay"

# ── Pretty output ──────────────────────────────────────────────
bold()  { printf '\033[1m%s\033[0m\n' "$*"; }
info()  { printf '  %s\n' "$*"; }
ok()    { printf '\033[32m  ✓\033[0m %s\n' "$*"; }
warn()  { printf '\033[33m  ⚠\033[0m %s\n' "$*"; }
fail()  { printf '\033[31m  ✗\033[0m %s\n' "$*" >&2; }
header() {
  echo
  printf '\033[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m\n'
  bold "  $*"
  printf '\033[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m\n'
  echo
}

# ── macOS only ─────────────────────────────────────────────────
if [[ "$(uname -s)" != "Darwin" ]]; then
  fail "This installer is for macOS only."
  fail "Download the correct binary at https://pastcut.ru/relay"
  read -rp "Press Enter to close…"
  exit 1
fi

ARCH="$(uname -m)"
case "$ARCH" in
  arm64)   BIN_NAME="pastcut-relay-macos-arm64"; ARCH_LABEL="Apple Silicon (M1/M2/M3/M4)" ;;
  x86_64)  BIN_NAME="pastcut-relay-macos-x64";   ARCH_LABEL="Intel" ;;
  *)       fail "Unsupported Mac architecture: $ARCH"; exit 1 ;;
esac

header "PastCut Relay Installer"
info "Architecture: $ARCH_LABEL"
echo

# ── Step 1: Homebrew ───────────────────────────────────────────
bold "Step 1/4 · Checking Homebrew…"
if ! command -v brew >/dev/null 2>&1; then
  warn "Homebrew not found — installing (you may be asked for your password)…"
  NONINTERACTIVE=1 /bin/bash -c \
    "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  if [[ -x /opt/homebrew/bin/brew ]]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
  elif [[ -x /usr/local/bin/brew ]]; then
    eval "$(/usr/local/bin/brew shellenv)"
  fi
fi
ok "Homebrew: $(brew --version | head -1)"

# ── Step 2: ffmpeg ─────────────────────────────────────────────
bold "Step 2/4 · Checking ffmpeg…"
if ! command -v ffmpeg >/dev/null 2>&1; then
  warn "ffmpeg not found — installing via brew (one-time, ~2 min)…"
  brew install ffmpeg
fi
ok "ffmpeg: $(ffmpeg -version 2>&1 | head -1)"

# ── Step 3: Download relay ─────────────────────────────────────
bold "Step 3/4 · Downloading PastCut Relay…"
mkdir -p "$DEST_DIR"

if [[ -f "$DEST" ]]; then
  info "Updating existing install at $DEST"
fi

curl -fL --progress-bar "$RELAY_BASE/$BIN_NAME" -o "$DEST"
chmod +x "$DEST"
xattr -d com.apple.quarantine "$DEST" 2>/dev/null || true
ok "Installed to $DEST"

# ── Step 4: PATH ───────────────────────────────────────────────
bold "Step 4/4 · Checking PATH…"
if ! echo ":$PATH:" | grep -q ":$DEST_DIR:"; then
  # Add to shell profile
  SHELL_RC="$HOME/.zshrc"
  [[ "$(basename "$SHELL")" == "bash" ]] && SHELL_RC="$HOME/.bash_profile"

  if ! grep -q '.local/bin' "$SHELL_RC" 2>/dev/null; then
    echo '' >> "$SHELL_RC"
    echo '# PastCut relay' >> "$SHELL_RC"
    echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_RC"
    ok "Added ~/.local/bin to PATH in $(basename "$SHELL_RC")"
  fi
  export PATH="$DEST_DIR:$PATH"
fi
ok "PATH configured"

# ── Done — start relay ─────────────────────────────────────────
header "Installation Complete!"
echo
ok "PastCut Relay is installed."
echo
info "The relay will now start. Keep this window open while restreaming."
info "To stop: press Ctrl+C or close this window."
info "To start again: double-click this file or run 'pastcut-relay'"
echo
printf '\033[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m\n'
bold "  Starting PastCut Relay…"
printf '\033[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m\n'
echo

exec "$DEST"
