#!/usr/bin/env bash
set -u
GW=$1; OUT=$GW/.spt-lint
mkdir "$OUT" 2>/dev/null || { echo REFUSE >&2; exit 4; }
cd "$GW" || exit 9
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME
ps_join() { powershell.exe -NoProfile -Command "$1" 2>/dev/null | tr -d '\r\n'; }
{
  echo "started: $(date -u +%FT%TZ)"
  echo "head: $(git rev-parse HEAD)"
  echo "dirty: $(git status --porcelain | grep -c '^ M')"
  echo "holders(exe-in-pool, REFUSES): [$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.ExecutablePath -like '*hertz-65s-rider*' } | Select-Object -ExpandProperty ProcessId) -join ','")]"
  echo "builders box-wide (INFORMATIONAL, todlando is running -p spt in parallel; wall time NOT a quiet-box number): [$(ps_join "(Get-CimInstance Win32_Process | Where-Object { \$_.Name -in @('cargo.exe','rustc.exe','clippy-driver.exe') } | Select-Object -ExpandProperty ProcessId) -join ','")]"
  echo "TOOLCHAIN cargo: $(cargo --version)"
  echo "TOOLCHAIN clippy: $(cargo clippy --version)"
} > "$OUT/census.txt"
cargo clippy --workspace --all-targets -- -D warnings > "$OUT/clippy.raw" 2>&1
echo $? > "$OUT/clippy.exit"
{
  echo "clippy.exit=$(cat "$OUT/clippy.exit")"
  echo "warnings=$(grep -cE '^warning' "$OUT/clippy.raw")"
  echo "errors=$(grep -cE '^error' "$OUT/clippy.raw")"
  echo "finished: $(date -u +%FT%TZ)"
} >> "$OUT/census.txt"
