#!/usr/bin/env bash
# Pre-release gate for the zenops workspace.
#
# Runs on the working tree (not HEAD), so the `bump-version` skill can invoke
# this after staging version edits but before committing. Fails fast on the
# first check that doesn't pass.
#
# Safe to run directly from a clean tree as well — every step is read-only
# from a repo-state perspective.

set -euo pipefail

cd "$(dirname "$0")/.."

step() {
    printf '\n==> %s\n' "$1"
}

step 'cargo fmt --all -- --check'
cargo fmt --all -- --check

step 'cargo clippy --workspace --all-targets --all-features -- -D warnings'
cargo clippy --workspace --all-targets --all-features -- -D warnings

step 'cargo test --workspace'
cargo test --workspace

step 'cargo build --workspace --release'
cargo build --workspace --release

step 'cargo doc --workspace --no-deps'
cargo doc --workspace --no-deps

step 'cargo package --workspace --allow-dirty --no-verify'
# --workspace:   cover every publishable workspace member in one invocation;
#                crates with `publish = false` are skipped automatically.
# --allow-dirty: bump-version invokes this with uncommitted version edits.
# --no-verify:   the verify-build resolves internal workspace deps from the
#                registry, which fails before siblings are published; the
#                preceding `cargo build --release --workspace` already
#                exercised the code with real resolution.
cargo package --workspace --allow-dirty --no-verify

step 'python3 tests/cross-platform/run.py'
# Cross-platform integration matrix. Drives a real `zenops init` →
# `zenops apply` → `zenops import` journey in a Docker container per
# (distro, shell) combination. Last in the sequence because it's the
# slowest gate (image builds + per-row Rust compile inside the
# container), so cheaper failures surface first.
#
# Requirements: docker daemon reachable, python3 on PATH. See
# tests/cross-platform/README.md for the matrix definition and any
# host-specific setup notes.
if ! command -v docker >/dev/null 2>&1; then
    printf '\nERROR: docker is required by the cross-platform matrix step. ' >&2
    printf 'Install Docker (or start colima) before running this gate.\n' >&2
    exit 1
fi
if ! docker info >/dev/null 2>&1; then
    printf '\nERROR: docker daemon not reachable. ' >&2
    printf 'Start colima (or your Docker engine) and re-run.\n' >&2
    exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
    printf '\nERROR: python3 is required by tests/cross-platform/run.py.\n' >&2
    exit 1
fi
python3 tests/cross-platform/run.py

printf '\nPre-release checks passed.\n'
