#!/usr/bin/env bash
# ==========================================================================
#         ____            _                     _____           _
#        / ___| _   _ ___| |_ ___ _ __ ___     |_   _|__   ___ | |___
#        \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
#         ___) | |_| \__ \ ||  __/ | | | | |_____| | (_) | (_) | \__ \
#        |____/ \__, |___/\__\___|_| |_| |_|     |_|\___/ \___/|_|___/
#               |___/
#                             --- System-Tools ---
#                  https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# Print-UTF8
# Copyright (C) 2024-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="./print-utf8 "
# RUN="valgrind ./print-utf8 "

make ./print-utf8

failures=0
# Tests: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
for test in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ; do

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

   outputFile="print-utf8-tests/${test}.out"
   goodFile="print-utf8-tests/${test}.good"
   badFile="print-utf8-tests/${test}.bad"
   rm -f "${badFile}"

   # ====== Perform the test run ============================================
   (
      # ------ Test 00 ------------------------------------------------------
      if [ "${test}" == "00" ] ; then
         ${RUN} -n -s '<*' '=' '*>'
         for indent in -20 +20 ; do
            echo "Indented ${indent}:"
            ${RUN} -i ${indent} "Test: "
            echo "*"
            ${RUN} -i ${indent} "Test äöüß ÆØÅ: "
            echo "*"
            ${RUN} -i ${indent} "托马斯博士: "
            echo "*"
            ${RUN} -i ${indent} "😀💻: "
            echo "*"
         done

         ${RUN} -n -s '<😀' '=' '😀>'
         echo "Centered:"
         ${RUN} -n -c "Test"
         ${RUN} -n -c "\x1b[38;2;241;71;29mTest\x1b[0m"
         ${RUN} -n -c "äöüß"
         ${RUN} -n -c "托马斯博士"

         ${RUN} -n -s '<💻😀' '=' '😀💻>'

         ${RUN} -l '<💻😀====😀💻>'
         ${RUN} -b '<💻😀====😀💻>'
         ${RUN} -w '<💻😀====😀💻>'
         ${RUN} -a '<💻😀====😀💻>'

      # ------ Test 01 ------------------------------------------------------
      elif [ "${test}" == "01" ] ; then
         ./print-utf8 -h || true

      # ------ Test 02 ------------------------------------------------------
      elif [ "${test}" == "02" ] ; then
         ./print-utf8 --help || true

      # ------ Test 03 ------------------------------------------------------
      elif [ "${test}" == "03" ] ; then
         ./print-utf8 --version

      # ------ Test 04 ------------------------------------------------------
      elif [ "${test}" == "04" ] ; then
         ./print-utf8 --newline --center "This line is centered in console width!"

      # ------ Test 05 ------------------------------------------------------
      elif [ "${test}" == "05" ] ; then
         ./print-utf8 --newline --columns 80 --center "This line is centered in 80 columns!"

      # ------ Test 06 ------------------------------------------------------
      elif [ "${test}" == "06" ] ; then
         ./print-utf8 --newline --center '\x1b[38;2;241;71;29mThis line is centered!\x1b[0m'

      # ------ Test 07 ------------------------------------------------------
      elif [ "${test}" == "07" ] ; then
         figlet "This is a test!" | ./print-utf8 --newline --multiline-center "<🏁" "🏁>"

      # ------ Test 08 ------------------------------------------------------
      elif [ "${test}" == "08" ] ; then
         figlet "This is another test!" | ./print-utf8 -x -8 -n -C "<🏁" "🏁>"

      # ------ Test 09 ------------------------------------------------------
      elif [ "${test}" == "09" ] ; then
         figlet Right | ./print-utf8 -n -I +72 "😀" "😀"

      # ------ Test 10 ------------------------------------------------------
      elif [ "${test}" == "10" ] ; then
         figlet Left | ./print-utf8 -n -I -72 "😀" "😀"

      # ------ Test 11 ------------------------------------------------------
      elif [ "${test}" == "11" ] ; then
         # date +"%d. %m. %Y %H:%M:%S" | figlet -w 128 | ./print-utf8 -n -C "[🕑" "🕑]"
         echo "06. 04. 2025 14:28:56" | figlet -w 128 | ./print-utf8 -n -C "[🕑" "🕑]"

      # ------ Test 12 ------------------------------------------------------
      elif [ "${test}" == "12" ] ; then
         ./print-utf8 -n -c "😀😀😀 This line is centered in console width! 😀😀😀"

      # ------ Test 13 ------------------------------------------------------
      elif [ "${test}" == "13" ] ; then
         ./print-utf8 -n -x 80 -c "😀😀😀 This line is centered in 80 columns! 😀😀😀"

      # ------ Test 14 ------------------------------------------------------
      elif [ "${test}" == "14" ] ; then
         ./print-utf8 --indent -32 "Left: " && echo "1234"

      # ------ Test 15 ------------------------------------------------------
      elif [ "${test}" == "15" ] ; then
         ./print-utf8 --indent +32 "Right: " && echo "1234"

      # ------ Test 16 ------------------------------------------------------
      elif [ "${test}" == "16" ] ; then
         ./print-utf8 --newline --separator "<*" "=" "*>"

      # ------ Test 17 ------------------------------------------------------
      elif [ "${test}" == "17" ] ; then
         ./print-utf8 --columns 80 --newline --separator "<*" "=" "*>"

      # ------ Test 18 ------------------------------------------------------
      elif [ "${test}" == "18" ] ; then
         ./print-utf8 -n -s "<💻😀" "=" "😀💻>"

      # ------ Test 19 ------------------------------------------------------
      elif [ "${test}" == "19" ] ; then
         ./print-utf8 --width "Test"
         ./print-utf8 --width "Test äöü"

      # ------ Test 20 ------------------------------------------------------
      elif [ "${test}" == "20" ] ; then
         ./print-utf8 -b "😀 Test 😀"
         ./print-utf8 -l "😀 Test 😀"
         ./print-utf8 -w "😀 Test 😀"
         ./print-utf8 -a "😀 Test 😀"

      # ------ Test 21 ------------------------------------------------------
      elif [ "${test}" == "21" ] ; then
         ./print-utf8 --terminal-info

      # ------ Test 22 ------------------------------------------------------
      elif [ "${test}" == "22" ] ; then
         ./print-utf8 -n -s "\e[1;34m╔" "═" "╗\e[0m" ;
         # date +"%d. %m. %Y %H:%M:%S"
         echo "06. 04. 2025 14:28:56" | figlet -w 128 | \
         ./print-utf8 -n -C "\e[1;34m║🕑\e[1;31m" "\e[1;34m🕑║\e[0m" ;
         ./print-utf8 -n -s "\e[1;34m╚" "═" "╝\e[0m"

      # ------ Test 23 ------------------------------------------------------
      elif [ "${test}" == "23" ] ; then
         ./print-utf8 -n -s "\e[1;31;5m█" "▀" "█\e[0m" ;
         echo "ERROR  1234" | figlet -w 128 | ./print-utf8 -n -C "\e[1;31;5m█\e[25m" "\e[5m█\e[0m" ;
         ./print-utf8 -n -s "\e[1;31;5m█" "▄" "█\e[0m"

      # ------ Test 24 ------------------------------------------------------
      elif [ "${test}" == "24" ] ; then
         ./print-utf8 -n -s "\e[1;31;40;5m█" "▀" "█\e[0m" ;
         echo -e "Software Failure.   Press left mouse button to continue.\nGuru Meditation #00000004.48454C50" | \
         ./print-utf8 -n -C "\e[1;31;40;5m█\e[25m" "\e[5m█\e[0m" ;
         ./print-utf8 -n -s "\e[1;31;40;5m█" "▄" "█\e[0m"

      # ------ Test 25 ------------------------------------------------------
      elif [ "${test}" == "25" ] ; then
         ./print-utf8 -n -x 44 -s "\e[1;32m╒" "═" "╕\e[0m" ;
         # shellcheck disable=SC2012
         ls / | ./print-utf8 -n --multiline-indent -42 "\e[1;32m│\e[34m" "\e[1;32m│\e[0m" ;
         ./print-utf8 -n -x 44 -s "\e[1;32m╘" "═" "╛\e[0m"

      else
         echo >&2 "ERROR: Undefined test!"
         exit 1
      fi
   ) >"${outputFile}" 2>&1


   # ====== 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 ""
            ./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
