#!/usr/bin/env bash
# Run test with different package options.

# SPDX-FileCopyrightText: 2015-2023 Robin Schneider <ypid@riseup.net>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

function containsElement() {
    local e match="$1"
    shift
    for e; do [[ "$e" == "$match" ]] && return 0; done
    return 1
}

function main() {
    test_only_those_options=( "$@" )

    supported_licenses="$(grep '.*&.*&.*@nameuse{doclicense@lang' ./doclicense.dtx | sed 's/.*doclicense@lang@lic@\([^@]\+\)@\([^@]\+\)@\([^]]\+\)}.*/\1 \2 \3/' | sort -u)"

    echo "$supported_licenses" | \
        while read -r type modifier version; do
        for imagemodifier in "" "-eu" "-80x15"; do
            for lang in "ngerman" "english" "brazilian" "spanish" "french" "russian" "italian" "polish" "portuguese" "catalan" "galician" "croatian" "swedish" "esperanto" "ukrainian" "austrian" "naustrian" "swissgerman" "nswissgerman" "greek"; do
                if [ "$lang" == "ngerman" ] && [ "$modifier" == "pd" ]; then
                    continue # Does not exist in Germany.
                fi
                if [ "$lang" == "ngerman" ] && [ "$modifier" == "pd" ]; then
                    continue # Does not exist in Germany.
                fi
                if [ "$imagemodifier" == "-eu" ] && [[ "$modifier" != *-nc-* ]]; then
                    continue # eu (Euro) does only exist for nc (non commercial).
                fi
                if [[ ${#test_only_those_options[@]} != 0 ]] && \
                    ! containsElement "$lang" "${test_only_those_options[@]}"; then

                    continue
                fi

                (

                echo "
                    \documentclass[$lang]{article}
                    \usepackage{
                      iftex,
                    }

                    \ifPDFTeX%
                      \usepackage[utf8]{inputenc}
                      \usepackage[T1]{fontenc}
                      \usepackage{babel}
                    \fi
                    \ifLuaTeX%
                      \usepackage{
                        fontspec,
                        polyglossia,
                      }
                    \fi

                    \usepackage{
                        hyperref,
                        hyperxmp,
                    }
                    \ifLuaTeX%
                      \setdefaultlanguage{$lang}
                    \fi
                    \usepackage[
                        type={$type},
                        modifier={$modifier},
                        version={$version},"
                if [ "$imagemodifier" != "" ]; then
                    echo "                    imagemodifier={$imagemodifier},% chktex 29"
                fi
                echo "                ]{doclicense}
                    \begin{document}
                    doclicenseType
                    \doclicenseType%

                    doclicenseLongType
                    \doclicenseLongType%

                    doclicenseModifier
                    \doclicenseModifier%

                    doclicenseVersion
                    \doclicenseVersion%

                    doclicenseURL
                    \doclicenseURL%

                    doclicenseName
                    \doclicenseName%

                    doclicenseLongName
                    \doclicenseLongName%

                    doclicenseNameRef
                    \doclicenseNameRef%

                    doclicenseLongNameRef
                    \doclicenseLongNameRef%

                    doclicenseText
                    \doclicenseText%

                    doclicenseLongText
                    \doclicenseLongText%

                    doclicenseIcon
                    \doclicenseIcon%

                    doclicenseTypeIcon
                    \doclicenseTypeIcon%

                    doclicenseImage
                    \doclicenseImage%

                    doclicenseImageFileName
                    \doclicenseImageFileName%

                    doclicenseThis
                    \doclicenseThis%

                    doclicensePlainFullText
                    \doclicensePlainFullText%

                    doclicenseFullText
                    \doclicenseFullText%
                    \end{document}"

                ) > testing.tex

                echo "*** Testing variant: $lang $type $modifier $version $imagemodifier"

                # latexmk -c
                rm -f testing.aux testing.log testing.out testing.pdf

                texfot pdflatex -no-shell-escape -interaction=nonstopmode -halt-on-error testing.tex | egrep --invert-match 'Overfull|Underfull'
            done
        done
    done
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
    PS4='+ $(date --rfc-3339=seconds), ${BASH_SOURCE-}:${LINENO-}: '
    export PS4
    trap 'echo Encountered an unexpected error. Exiting with exit code $? in ${BASH_SOURCE-}:${LINENO-}. >&2' ERR
    set -o nounset -o pipefail -o errexit -o errtrace

    main "$@"
fi