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


# ###### Make banner with equal-length lines ################################
function make-banner ()
{
   local bannerText="$1"

   # ====== Create banner with figlet, toilet or echo as fallback ===========
   (
      figlet -w 256 -f standard "${bannerText}" 2>/dev/null || \
      toilet -w 256 -f standard "${bannerText}" 2>/dev/null || \
      if [ ${#bannerText} -le 10 ] ; then sysvbanner "${bannerText}" ; else false ; fi || \
      echo "${bannerText}"
   ) | (
   # ====== Compute maximum line length and pad banner lines to this length ====
      # Array to store the lines of the banner:
      banner=( )

      # Read lines, and compute the maximum line length
      maxLineLength=0
      while IFS= read -r line ; do
         if [ ${#line} -gt "${maxLineLength}" ] ; then
            maxLineLength=${#line}
         fi
         banner+=( "${line}" )
      done

      # Print maximum-length lines padded with spaces
      for line in "${banner[@]}" ; do
         printf "%-${maxLineLength}s\n" "${line}"
      done
   )
}


# ###### Main program #######################################################
if [ $# -lt 1 ] ; then
   echo >&2 "Usage: $0 banner_text [...]"
   exit 1
fi
while [ $# -gt 0 ] ; do
   make-banner "$1" | while IFS= read -r line ; do
      ./print-utf8 --newline --columns 0 --center "${line}"
   done
   shift
done
