#!/usr/bin/env bash
set -euo pipefail

DEFAULT_VERSION="v0.13.28"
VERSION="${SKILL_CLI_VERSION:-$DEFAULT_VERSION}"
DEFAULT_BASE_URL="https://gitee.com/marvin-dev/skill-cli-creator/raw/${VERSION}/bin/targets"
DEFAULT_BASE_URL_USES_VERSION=1
BASE_URL="${SKILL_CLI_BASE_URL:-$DEFAULT_BASE_URL}"
BASE_URL_EXPLICIT=0
if [ -n "${SKILL_CLI_BASE_URL:-}" ]; then
  BASE_URL_EXPLICIT=1
fi
INSTALL_DIR="${SKILL_CLI_INSTALL_DIR:-$HOME/.local/bin}"
CLAUDE_DIR="${SKILL_CLI_CLAUDE_DIR:-}"
RUN_COMPONENT_INSTALL=1
DOCTOR_MODE="${SKILL_CLI_DOCTOR:-lite}"
DRY_RUN=0

usage() {
  cat <<'EOF'
Install skill-cli from a prebuilt binary URL.

Usage:
  curl -fsSL <install.sh-url> | bash
  curl -fsSL <install.sh-url> | bash -s -- [options]

Options:
  --base-url URL       Base URL containing skill-cli-* binaries.
  --version VERSION    Gitee ref/tag used by the default Gitee base URL.
  --install-dir DIR    Directory to install skill-cli into.
  --claude-dir DIR     Pass --claude-dir to skill-cli install/doctor.
  --no-install         Only install the skill-cli binary.
  --doctor MODE        Doctor mode: lite, full, none. Default: lite.
  --dry-run            Print what would happen without downloading or writing.
  -h, --help           Show this help.

Environment:
  SKILL_CLI_BASE_URL      Same as --base-url.
  SKILL_CLI_VERSION       Same as --version. Default: v0.13.28.
  SKILL_CLI_INSTALL_DIR   Same as --install-dir.
  SKILL_CLI_CLAUDE_DIR    Same as --claude-dir.
  SKILL_CLI_DOCTOR        Same as --doctor.
EOF
}

while [ "$#" -gt 0 ]; do
  case "$1" in
    --base-url)
      BASE_URL="${2:?missing value for --base-url}"
      BASE_URL_EXPLICIT=1
      shift 2
      ;;
    --version)
      VERSION="${2:?missing value for --version}"
      if [ "$BASE_URL_EXPLICIT" = "0" ] && [ "$DEFAULT_BASE_URL_USES_VERSION" = "1" ]; then
        BASE_URL="https://gitee.com/marvin-dev/skill-cli-creator/raw/${VERSION}/bin/targets"
      fi
      shift 2
      ;;
    --install-dir)
      INSTALL_DIR="${2:?missing value for --install-dir}"
      shift 2
      ;;
    --claude-dir)
      CLAUDE_DIR="${2:?missing value for --claude-dir}"
      shift 2
      ;;
    --no-install)
      RUN_COMPONENT_INSTALL=0
      shift
      ;;
    --doctor)
      DOCTOR_MODE="${2:?missing value for --doctor}"
      shift 2
      ;;
    --dry-run)
      DRY_RUN=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "skill-cli install: unknown option: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

case "$DOCTOR_MODE" in
  lite|full|none) ;;
  *)
    echo "skill-cli install: --doctor must be one of: lite, full, none" >&2
    exit 2
    ;;
esac

detect_asset() {
  local os arch
  os="$(uname -s | tr '[:upper:]' '[:lower:]')"
  arch="$(uname -m | tr '[:upper:]' '[:lower:]')"

  case "$os" in
    linux*) os="linux" ;;
    darwin*) os="darwin" ;;
    mingw*|msys*|cygwin*) os="win32" ;;
    *)
      echo "unsupported OS: $os" >&2
      return 1
      ;;
  esac

  case "$arch" in
    x86_64|amd64) arch="x64" ;;
    aarch64|arm64) arch="arm64" ;;
    *)
      echo "unsupported architecture: $arch" >&2
      return 1
      ;;
  esac

  if [ "$os" = "win32" ]; then
    if [ "$arch" != "x64" ]; then
      echo "unsupported Windows architecture: $arch" >&2
      return 1
    fi
    printf '%s\n' "skill-cli-win32-x64.exe"
    return 0
  fi

  printf 'skill-cli-%s-%s\n' "$os" "$arch"
}

download_file() {
  local url="$1" out="$2"
  if command -v curl >/dev/null 2>&1; then
    curl -fL --retry 3 --connect-timeout 15 -o "$out" "$url"
  elif command -v wget >/dev/null 2>&1; then
    wget -O "$out" "$url"
  else
    echo "skill-cli install: curl or wget is required" >&2
    return 1
  fi
}

ASSET="$(detect_asset)"
URL="${BASE_URL%/}/$ASSET"
TARGET_NAME="skill-cli"
case "$ASSET" in
  *.exe) TARGET_NAME="skill-cli.exe" ;;
esac
TARGET_PATH="$INSTALL_DIR/$TARGET_NAME"

echo "skill-cli installer"
echo "  asset:      $ASSET"
echo "  source:     $URL"
echo "  target:     $TARGET_PATH"
if [ -z "${SKILL_CLI_BASE_URL:-}" ]; then
  echo "  version:    $VERSION"
fi
if [ -n "$CLAUDE_DIR" ]; then
  echo "  claude dir: $CLAUDE_DIR"
fi

if [ "$DRY_RUN" = "1" ]; then
  echo "dry run: no files written"
  exit 0
fi

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

download_file "$URL" "$TMP_DIR/$ASSET"
chmod +x "$TMP_DIR/$ASSET" || true
mkdir -p "$INSTALL_DIR"
cp "$TMP_DIR/$ASSET" "$TARGET_PATH"
chmod +x "$TARGET_PATH" || true

echo ""
"$TARGET_PATH" version

claude_args=()
if [ -n "$CLAUDE_DIR" ]; then
  claude_args=(--claude-dir "$CLAUDE_DIR")
fi

if [ "$RUN_COMPONENT_INSTALL" = "1" ]; then
  echo ""
  "$TARGET_PATH" install "${claude_args[@]}"
fi

case "$DOCTOR_MODE" in
  lite)
    echo ""
    "$TARGET_PATH" doctor "${claude_args[@]}" --check-adversarial=false
    ;;
  full)
    echo ""
    "$TARGET_PATH" doctor "${claude_args[@]}"
    ;;
  none)
    ;;
esac

case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    echo ""
    echo "Add this directory to PATH if 'skill-cli' is not found in new shells:"
    echo "  $INSTALL_DIR"
    ;;
esac

echo ""
echo "skill-cli install complete."
