name: CI

on:
  push:
  pull_request:
  workflow_dispatch:
    inputs:
      cold_cache:
        description: Deliberately clear the required dependency/native cache before validation
        required: false
        default: false
        type: boolean

concurrency:
  group: ci-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  required:
    name: required
    runs-on: ubuntu-latest
    timeout-minutes: 15
    env:
      CARGO_BUILD_JOBS: "2"
      CARGO_INCREMENTAL: "0"
      CARGO_PROFILE_DEV_DEBUG: "0"
      CARGO_PROFILE_TEST_DEBUG: "0"
      RUSTDOCFLAGS: "-D warnings"
      CARGO_HOME: ${{ runner.tool_cache }}/metacrate/cargo/home-rust-1.97.1-aarch64-unknown-linux-gnu
      METACRATE_EXPECTED_HOST: aarch64-unknown-linux-gnu
      METACRATE_PROVENANCE_DIAGNOSTICS_DIR: artifacts/ci/provenance-candidates
    steps:
      - uses: actions/checkout@v4

      - name: Inspect the runner-persistent, architecture-keyed cache
        run: |
          cache_key="$(sha256sum .gitea/workflows/ci.yml Cargo.lock Cargo.toml ci/ci-coverage.json ci/dependency-policy.json deny.toml tools/install_openjpeg_2_5_4.sh crates/libremetaverse-imaging-skia/Cargo.toml | sha256sum | cut -d ' ' -f 1)"
          cargo_target="${{ runner.tool_cache }}/metacrate/cargo/target-rust-1.97.1-aarch64-unknown-linux-gnu-$cache_key"
          openjpeg_prefix="${{ runner.tool_cache }}/metacrate/native/openjpeg-2.5.4-${{ runner.arch }}"
          skia_dir="${{ runner.tool_cache }}/metacrate/native/skia-0.99.0-aarch64-unknown-linux-gnu"
          skia_archive="$skia_dir/skia-binaries-a25a0fdb7d90429aa2d1-aarch64-unknown-linux-gnu-jpegd-jpege-pdf-svg-textlayout-vulkan-webpd-webpe.tar.gz"
          if test "${{ github.event_name }}" = workflow_dispatch && test "${{ inputs.cold_cache }}" = true; then
            cargo clean --target-dir "$cargo_target"
            rm -rf -- "$CARGO_HOME/registry" "$CARGO_HOME/git" "$openjpeg_prefix" "$skia_dir"
            rm -f -- "$CARGO_HOME/bin/cargo-deny" "$CARGO_HOME/bin/cargo-machete"
          fi
          mkdir -p "$CARGO_HOME" "$cargo_target" "$skia_dir"
          test -f "$cargo_target/.metacrate-ready" && cargo_hit=true || cargo_hit=false
          test -f "$openjpeg_prefix/metacrate-openjpeg.identity" && test -f "$skia_archive" && native_hit=true || native_hit=false
          test -x "$CARGO_HOME/bin/cargo-deny" && test -x "$CARGO_HOME/bin/cargo-machete" && tools_hit=true || tools_hit=false
          echo "CARGO_TARGET_DIR=$cargo_target" >> "$GITHUB_ENV"
          echo "OPENJPEG_PREFIX=$openjpeg_prefix" >> "$GITHUB_ENV"
          echo "PKG_CONFIG_PATH=$openjpeg_prefix/lib/pkgconfig" >> "$GITHUB_ENV"
          echo "LD_LIBRARY_PATH=$openjpeg_prefix/lib" >> "$GITHUB_ENV"
          echo "METACRATE_SKIA_ARCHIVE=$skia_archive" >> "$GITHUB_ENV"
          echo "METACRATE_CARGO_CACHE_HIT=$cargo_hit" >> "$GITHUB_ENV"
          echo "METACRATE_NATIVE_CACHE_HIT=$native_hit" >> "$GITHUB_ENV"
          echo "METACRATE_TOOLS_CACHE_HIT=$tools_hit" >> "$GITHUB_ENV"

      - name: Install the single native prerequisite set
        run: |
          sudo apt-get update
          sudo apt-get install --yes build-essential clang cmake curl git ninja-build pkg-config python3 libfontconfig1-dev libfreetype6-dev libopus-dev libasound2-dev

      - name: Install pinned Rust and audit tools
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: 1.97.1
          components: clippy,rustfmt
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-deny@0.20.2,cargo-machete@0.9.2

      - name: Verify pinned audit tool versions
        run: |
          cargo deny --version | grep '0.20.2'
          cargo machete --version | grep '0.9.2'

      - name: Validate and populate the exact native cache
        run: |
          test "$(uname -m)" = aarch64
          rustc -vV | grep '^host: aarch64-unknown-linux-gnu$'
          tools/install_openjpeg_2_5_4.sh "$OPENJPEG_PREFIX"
          pkg-config --exact-version 2.5.4 libopenjp2
          mkdir -p "$(dirname "$METACRATE_SKIA_ARCHIVE")"
          if ! echo 'dd127f458a5e67a79f3936a8aa19f822fe90a1d6a11b50b5f84df2b0519d909c  '"$METACRATE_SKIA_ARCHIVE" | sha256sum --check --status; then
            curl --fail --location --proto '=https' --tlsv1.2 --output "$METACRATE_SKIA_ARCHIVE" \
              https://github.com/rust-skia/skia-binaries/releases/download/0.99.0/skia-binaries-a25a0fdb7d90429aa2d1-aarch64-unknown-linux-gnu-jpegd-jpege-pdf-svg-textlayout-vulkan-webpd-webpe.tar.gz
          fi
          echo 'dd127f458a5e67a79f3936a8aa19f822fe90a1d6a11b50b5f84df2b0519d909c  '"$METACRATE_SKIA_ARCHIVE" | sha256sum --check

      - name: Run the authoritative code-ready graph
        run: cargo run --locked -p metacrate-ci-matrix -- required-gate --evidence artifacts/ci/required-gate.json

      - name: Mark the validated cache generation ready
        run: touch "$CARGO_TARGET_DIR/.metacrate-ready"

      - name: Upload stage timing and audit evidence
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: required-ci-evidence
          path: artifacts/ci/
          if-no-files-found: error
