#!/usr/bin/env sh
set -u

script_dir="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
preset="${1:-linux-clang}"
start="$(date +%s)"
cmake --preset "$preset" -DPP_ENABLE_CLANG_TIDY=ON -DPP_ENABLE_CPPCHECK=ON
configure_exit_code="$?"
shader_exit_code="0"
renderer_boundary_exit_code="0"
if [ "$configure_exit_code" -eq 0 ]; then
    cmake --build --preset "$preset" --target panopainter_validate_shaders
    shader_exit_code="$?"
fi
if [ "$configure_exit_code" -eq 0 ]; then
    "$script_dir/check-renderer-boundary.sh"
    renderer_boundary_exit_code="$?"
fi
exit_code="$configure_exit_code"
if [ "$exit_code" -eq 0 ] && [ "$shader_exit_code" -ne 0 ]; then
    exit_code="$shader_exit_code"
fi
if [ "$exit_code" -eq 0 ] && [ "$renderer_boundary_exit_code" -ne 0 ]; then
    exit_code="$renderer_boundary_exit_code"
fi
end="$(date +%s)"
elapsed_ms="$(( (end - start) * 1000 ))"
printf '{"command":"analyze","preset":"%s","exitCode":%s,"checks":[{"name":"configure","exitCode":%s},{"name":"shader-validation","exitCode":%s},{"name":"renderer-boundary","exitCode":%s}],"elapsedMs":%s}\n' "$preset" "$exit_code" "$configure_exit_code" "$shader_exit_code" "$renderer_boundary_exit_code" "$elapsed_ms"
exit "$exit_code"
