#!/usr/bin/env bash

# force-enable double star globbing
shopt -s globstar

# Perform spell checking on the docs

if [[ ${1:-} == "--help" ]] || [[ ${1:-} == "-h" ]]; then
    echo "Usage $0 [--help|-h] [-i]"
    echo "  --help|-h: print this help"
    echo "  -i: interactive mode"
    exit 0
fi

ROOT_PATH=$(git rev-parse --show-toplevel)

CHECK_LANG=en

ASPELL_IGNORE_PATH="${ROOT_PATH}/utils/check-style/aspell-ignore/${CHECK_LANG}"

STATUS=0
for fname in ${ROOT_PATH}/docs/${CHECK_LANG}/**/*.md; do
    if [[ ${1:-} == "-i" ]]; then
        echo "Checking $fname"
        aspell --personal=aspell-dict.txt --add-sgml-skip=code --encoding=utf-8 --mode=markdown -W 3 --lang=${CHECK_LANG} --home-dir=${ASPELL_IGNORE_PATH} -c "$fname"
        continue
    fi

    errors=$(cat "$fname" \
        | aspell list \
            -W 3 \
            --personal=aspell-dict.txt \
            --add-sgml-skip=code \
            --encoding=utf-8 \
            --mode=markdown \
            --lang=${CHECK_LANG} \
            --home-dir=${ASPELL_IGNORE_PATH} \
        | sort | uniq)
    if [ ! -z "$errors" ]; then
        STATUS=1
        echo "====== $fname ======"
        echo "$errors"
    fi
done

if (( STATUS != 0 )); then
    echo "====== Errors found ======"
    echo "To exclude some words add them to the dictionary file \"${ASPELL_IGNORE_PATH}/aspell-dict.txt\""
    echo "You can also run ${0} -i to see the errors interactively and fix them or add to the dictionary file"
fi

exit ${STATUS}