#!/bin/bash
# Install the Fencio agent on this Mac and connect it to your own account.
#
#   sudo bash -c "$(curl -fsSL https://developer.fencio.dev/download/install-agent.sh)"
#
# It downloads the installer bundle, checks it against its published
# checksum, and runs it. You sign in once in your browser; no admin has to
# approve anything. See what it does without installing: add --preview.
# Removing Fencio later:  sudo /usr/local/fencio/uninstall.sh
set -eu

BASE_URL="https://developer.fencio.dev/download"
BUNDLE="fencio-install-macos.zip"

if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then
  BOLD=$'\033[1m'; DIM=$'\033[2m'; GREEN=$'\033[32m'; OFF=$'\033[0m'
  TEAL=$'\033[38;5;44m'
  SHADES=(51 45 44 38 37 36)
else
  BOLD=''; DIM=''; GREEN=''; OFF=''; TEAL=''
  SHADES=('' '' '' '' '' '')
fi

banner() {
  local lines=(
    '███████╗███████╗███╗   ██╗ ██████╗██╗ ██████╗ '
    '██╔════╝██╔════╝████╗  ██║██╔════╝██║██╔═══██╗'
    '█████╗  █████╗  ██╔██╗ ██║██║     ██║██║   ██║'
    '██╔══╝  ██╔══╝  ██║╚██╗██║██║     ██║██║   ██║'
    '██║     ███████╗██║ ╚████║╚██████╗██║╚██████╔╝'
    '╚═╝     ╚══════╝╚═╝  ╚═══╝ ╚═════╝╚═╝ ╚═════╝ '
  )
  local i=0 shade
  printf '\n'
  for line in "${lines[@]}"; do
    shade=${SHADES[$i]}
    if [ -n "$shade" ]; then
      printf '  \033[38;5;%sm%s\033[0m\n' "$shade" "$line"
    else
      printf '  %s\n' "$line"
    fi
    i=$((i + 1))
  done
  printf '\n'
}

welcome() {
  printf '  %sWelcome to Fencio.%s\n' "$BOLD" "$OFF"
  printf '  Fencio checks what your AI apps send and do against rules you\n'
  printf '  write, so you stay in control of your own tools.\n\n'
  printf '  %sHere is what happens next%s\n\n' "$BOLD" "$OFF"
  printf '    1  Download and verify the installer\n'
  printf '    2  Install the Fencio agent and the fencio command\n'
  printf '    3  Your browser opens so you can sign up or sign in\n'
  printf '    4  Choose which AI apps Fencio watches\n\n'
  printf '  %sNo admin has to approve anything. Fencio records the prompts,%s\n' \
    "$DIM" "$OFF"
  printf '  %sreplies and tool calls of the apps you choose, and keeps them%s\n' \
    "$DIM" "$OFF"
  printf '  %sin your own Fencio workspace, where you can review them.%s\n' \
    "$DIM" "$OFF"
}

fail() { printf '\nCannot continue: %s\n' "$*" >&2; exit 1; }

banner
welcome
[ "${1:-}" = "--preview" ] && { printf '\n  (preview only; nothing was installed)\n\n'; exit 0; }

[ "$(uname -s)" = Darwin ] || fail 'this installer is for macOS'
[ "$(id -u)" -eq 0 ] ||
  fail 'run it as: sudo bash -c "$(curl -fsSL '"$BASE_URL"'/install-agent.sh)"'
command -v curl >/dev/null 2>&1 || fail 'curl is required'

WORK=$(mktemp -d "${TMPDIR:-/tmp}/fencio-agent.XXXXXX")
trap 'rm -rf "$WORK"' EXIT

printf '\n%s%sStep 1 of 4 · Downloading the installer%s\n' \
  "$BOLD" "$TEAL" "$OFF"
curl -fsSL -o "$WORK/$BUNDLE" "$BASE_URL/$BUNDLE" ||
  fail "could not download $BASE_URL/$BUNDLE"
expected=$(curl -fsSL "$BASE_URL/$BUNDLE.sha256" | awk '{ print $1; exit }') ||
  fail 'could not download the checksum'
actual=$(shasum -a 256 "$WORK/$BUNDLE" | awk '{ print $1 }')
[ -n "$expected" ] && [ "$expected" = "$actual" ] ||
  fail 'the download does not match its published checksum; nothing was installed'
unzip -q "$WORK/$BUNDLE" -d "$WORK"
printf '  %s✓%s Downloaded and verified\n' "$GREEN" "$OFF"

# The bundle's script prints steps 2 to 4 and reads the terminal for your
# choices.
bash "$WORK/fencio-install/install.sh" "$@"
