#!/usr/bin/env bash
# ##########################################################################
#
#              //===//   //=====   //===//   //       //   //===//
#             //    //  //        //    //  //       //   //    //
#            //===//   //=====   //===//   //       //   //===<<
#           //   \\         //  //        //       //   //    //
#          //     \\  =====//  //        //=====  //   //===//   Version III
#
#             ###################################################
#           ======  D E M O N S T R A T I O N   S Y S T E M  ======
#             ###################################################
#
# ############# An Efficient RSerPool Prototype Implementation #############
#
# Copyright (C) 2002-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: thomas.dreibholz@gmail.com
#

# Bash options:
set -eu


if [ $# -lt 4 ] ; then
   echo >&2 "Usage: $0 output_file width height input_directory"
   exit 1
fi
OUTPUT="$1"
WIDTH="$2"
HEIGHT="$3"
DIRECTORY="$4"

GM="gm"

# ------ Proportions --------------------------------------------------------
titleWidth=0.35      # of Width
subtitleWidth=0.45   # of Width
logoWidth=0.25        # of Width
logoIndent=0.025      # of Height
websiteWidth=0.925    # of Width
websiteIndent=0.050   # of Height

titleWidthValue=$(awk "BEGIN { print int(${WIDTH}*${titleWidth}) }")
subtitleWidthValue=$(awk "BEGIN { print int(${WIDTH}*${subtitleWidth}) }")
logoWidthValue=$(LC_NUMERIC=C awk "BEGIN { print int(${WIDTH}*${logoWidth}) }")
logoIndentValue=$(LC_NUMERIC=C awk "BEGIN { print int(${HEIGHT}*${logoIndent}) }")
websiteWidthValue=$(LC_NUMERIC=C awk "BEGIN { print int(${WIDTH}*${websiteWidth}) }")
websiteIndentValue=$(LC_NUMERIC=C awk "BEGIN { print int(${HEIGHT}*${websiteIndent}) }")


tempDirectory=$(mktemp -d rserpooldemo-overlay-"${WIDTH}"x"${HEIGHT}".XXXXXXXXXXXXXXXX)

# ------ Logo for upper-right corner and bottom -----------------------------
${GM} convert "$DIRECTORY"/Logo-RSPLIB.png -trim \
   -resize "${logoWidthValue}"x \
   +compress "${tempDirectory}"/logo.png
${GM} convert "$DIRECTORY"/Website.png -trim \
   -resize "${websiteWidthValue}"x \
   +compress "${tempDirectory}"/website.png

${GM} composite -size "${WIDTH}"x"${HEIGHT}" \
   -gravity NorthEast "${tempDirectory}"/logo.png -geometry +"${logoIndentValue}"+"${logoIndentValue}" \
   xc:none +compress "${tempDirectory}"/layer-logo.png
${GM} composite -size "${WIDTH}"x"${HEIGHT}" \
   -gravity South -geometry +0+"${websiteIndentValue}" "${tempDirectory}"/website.png \
   xc:none +compress "${tempDirectory}"/layer-website.png

# ------ Center title over subtitle -----------------------------------------
${GM} convert "$DIRECTORY"/Title.png -trim -resize "${titleWidthValue}"x \
   +compress "${tempDirectory}"/title.png
${GM} convert "$DIRECTORY"/Subtitle.png -trim -resize "${subtitleWidthValue}"x \
   -bordercolor none \
   +compress "${tempDirectory}"/subtitle.png
th=$(gm identify -format "%h" "${tempDirectory}/title.png")
# Size is ${subtitleWidthValue}x${th}:
${GM} composite -size "${subtitleWidthValue}"x"${th}" \
   -gravity Center "${tempDirectory}"/title.png \
   xc:none +compress "${tempDirectory}"/title-c.png

${GM} convert -background none -append -border 25 -bordercolor transparent \
   "${tempDirectory}"/title-c.png \
   "${tempDirectory}"/subtitle.png \
   +compress "${tempDirectory}"/center.png

${GM} composite -size "${WIDTH}"x"${HEIGHT}" \
   -gravity Center "${tempDirectory}"/center.png \
   xc:none +compress "${tempDirectory}"/layer-center.png

# ------ Combine everything -------------------------------------------------
${GM} composite -size "${WIDTH}"x"${HEIGHT}" \
   "${tempDirectory}"/layer-logo.png \
   "${tempDirectory}"/layer-website.png \
   "${tempDirectory}"/layer-borders.png
${GM} composite -size "${WIDTH}"x"${HEIGHT}" \
   "${tempDirectory}"/layer-borders.png \
   "${tempDirectory}"/layer-center.png \
   "$OUTPUT"

rm -rf "${tempDirectory}"
