#!/bin/sh
# script/fmt-check: check formatting (read-only). Same scope as
# script/fmt, but fails instead of writing.
set -eu

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

main() {
    cd "$ROOT"
    # Where script/bootstrap installs Go when the host has none.
    PATH="$PATH:$ROOT/.tool/go/bin"
    # .tool/go holds that toolchain's own sources, which are not ours.
    if ! unformatted="$(find . -path ./.tool -prune -o -name '*.go' -type f \
        -exec gofmt -l {} +)"; then
        echo "fmt-check: gofmt failed" >&2
        exit 1
    fi
    if [ -n "$unformatted" ]; then
        echo "Files not formatted:" >&2
        echo "$unformatted" >&2
        exit 1
    fi
}

main "$@"
