#!/bin/sh
# script/fmt-check: check formatting (read-only). Same scope as
# script/fmt: gofmt for Go, prettier for Markdown. Both run every time
# and each reports independently, so a failure names which formatter is
# unhappy; the script exits non-zero if either found unformatted files.
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"

run_prettier() {
    if ! command -v yarn >/dev/null 2>&1; then
        echo "fmt-check: yarn not found; run script/bootstrap first" >&2
        exit 1
    fi
    yarn run prettier "$@"
}

main() {
    cd "$ROOT"
    rc=0

    files="$(gofmt -s -l .)"
    if [ -n "$files" ]; then
        echo "gofmt: files not formatted:" >&2
        echo "$files" >&2
        rc=1
    fi

    if ! run_prettier --check '**/*.md' --tab-width 4 --prose-wrap always; then
        echo "prettier: Markdown not formatted; run make fmt" >&2
        rc=1
    fi

    exit "$rc"
}

main "$@"
