#!/bin/sh
# Extend's helper on this computer (Hearz and the Waiyers button), installed for the person using it:
#
#   curl -fsSL https://extend.waiyers.com/install.sh | sh
#
# Nothing is needed beforehand: a Python of its own is fetched (by uv, which is installed when missing), in a folder
# of its own (~/.extend), with what it needs; the browsers found (Chrome, Chromium, Brave, Edge, ...) are told where
# the helper is. Run it again to update. `sh install.sh --uninstall` removes it (what Hearz saved in Downloads/extend
# is kept). macOS and Linux.
#
#   EXTEND_HELPER_URL   where the helper's bundle is (a URL or a file; default: beside this script on the site)
#   EXTEND_HOME         the helper's folder (default ~/.extend)
set -eu

SITE="${EXTEND_SITE:-https://extend.waiyers.com}"
BUNDLE="${EXTEND_HELPER_URL:-$SITE/extend-helper.tar.gz}"
ROOT="${EXTEND_HOME:-$HOME/.extend}"
say() { printf '%s\n' "$*"; }
fail() { printf 'Extend: %s\n' "$*" >&2; exit 1; }

case "$(uname -s)" in
  Darwin) HOSTS="$HOME/Library/Application Support" ;;
  Linux) HOSTS="${XDG_CONFIG_HOME:-$HOME/.config}" ;;
  *) fail "this installer is for macOS and Linux (Windows is not supported yet)" ;;
esac

if [ "${1:-}" = "--uninstall" ]; then
  # the browsers no longer know the helper; its folder goes (the button first, if it is on the screen)
  if [ -f "$ROOT/button.pid" ]; then kill "$(cat "$ROOT/button.pid")" 2>/dev/null || true; fi
  find "$HOSTS" -maxdepth 4 -name 'com.waiyers.extend.json' -path '*NativeMessagingHosts*' -exec rm -f {} + 2>/dev/null || true
  for part in venv app bin extend-native host.log Waiyers.app Waiyers.built Waiyers.signed Waiyers.iconset button.pid button.json button.log; do
    rm -rf "${ROOT:?}/$part"
  done
  rmdir "$ROOT" 2>/dev/null || true
  say "Extend's helper is removed. What Hearz and Kaptures saved (Downloads/extend) is kept."
  exit 0
fi

command -v curl >/dev/null 2>&1 || fail "curl is needed to download the helper"
command -v tar >/dev/null 2>&1 || fail "tar is needed to open the helper's bundle"

# 1. uv: it fetches a Python of its own (none is needed on the computer) and installs fast
PATH="$HOME/.local/bin:$HOME/.cargo/bin:$PATH"
export PATH
if ! command -v uv >/dev/null 2>&1; then
  say "Extend: getting uv (it brings its own Python)…"
  curl -LsSf https://astral.sh/uv/install.sh | sh >/dev/null 2>&1 || fail "uv could not be installed (see https://docs.astral.sh/uv/)"
  command -v uv >/dev/null 2>&1 || fail "uv was installed but is not found: open a new terminal and run this again"
fi

# 2. the helper's own code
mkdir -p "$ROOT"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
say "Extend: getting the helper…"
case "$BUNDLE" in
  http://*|https://*) curl -fsSL "$BUNDLE" -o "$TMP/helper.tar.gz" || fail "the helper could not be downloaded from $BUNDLE" ;;
  *) cp "$BUNDLE" "$TMP/helper.tar.gz" || fail "no helper bundle at $BUNDLE" ;;
esac
mkdir "$TMP/app"
tar -xzf "$TMP/helper.tar.gz" -C "$TMP/app" || fail "the helper's bundle could not be opened"
[ -f "$TMP/app/tadata/extend/native.py" ] || fail "the helper's bundle is not the one expected"
rm -rf "$ROOT/app"
mv "$TMP/app" "$ROOT/app"

# 3. its Python and what it needs (a folder of its own: nothing is added to the computer's Python)
say "Extend: preparing its Python (a few minutes the first time)…"
uv venv --quiet --python 3.12 --allow-existing "$ROOT/venv" || fail "its Python could not be prepared"
uv pip install --quiet --python "$ROOT/venv/bin/python" -r "$ROOT/app/requirements.txt" || fail "what the helper needs could not be installed"

# 4. ffmpeg: the computer's own, else the one that came with the helper
mkdir -p "$ROOT/bin"
if ! command -v ffmpeg >/dev/null 2>&1 && [ ! -x /opt/homebrew/bin/ffmpeg ] && [ ! -x /usr/local/bin/ffmpeg ]; then
  OWN="$("$ROOT/venv/bin/python" -c 'import imageio_ffmpeg; print(imageio_ffmpeg.get_ffmpeg_exe())' 2>/dev/null || true)"
  [ -n "$OWN" ] && [ -x "$OWN" ] && ln -sf "$OWN" "$ROOT/bin/ffmpeg"
fi

# 5. the browsers are told where the helper is
OUT="$(PYTHONPATH="$ROOT/app" "$ROOT/venv/bin/python" -m tadata.extend.native install)" || fail "the helper could not be registered with the browsers"
FOUND="$(printf '%s\n' "$OUT" | grep -c '^host registered' || true)"

say ""
if [ "$FOUND" -gt 0 ]; then
  say "Extend's helper is installed ($FOUND browser$([ "$FOUND" = 1 ] || printf s) found)."
  say "Close the Extend panel and open it again: Hearz says \"on this computer\"."
else
  say "Extend's helper is installed, but no Chrome, Chromium, Brave or Edge was found on this computer."
  say "Install the browser, open it once, then run this again."
fi
say "The first transcription downloads its listening model (about 150 MB), once."
