#!/usr/bin/env bash

set -o pipefail

GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
VALE_MIN_ALERT_LEVEL=${VALE_MIN_ALERT_LEVEL:-}
ERROR_RESULTS=0

echo "Lint prose"
if command -v vale >/dev/null 2>&1; then
    args=()
    if [ -n "${VALE_MIN_ALERT_LEVEL}" ]; then
        args+=("--minAlertLevel" "${VALE_MIN_ALERT_LEVEL}")
    fi
    vale --config "${GIT_ROOT}/.vale.ini" "${args[@]}" "${GIT_ROOT}/docs" || ((ERROR_RESULTS++))
else
    echo "Vale is missing, please install it from https://vale.sh/docs/vale-cli/installation/"
fi

echo "Lint Markdown"
if command -v markdownlint-cli2 >/dev/null 2>&1; then
    markdownlint-cli2 'docs/**/*.md' || ((ERROR_RESULTS++))
else
    echo "markdownlint-cli2 is missing, please install it from https://github.com/DavidAnson/markdownlint-cli2#install"
fi

echo "Check links"
if command -v lychee >/dev/null 2>&1; then
   lychee --offline --include-fragments docs || ((ERROR_RESULTS++))
else
    echo "Lychee is missing, please install it from https://lychee.cli.rs/installation/"
fi

if [ "${ERROR_RESULTS}" -ne 0 ]; then
    echo "✖ ${ERROR_RESULTS} lint test(s) failed. Review the log carefully to see full listing."
    exit 1
else
    echo "✔ Linting passed"
    exit 0
fi
