#!/usr/bin/env bash
# Praex CLI installer
# Usage: curl -fsSL https://praex.ai/install.sh | bash
set -euo pipefail

BASE_URL="${PRAEX_BASE_URL:-https://praex.ai}"
INSTALL_DIR="${PRAEX_INSTALL_DIR:-$HOME/.local/bin}"

case "$(uname -s)-$(uname -m)" in
  Linux-x86_64)          ASSET=praex-linux-x64.tar.gz ;;
  Darwin-arm64)          ASSET=praex-darwin-arm64.tar.gz ;;
  Darwin-x86_64)         ASSET=praex-darwin-x64.tar.gz ;;
  *)
    echo "praex ships for Linux x86_64 and macOS (Apple Silicon or Intel)."
    echo "Found: $(uname -s) $(uname -m). Windows users: praex runs under WSL."
    exit 1
    ;;
esac

# macOS has no sha256sum; it ships shasum instead.
if command -v sha256sum >/dev/null 2>&1; then
  sha_cmd() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
  sha_cmd() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
  echo "need sha256sum or shasum to verify the download"; exit 1
fi

echo "Downloading praex ($ASSET)..."
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSL "$BASE_URL/dl/$ASSET" -o "$tmp/$ASSET"
curl -fsSL "$BASE_URL/dl/SHA256SUMS" -o "$tmp/SHA256SUMS"

echo "Verifying checksum..."
# SHA256SUMS covers every platform, so check our asset rather than the whole
# file -- the tarballs we did not download are legitimately absent.
want="$(awk -v a="$ASSET" '$2 == a || $2 == "*" a {print $1; exit}' "$tmp/SHA256SUMS")"
if [ -z "$want" ]; then echo "no checksum published for $ASSET"; exit 1; fi
got="$(sha_cmd "$tmp/$ASSET")"
if [ "$want" != "$got" ]; then
  echo "checksum mismatch for $ASSET"
  echo "  expected: $want"
  echo "  actual:   $got"
  exit 1
fi

mkdir -p "$INSTALL_DIR"
tar xzf "$tmp/$ASSET" -C "$INSTALL_DIR"
chmod +x "$INSTALL_DIR/praex"

echo ""
echo "  Installed: $INSTALL_DIR/praex ($("$INSTALL_DIR/praex" --version 2>/dev/null | tail -1))"
# Not on PATH yet: add it to the shell's startup file (once), the way bun and rustup do, so
# `praex` works in every new terminal. PRAEX_NO_MODIFY_PATH=1 skips this.
on_path=1
case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    on_path=0
    line="export PATH=\"$INSTALL_DIR:\$PATH\""
    case "$(basename "${SHELL:-bash}")" in
      zsh)  rc="$HOME/.zshrc" ;;
      fish) rc="$HOME/.config/fish/config.fish"; line="fish_add_path \"$INSTALL_DIR\"" ;;
      bash) if [ "$(uname -s)" = Darwin ]; then rc="$HOME/.bash_profile"; else rc="$HOME/.bashrc"; fi ;;
      *)    rc="$HOME/.profile" ;;
    esac
    if [ "${PRAEX_NO_MODIFY_PATH:-0}" != 1 ]; then
      mkdir -p "$(dirname "$rc")"
      if ! grep -qsF "$INSTALL_DIR" "$rc"; then
        printf '\n# praex\n%s\n' "$line" >> "$rc"
        echo "  Added $INSTALL_DIR to your PATH in $rc"
      fi
      echo "  Open a new terminal, or run this once here:  $line"
    else
      echo "  NOTE: add $INSTALL_DIR to your PATH:  $line"
    fi
    ;;
esac
echo ""
if [ "$on_path" = 1 ]; then echo "  Start it:           praex"; else echo "  Start it:           $INSTALL_DIR/praex   (or praex, in a new terminal)"; fi
echo "  Free hosted model:  praex login   (Velox II: free, every day)"
echo "  Or bring your own keys. praex speaks every major provider."
