#!/bin/sh
# script/fmt: format all files (writes): the Go sources with gofmt, the
# Markdown with prettier. prettier is never installed on the host: it
# runs from the Dockerfile's prettier stage with the repository mounted,
# as the calling user so the files it rewrites keep their owner. The tag
# makes each build replace the previous image instead of leaving another
# one behind.
set -eu

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

main() {
    cd "$ROOT"
    gofmt -s -w .
    image="$("$SCRIPT_DIR/projectname")-prettier"
    docker build -q --target prettier -t "$image" . >/dev/null
    docker run --rm --user "$(id -u):$(id -g)" -v "$ROOT:/src" "$image" \
        prettier --write '**/*.md' --tab-width 4 --prose-wrap always
}

main "$@"
