#!/bin/sh
# Install script for Zilliz CLI (zilliz / zz)
# Usage:
#   curl -fsSL https://github.com/zilliztech/zilliz-cli/releases/latest/download/install.sh | sh
#
# Environment variables:
#   VERSION     - specific version to install (e.g., "1.0.0"). Default: latest.
#   INSTALL_DIR - install directory. Default: ~/.zilliz/bin

set -eu

REPO="zilliztech/zilliz-cli"

# --- Dependency check ---

check_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "Error: '$1' is required but not found. Please install it and try again." >&2
    exit 1
  fi
}

check_cmd curl
check_cmd tar

# --- Platform detection ---

detect_target() {
  OS=$(uname -s)
  ARCH=$(uname -m)

  case "$OS" in
    Linux)  OS_PART="unknown-linux-gnu" ;;
    Darwin) OS_PART="apple-darwin" ;;
    *)
      echo "Error: Unsupported operating system: $OS" >&2
      echo "Supported: Linux, macOS (Darwin)" >&2
      exit 1
      ;;
  esac

  case "$ARCH" in
    x86_64)          ARCH_PART="x86_64" ;;
    aarch64|arm64)   ARCH_PART="aarch64" ;;
    *)
      echo "Error: Unsupported architecture: $ARCH" >&2
      echo "Supported: x86_64, aarch64 (arm64)" >&2
      exit 1
      ;;
  esac

  TARGET="${ARCH_PART}-${OS_PART}"
}

# --- Version resolution ---

resolve_version() {
  if [ -n "${VERSION:-}" ]; then
    echo "$VERSION"
    return
  fi

  # Query GitHub API for latest release with zilliz-v prefix
  LATEST_TAG=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases" \
    | grep -o '"tag_name": *"zilliz-v[^"]*"' \
    | head -1 \
    | sed 's/.*"zilliz-v\([^"]*\)".*/\1/')

  if [ -z "$LATEST_TAG" ]; then
    echo "Error: Could not determine latest version from GitHub releases." >&2
    echo "URL: https://api.github.com/repos/${REPO}/releases" >&2
    exit 1
  fi

  echo "$LATEST_TAG"
}

# --- Download and install ---

install() {
  detect_target
  VER=$(resolve_version)
  INSTALL="${INSTALL_DIR:-$HOME/.zilliz/bin}"

  ARCHIVE="zilliz-${VER}-${TARGET}.tar.gz"
  URL="https://github.com/${REPO}/releases/download/zilliz-v${VER}/${ARCHIVE}"

  echo "Installing Zilliz CLI v${VER} for ${TARGET}..."
  echo "Downloading ${URL}"

  TMPDIR=$(mktemp -d)
  trap 'rm -rf "$TMPDIR"' EXIT

  HTTP_CODE=$(curl -fsSL -w "%{http_code}" -o "${TMPDIR}/${ARCHIVE}" "$URL" 2>/dev/null) || true
  if [ ! -f "${TMPDIR}/${ARCHIVE}" ] || [ "$HTTP_CODE" != "200" ]; then
    echo "Error: Download failed (HTTP ${HTTP_CODE:-unknown})." >&2
    echo "URL: ${URL}" >&2
    echo "Check that version '${VER}' exists and your platform is supported." >&2
    exit 1
  fi

  tar -xzf "${TMPDIR}/${ARCHIVE}" -C "$TMPDIR"

  mkdir -p "$INSTALL"
  cp "${TMPDIR}/zilliz" "${INSTALL}/zilliz"
  chmod +x "${INSTALL}/zilliz"

  # Create zz symlink
  ln -sf "${INSTALL}/zilliz" "${INSTALL}/zz"

  echo ""
  echo "Zilliz CLI v${VER} installed successfully!"
  echo ""
  echo "  Location: ${INSTALL}/zilliz"
  echo "  Symlink:  ${INSTALL}/zz"

  # Check if install dir is in PATH
  case ":${PATH}:" in
    *":${INSTALL}:"*) ;;
    *)
      echo ""
      echo "Add the install directory to your PATH:"
      echo ""
      echo "  export PATH=\"${INSTALL}:\$PATH\""
      echo ""
      echo "To make it permanent, add the line above to your shell profile"
      echo "(~/.bashrc, ~/.zshrc, or ~/.profile)."
      ;;
  esac

  echo ""
  echo "Verify installation:"
  echo ""
  echo "  zilliz --version"
}

install
