#!/bin/sh
# script/fmt: format all files (writes).
set -eu

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

# Regenerate mfer/mf.pb.go from mfer/mf.proto if it is missing or stale
# (mirrors the old Makefile prerequisite; the generated file is
# committed, so this is normally a no-op).
ensure_pb() {
    if [ ! -f mfer/mf.pb.go ] ||
        [ -n "$(find mfer/mf.proto -newer mfer/mf.pb.go 2>/dev/null)" ]; then
        (cd mfer && go generate .)
    fi
}

main() {
    cd "$ROOT"
    ensure_pb
    gofumpt -l -w mfer internal cmd
    golangci-lint run --fix
    # Markdown and JSON, over the same file set script/fmt-check verifies.
    "$SCRIPT_DIR/prettier" --write
}

main "$@"
