#!/usr/bin/env bash
# ==========================================================================
#         ____            _                     _____           _
#        / ___| _   _ ___| |_ ___ _ __ ___     |_   _|__   ___ | |___
#        \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
#         ___) | |_| \__ \ ||  __/ | | | | |_____| | (_) | (_) | \__ \
#        |____/ \__, |___/\__\___|_| |_| |_|     |_|\___/ \___/|_|___/
#               |___/
#                             --- System-Tools ---
#                  https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Text-Block
# Copyright (C) 2025 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com

set -eu

RUN=""
# RUN="valgrind "

make text-block
insertFile="text-block-tests/insert.txt"
failures=0
# Tests: simple markers advanced1 advanced2 example1 example2
for test in simple markers advanced1 advanced2 example1 example2 ; do
   # Modes: cat discard enumerate highlight extract remove insert-front insert-back replace
   for mode in cat discard enumerate highlight extract remove insert-front insert-back replace ; do
      for o1 in include-tags exclude-tags ; do
         for o2 in tags-only full-tag-lines ; do

            # ====== Prepare test run =======================================
            ./print-utf8 -x 86 -s "\x1b[34m###### Testing: ${test} --${mode} --${o1} --${o2} " "#" "#\x1b[0m   " >&2

            # ------ Default parameters -------------------------------------
            inputFile="text-block-tests/${test}.txt"
            outputFile="text-block-tests/${test}-${mode}-${o1}-${o2}.out"
            goodFile="text-block-tests/${test}-${mode}-${o1}-${o2}.good"
            badFile="text-block-tests/${test}-${mode}-${o1}-${o2}.bad"
            beginTag="<BEGIN-BLOCK>"
            endTag="<END-BLOCK>"
            rm -f "${badFile}"

            # ------ Special cases ------------------------------------------
            if [ "${test}" == "advanced1" ] ; then
               inputFile="text-block-tests/advanced.txt"
            elif [ "${test}" == "advanced2" ] ; then
               inputFile="text-block-tests/advanced.txt"
               beginTag="<MARKER>"
               endTag="<MARKER>"
            elif [ "${test}" == "example2" ] ; then
               beginTag="<MARKER>"
               endTag="<MARKER>"
            fi

            m="${mode}"
            if [ "${mode}" == "discard" ] ; then
               touch "${goodFile}"   # There are no empty files in Git!
            elif [ "${mode}" == "extract" ] ; then
               if [ "${beginTag}" == "${endTag}" ] && [ "${o1}" == "exclude-tags" ] ; then
                  touch "${goodFile}"   # There are no empty files in Git!
               fi
            elif [ "${mode}" == "replace" ] || [ "${mode}" == "insert-front" ] || [ "${mode}" == "insert-back" ] ; then
               m="${mode} ${insertFile}"   # Add insert file name as parameter
            fi


            # ====== Perform the test run ===================================
            command="${RUN} ./text-block -q -i ${inputFile} -o ${outputFile} -b '${beginTag}' -e '${endTag}' --highlight-unmarked1 $'\x1b[34mu' --highlight-unmarked2 $'U\x1b[0m' --highlight-marked1 $'m\x1b[31m' --highlight-marked2 $'M\x1b[0m' --${m} --${o1} --${o2}"
            bash -c "${command}"

            # ====== Check results ==========================================
            if [ -e "${goodFile}" ] ; then
               if ! diff -q "${goodFile}" "${outputFile}" >/dev/null ; then
                  failures=$((failures+1))
                  (
                     echo ""
                     ./print-utf8 -n -s "\e[1;31;5m█" "▀" "█\e[0m" ;
                     echo "TEST FAILED!" | figlet -w 64 | ./print-utf8 -n -C "\e[1;31;5m█\e[25m" "\e[5m█\e[0m" ;
                     ./print-utf8 -n -s "\e[1;31;5m█" "▄" "█\e[0m"
                     echo "Failure #${failures}:"
                     echo "Command:${command}"
                     echo ""
                     ./print-utf8 -x 78 -n -s  "\x1b[33m------ Good ($(basename "${goodFile}")) " "-" "-\x1b[0m"
                     cat "${goodFile}"
                     echo ""
                     ./print-utf8 -x 78 -n -s  "\x1b[35m------ Output ($(basename "${outputFile}")) " "-" "-\x1b[0m"
                     cat "${outputFile}"
                     echo ""
                     ./print-utf8 -x 78 -n -s  "\x1b[31m------ Differences: " "-" "-\x1b[0m"
                     diff --color "${goodFile}" "${outputFile}" || true
                     echo ""
                     cp "${outputFile}" "${badFile}"
                  ) >&2
                  exit 1
               else
                  echo -e "\x1b[33mOKAY!\x1b[0m"
               fi
            else
               echo -e "\x1b[35mN/A!\x1b[0m"
               echo "*** No good file for $(basename "${outputFile}")! ***"
            fi
         done
      done
   done
done
