#!/bin/sh
# script/fmt-check: check formatting (read-only): gofmt for Go, prettier
# for markdown.
set -eu

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

GO_DIRS="cmd internal pkg"

# Debian and Ubuntu name the yarn command yarnpkg.
run_yarn() {
    if command -v yarn >/dev/null 2>&1; then
        yarn "$@"
    elif command -v yarnpkg >/dev/null 2>&1; then
        yarnpkg "$@"
    else
        echo "no yarn; run script/bootstrap first" >&2
        exit 1
    fi
}

main() {
    cd "$ROOT"
    # shellcheck disable=SC2086 # GO_DIRS is a word list on purpose
    unformatted="$(gofmt -l $GO_DIRS)"
    if [ -n "$unformatted" ]; then
        echo "gofmt: the following files need formatting:" >&2
        echo "$unformatted" >&2
        exit 1
    fi
    run_yarn run prettier --check '**/*.md' --tab-width 4 --prose-wrap always
}

main "$@"
