#!/bin/sh

cd "$(git rev-parse --show-toplevel)"

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

echo "Running checks in parallel..."

uv run ruff check . > "$tmpdir/ruff-check.log" 2>&1 &
pid_ruff_check=$!
uv run ruff format --check . > "$tmpdir/ruff-format.log" 2>&1 &
pid_ruff_format=$!
uv run pytest > "$tmpdir/pytest.log" 2>&1 &
pid_pytest=$!

failed=0

if ! wait $pid_ruff_check; then
  echo "Ruff check failed:"
  cat "$tmpdir/ruff-check.log"
  failed=1
else
  echo "  ok ruff check"
fi

if ! wait $pid_ruff_format; then
  echo "Ruff format failed:"
  cat "$tmpdir/ruff-format.log"
  failed=1
else
  echo "  ok ruff format"
fi

if ! wait $pid_pytest; then
  echo "Pytest failed:"
  cat "$tmpdir/pytest.log"
  failed=1
else
  echo "  ok pytest"
fi

if [ $failed -eq 1 ]; then
  exit 1
fi

echo "All checks passed."
