#!/usr/bin/env bash
# ==========================================================================
#         ____            _                     _____           _
#        / ___| _   _ ___| |_ ___ _ __ ___     |_   _|__   ___ | |___
#        \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __|
#         ___) | |_| \__ \ ||  __/ | | | | |_____| | (_) | (_) | \__ \
#        |____/ \__, |___/\__\___|_| |_| |_|     |_|\___/ \___/|_|___/
#               |___/
#                             --- System-Tools ---
#                  https://www.nntb.no/~dreibh/system-tools/
# ==========================================================================
#
# X.509 Tools
# Copyright (C) 2025-2026 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: dreibh@simula.no

# Bash options:
set -eu


CA="TestCA/TestLevel1/certs/TestLevel1.crt"
CRL="TestCA/TestGlobal.crl"   # combined CRL!
CERTIFICATES="$(find TestCA -name "*.crt" | sort)"


# ###### Helper function to show failure ####################################
exit-failed ()
{
   echo
   "${PRINT_UTF8}" -n -s "\e[1;31;5m█" "▀" "█\e[0m" ;
   echo "CHECK FAILED!" | "${FIGLET}" -w "${COLUMNS}" | "${PRINT_UTF8}" -n -C "\e[1;31;5m█\e[25m" "\e[5m█\e[0m" ;
   "${PRINT_UTF8}" -n -s "\e[1;31;5m█" "▄" "█\e[0m"
   exit 1
}


# ###### Helper function to show success ####################################
exit-success ()
{
   echo
   "${PRINT_UTF8}" -n -s "\e[1;32m█" "▀" "█\e[0m" ;
   echo "ALL CHECKS SUCCEEDED!" | "${FIGLET}" -w "${COLUMNS}" | "${PRINT_UTF8}" -n -C "\e[1;32m█\e[25m" "█\e[0m" ;
   "${PRINT_UTF8}" -n -s "\e[1;32m█" "▄" "█\e[0m"
   exit 0
}


# ###### Helper function to show script information #########################
show-script ()
{
   local script="$1"

   "${PRINT_UTF8}" -n -s "\e[1;36m█" "▀" "█\e[0m" ;
   "${FIGLET}" -w "${COLUMNS}" "$(basename "${script}")" | "${PRINT_UTF8}" -n -C "\e[1;36m█\e[25m" "█\e[0m" ;
   "${PRINT_UTF8}" -n -s "\e[1;36m█" "▄" "█\e[0m"
}



# ###### Main program #######################################################

# ====== Find tools =========================================================
PRINT_UTF8="$(which print-utf8 2>/dev/null || true)"
if [ ! -x "${PRINT_UTF8}" ] ; then
   echo >&2 "ERROR: Print-UTF8 (from System-Tools) is not installed!"
   echo >&2 "Try this:"
   echo >&2 "* Ubuntu:  sudo apt-add-repository -sy ppa:dreibh/ppa"
   echo >&2 "           sudo apt install -y td-system-tools"
   echo >&2 "* Fedora:  sudo dnf copr enable -y dreibh/ppa"
   echo >&2 "           sudo dnf install -y td-system-tools"
   echo >&2 "* FreeBSD: sudo pkg install -y td-system-tools"
   exit 1
fi
FIGLET="$(which figlet 2>/dev/null || true)"
if [ ! -x "${FIGLET}" ] ; then
   echo >&2 "ERROR: Figlet is not installed!"
   echo >&2 "* Ubuntu:  sudo apt install -y figlet"
   echo >&2 "* Fedora:  sudo dnf install -y figlet"
   echo >&2 "* FreeBSD: sudo pkg install -y figlet"
   exit 1
fi

# ====== Run tests ==========================================================
for certificate in ${CERTIFICATES} ; do
   show-script "${certificate}"
   ./check-certificate "${CA}" "${certificate}" --crl "${CRL}" || exit-failed
done

exit-success
