#!/bin/sh
# Installs the Toupas command line (macOS, Linux):
#   curl -fsSL https://app.toupas.com/install.sh | sh
# Downloads the program for this computer, checks its SHA-256 checksum, and puts it in
# ~/.toupas/bin. Nothing else is needed: no Node.js, no administrator rights.
set -eu
BASE="${TOUPAS_DOWNLOAD_URL:-https://app.toupas.com/download}"
INSTALL_DIR="${TOUPAS_INSTALL_DIR:-$HOME/.toupas/bin}"

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

os=$(uname -s)
arch=$(uname -m)
case "$os" in
  Darwin) os=darwin ;;
  Linux) os=linux ;;
  *) fail "this system ($os) isn't supported. On Windows, use install.ps1." ;;
esac
case "$arch" in
  x86_64 | amd64) arch=x64 ;;
  arm64 | aarch64) arch=arm64 ;;
  *) fail "this processor ($arch) isn't supported." ;;
esac
# An Intel terminal on an Apple Silicon Mac (Rosetta) still gets the faster native program.
if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then
  arch=arm64
fi
platform="$os-$arch"

command -v curl >/dev/null 2>&1 || fail "curl is needed."
status=$(curl -sS -o /dev/null -w '%{http_code}' "$BASE/latest") || fail "couldn't reach $BASE. Check your internet connection."
[ "$status" = 404 ] && fail "no version of Toupas has been published yet."
latest=$(curl -fsSL "$BASE/latest") || fail "couldn't get the latest version from $BASE (HTTP $status)."
version=$(printf '%s' "$latest" | sed -n 's/.*"version" *: *"\([^"]*\)".*/\1/p')
[ -n "$version" ] || fail "couldn't read the latest version from $BASE/latest."

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
say "Downloading Toupas $version for $platform..."
# Compressed: about a third of the program's size. The checksum is of the program itself.
curl -fSL --progress-bar "$BASE/$version/toupas-$platform.gz" -o "$tmp/toupas.gz" || fail "the download failed."
gunzip "$tmp/toupas.gz" || fail "the download was damaged."
curl -fsSL "$BASE/$version/SHA256SUMS" -o "$tmp/SHA256SUMS" || fail "the checksum download failed."
expected=$(grep " toupas-$platform\$" "$tmp/SHA256SUMS" | cut -d' ' -f1)
if command -v sha256sum >/dev/null 2>&1; then
  actual=$(sha256sum "$tmp/toupas" | cut -d' ' -f1)
else
  actual=$(shasum -a 256 "$tmp/toupas" | cut -d' ' -f1)
fi
[ -n "$expected" ] && [ "$expected" = "$actual" ] || fail "the download didn't match its checksum, so nothing was installed."

mkdir -p "$INSTALL_DIR"
chmod 755 "$tmp/toupas"
mv "$tmp/toupas" "$INSTALL_DIR/toupas"
"$INSTALL_DIR/toupas" --version >/dev/null || fail "the program doesn't run on this computer."

case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    line="export PATH=\"$INSTALL_DIR:\$PATH\""
    case "${SHELL:-}" in
      */zsh) profiles="$HOME/.zshrc" ;;
      */bash) profiles="$HOME/.bashrc $HOME/.bash_profile" ;;
      *) profiles="$HOME/.profile" ;;
    esac
    for profile in $profiles; do
      if ! grep -qsF "$INSTALL_DIR" "$profile"; then
        printf '\n# Toupas\n%s\n' "$line" >>"$profile"
      fi
    done
    # Usable at once where ~/.local/bin is already on PATH.
    case ":$PATH:" in
      *":$HOME/.local/bin:"*) mkdir -p "$HOME/.local/bin" && ln -sf "$INSTALL_DIR/toupas" "$HOME/.local/bin/toupas" ;;
    esac
    ;;
esac

say ""
say "Toupas $version is installed: $INSTALL_DIR/toupas"
command -v toupas >/dev/null 2>&1 || say "Open a new terminal window to use it, or run: export PATH=\"$INSTALL_DIR:\$PATH\""
say "Next: in your app's folder, run: toupas live"
say "To let your coding agent (Claude Code, Codex, Cursor) use Toupas, run: toupas init"
