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

# Bootstrap dispatches to the platform-specific install scripts. Each step
# that needs root sources `script/warp_sudo` and asks for confirmation
# before running. Pass -y / --yes (or set WARP_SKIP_SUDO_PROMPT=1) to skip
# the prompts in unattended environments.

OS_TYPE="$(uname -s)"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
INSTALL_COMMON_SKILLS=1
COMMON_SKILLS_TARGET="${WARP_COMMON_SKILLS_INSTALL_TARGET:-}"
PLATFORM_ARGS=()

cd "${REPO_ROOT}"

usage() {
  cat <<EOF
Usage: ./script/bootstrap [options]

Prepare this checkout for Warp development by running the platform-specific bootstrap steps.

Options:
  -h, --help               Show this help message.
  --install-common-skills  Install or update common agent skills from skills-lock.json (default).
  --install-common-skills-in-repo
                            Install or update common agent skills in this checkout's .agents/skills.
  --install-common-skills-globally
                            Install or update common agent skills in ~/.agents/skills.
  --skip-common-skills     Skip installing common agent skills.

Environment:
  WARP_SKIP_COMMON_SKILLS_INSTALL=1
      Skip installing common agent skills, even when --install-common-skills is provided.
  WARP_COMMON_SKILLS_INSTALL_TARGET=project|global
      Choose the install target when no explicit prompt answer is provided.
      Target prompting and duplicate checks are delegated to
      warpdotdev/common-skills/scripts/install_common_skills.
  WARP_COMMON_SKILLS_SCRIPTS_DIR=/path/to/common-skills/scripts
      Override where common-skills management scripts are loaded from.
      If unset, ./script/resolve_common_skills executes the raw script from
      warpdotdev/common-skills.
  WARP_COMMON_SKILLS_REF=<git-ref>
      Override the remote warpdotdev/common-skills ref for raw script fallback.
EOF
}

print_bootstrap_preview() {
  local platform="$1"

  echo "Warp bootstrap is starting for ${platform}."
  echo "It will:"

  if [[ "${platform}" = "macOS" ]]; then
    echo "  - Configure Xcode as the active developer directory."
    echo "  - Install or update Cargo, Homebrew, PowerShell, Docker, gcloud, and related development tools."
    echo "  - Add the aarch64-apple-darwin Rust target."
  elif [[ "${platform}" = "Linux" ]]; then
    echo "  - Update apt package metadata."
    echo "  - Install dependencies needed to build, run, and test Warp."
    echo "  - Install linuxdeploy and check gcloud authentication."
  fi

  if [[ "${INSTALL_COMMON_SKILLS}" -eq 0 ]]; then
    echo "  - Skip common agent skills because --skip-common-skills was provided."
  elif [[ "${WARP_SKIP_COMMON_SKILLS_INSTALL:-}" = "1" ]]; then
    echo "  - Skip common agent skills because WARP_SKIP_COMMON_SKILLS_INSTALL=1."
  elif [[ "${COMMON_SKILLS_TARGET}" = "global" ]]; then
    echo "  - Install or update common agent skills in ~/.agents/skills if needed."
  elif [[ "${COMMON_SKILLS_TARGET}" = "project" ]]; then
    echo "  - Install or update common agent skills in this checkout's .agents/skills if needed."
  else
    echo "  - Prompt for where common agent skills should be installed before installing or updating them."
  fi
  if [[ "${INSTALL_COMMON_SKILLS}" -eq 1 && "${WARP_SKIP_COMMON_SKILLS_INSTALL:-}" != "1" ]]; then
    echo "  - Verify installed common skills match skills-lock.json."
  fi
  echo "Run ./script/bootstrap --help to see options and environment overrides."
  echo
}


for arg in "$@"; do
  case "${arg}" in
    -h|--help)
      usage
      exit 0
      ;;
    -y|--yes)
      export WARP_SKIP_SUDO_PROMPT=1
      if [[ ! "$OS_TYPE" =~ ^(MINGW64_NT|MSYS_NT) ]]; then
        PLATFORM_ARGS+=("${arg}")
      fi
      ;;
    --install-common-skills)
      INSTALL_COMMON_SKILLS=1
      ;;
    --install-common-skills-in-repo)
      INSTALL_COMMON_SKILLS=1
      COMMON_SKILLS_TARGET="project"
      ;;
    --install-common-skills-globally)
      INSTALL_COMMON_SKILLS=1
      COMMON_SKILLS_TARGET="global"
      ;;
    --skip-common-skills)
      INSTALL_COMMON_SKILLS=0
      ;;
    *)
      PLATFORM_ARGS+=("${arg}")
      ;;
  esac
done

maybe_install_common_skills() {
  if [[ "${INSTALL_COMMON_SKILLS}" -eq 1 ]]; then
    local target_args=()
    if [[ "${WARP_SKIP_COMMON_SKILLS_INSTALL:-}" = "1" ]]; then
      return
    fi
    if [[ "${COMMON_SKILLS_TARGET}" = "project" || "${COMMON_SKILLS_TARGET}" = "global" ]]; then
      target_args=("--${COMMON_SKILLS_TARGET}")
      if ! ./script/resolve_common_skills install_common_skills -- --repo-root "${REPO_ROOT}" "${target_args[@]}" --if-needed; then
        echo "error: unable to install common skills; continuing without them." >&2
      fi
    else
      if ! ./script/resolve_common_skills install_common_skills -- --repo-root "${REPO_ROOT}" --if-needed --prompt-for-target; then
        echo "error: unable to install common skills; continuing without them." >&2
      fi
    fi
  fi
}

# This repository requires Git LFS; ensure it is installed and initialized
# for this checkout.
ensure_git_lfs() {
  if ! command -v git-lfs >/dev/null 2>&1; then
    echo
    echo "warning: git-lfs is not installed. Install it before continuing:"
    case "${OS_TYPE}" in
      Darwin)               echo "  brew install git-lfs" ;;
      Linux)                echo "  sudo apt-get install -y git-lfs   # or your distro's package manager" ;;
      MINGW64_NT*|MSYS_NT*)  echo "  choco install git-lfs            # or: winget install GitHub.GitLFS" ;;
      *)                    echo "  See https://git-lfs.com for install instructions." ;;
    esac
    echo "After installing, re-run ./script/bootstrap."
    return 1
  fi
  git lfs install --local >/dev/null
  git lfs pull
}

ensure_git_lfs

if [[ "$OS_TYPE" = "Darwin" ]]; then
  print_bootstrap_preview "macOS"
  ./script/macos/bootstrap "${PLATFORM_ARGS[@]}"
  maybe_install_common_skills
elif [[ "$OS_TYPE" = "Linux" ]]; then
  print_bootstrap_preview "Linux"
  ./script/linux/bootstrap "${PLATFORM_ARGS[@]}"
  maybe_install_common_skills
elif [[ "$OS_TYPE" =~ ^(MINGW64_NT|MSYS_NT) ]]; then
  if [[ "${INSTALL_COMMON_SKILLS}" -eq 1 ]]; then
    if [[ -n "${COMMON_SKILLS_TARGET}" ]]; then
      ./script/windows/bootstrap.ps1 "${PLATFORM_ARGS[@]}" -InstallCommonSkills -CommonSkillsTarget "${COMMON_SKILLS_TARGET}"
    else
      ./script/windows/bootstrap.ps1 "${PLATFORM_ARGS[@]}" -InstallCommonSkills
    fi
  else
    ./script/windows/bootstrap.ps1 "${PLATFORM_ARGS[@]}"
  fi
else
  echo "No bootstrap script defined for the current platform!"
  exit 1
fi
