#!/bin/sh
# script/lint: run the linter. Inside a container, run it directly;
# on a host, build Dockerfile.lint so it runs in one anyway. The linter
# is never run on a developer host, where a shared result cache, a
# host-global lock and a stale toolchain make its answer untrustworthy.
#
# LINT_IN_CONTAINER is set by this repo's Dockerfiles and is the ONLY
# accepted signal. Do not add a /.dockerenv fallback: it is absent
# inside BuildKit RUN steps and present on hosts that are themselves
# containers, so it both misses and false-positives — and a false
# positive silently restores host linting.
set -eu

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

main() {
    cd "$ROOT"

    if [ "${LINT_IN_CONTAINER:-}" = "1" ]; then
        exec yarn run prettier --check '**/*.md' \
            --tab-width 4 --prose-wrap always
    fi

    # Own line, and `$$` because busybox `date` drops %N silently.
    # Without a fresh nonce the lint layer is cached and this exits 0
    # having linted nothing.
    epoch="$(date +%s%N)$$"
    docker build \
        --build-arg CHECK_EPOCH="$epoch" \
        -f Dockerfile.lint \
        .
}

main "$@"
