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

usage() {
  cat <<'EOF'
usage: run-build-backend-lane <lane>

Runs a developer-facing lane with Cargo by default and BuildBuddy when
MEERKAT_BUILDBUDDY=1 is set.

Lanes:
  build
  check
  lint
  test
  test-all
  test-unit
  test-int
  e2e-fast
  e2e-system
  e2e-live
  e2e-smoke
  e2e-auth
EOF
}

if [[ $# -ne 1 ]]; then
  usage >&2
  exit 2
fi

lane="$1"
root="$(git rev-parse --show-toplevel)"
cd "${root}"

source "${root}/scripts/build-backend-env"

cargo_bin="${CARGO:-${root}/scripts/repo-cargo}"

case "${lane}" in
  build)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" build
    fi
    exec "${cargo_bin}" build --workspace
    ;;
  check)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" check
    fi
    exec "${cargo_bin}" check --workspace --all-targets --all-features
    ;;
  lint)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" clippy
    fi
    exec "${cargo_bin}" clippy --workspace --all-targets --all-features -- -D warnings
    ;;
  test)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" test
    fi
    # Cargo's worktree+parent config-merge concatenates list-form aliases
    # (the workspace's `fast`/`int`/`rct` aliases live as TOML arrays at
    # both the worktree and the main checkout's `.cargo/config.toml`),
    # which duplicates `--workspace` and trips clap's "cannot be used
    # multiple times" guard. Expand the alias inline until the upstream
    # aliases are normalized to string form on `main`.
    exec "${cargo_bin}" nextest run --workspace --profile fast --show-progress none --status-level none --final-status-level fail
    ;;
  test-all)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" test-all
    fi
    exec "${cargo_bin}" nextest run --workspace --profile fast --show-progress none --status-level none --final-status-level fail --all-features
    ;;
  test-unit)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" test-unit
    fi
    exec "${cargo_bin}" nextest run --workspace --lib --show-progress none --status-level none --final-status-level fail
    ;;
  test-int)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" test-int
    fi
    exec "${cargo_bin}" nextest run --workspace --tests --profile fast --show-progress none --status-level none --final-status-level fail
    ;;
  e2e-fast)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" e2e-fast
    fi
    exec "${cargo_bin}" e2e-fast
    ;;
  e2e-system)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" e2e-system
    fi
    exec "${cargo_bin}" e2e-system
    ;;
  e2e-live)
    meerkat_load_local_secrets_env "${root}"
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" e2e-live
    fi
    exec "${cargo_bin}" e2e-live
    ;;
  e2e-smoke)
    meerkat_load_local_secrets_env "${root}"
    if meerkat_buildbuddy_enabled; then
      "${root}/scripts/buildbuddy-dev" e2e-smoke
      case "${BUILDBUDDY_DRY_RUN:-}" in
        1|true|TRUE|yes|YES|on|ON)
          exit 0
          ;;
      esac
    fi
    exec "${root}/scripts/e2e-smoke-lane"
    ;;
  e2e-auth)
    if meerkat_buildbuddy_enabled; then
      exec "${root}/scripts/buildbuddy-dev" e2e-auth
    fi
    exec "${cargo_bin}" e2e-auth
    ;;
  *)
    echo "unknown build backend lane: ${lane}" >&2
    usage >&2
    exit 2
    ;;
esac
