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


# ###### Usage ##############################################################
usage () {
   local exitCode="$1"
   echo >&2 "Usage: $0 input_filename output_filename [-X|--blur-x value] [-Y|--blur-y value] [-A|--azimut bearing] [-E|--elevation bearing] [-D|--depth depth] [-w|--verbose] [-q|--quiet] [-h|--help] [-v|--version]"
   exit "${exitCode}"
}


# ###### Version ############################################################
version () {
   echo "gs-clothify @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
   exit 0
}



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

# ====== Handle arguments ===================================================
GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)"
# shellcheck disable=SC2068
options="$(${GETOPT} -o X:Y:A:E:D:wqhv --long blur-x:,blur-y:,azimut:,elevation:,depth:,verbose,quiet,help,version -a -- "$@")"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
   usage 1
fi
eval set -- "${options}"

BLUR_X=3
BLUR_Y=3
AZIMUT=135.0
ELEVATION=45.0
DEPTH=5
VERBOSE=0
while [ $# -gt 0 ] ; do
   case "$1" in
      -X | --blur-x)
         BLUR_X="$2"
         if [[ ! "${BLUR_X}" =~ ^[0-9]+$ ]] || [ "${BLUR_X}" -lt 3 ] || [ "${BLUR_X}" -gt 100 ] ; then
            echo >&2 "ERROR: Invalid value ${BLUR_X} for -X|--blur-x!"
            exit 1
         fi
         shift 2
         ;;
      -Y | --blur-y)
         BLUR_Y="$2"
         if [[ ! "${BLUR_Y}" =~ ^[0-9]+$ ]] || [ "${BLUR_Y}" -lt 3 ] || [ "${BLUR_Y}" -gt 100 ] ; then
            echo >&2 "ERROR: Invalid value ${BLUR_Y} for -Y|--blur-y!"
            exit 1
         fi
         shift 2
         ;;
      -A | --azimut)
         AZIMUT="$2"
         if [[ ! "${AZIMUT}" =~ ^([-]{0,1})([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$  ]] || \
            awk "BEGIN { exit ( ($AZIMUT < 0.0) || ($AZIMUT > 360.0) ? 0 : 1) }" ; then
            echo >&2 "ERROR: Invalid value ${AZIMUT} for -A|--azimut!"
            exit 1
         fi
         shift 2
         ;;
      -E | --elevation)
         ELEVATION="$2"
         if [[ ! "${ELEVATION}" =~ ^([-]{0,1})([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$  ]] || \
            awk "BEGIN { exit ( ($ELEVATION < 0.5) || ($ELEVATION > 90.0) ? 0 : 1) }" ; then
            echo >&2 "ERROR: Invalid value ${ELEVATION} for -E|--elevation!"
            exit 1
         fi
         shift 2
         ;;
      -D | --depth)
         if [[ ! "${DEPTH}" =~ ^[0-9]+$ ]] || [ "${DEPTH}" -lt 1 ] || [ "${DEPTH}" -gt 65 ] ; then
            echo >&2 "ERROR: Invalid value ${DEPTH} for -D|--depth!"
            exit 1
         fi
         shift 2
         ;;
      -w | --verbose)
         VERBOSE=1
         shift
         ;;
      -q | --quiet)
         VERBOSE=0
         shift
         ;;
      -h | --help)
         usage 0
         ;;
      -v | --version)
         version
         # shift
         ;;
      --)
         shift
         break
         ;;
      *)
         usage 1
         ;;
  esac
done

if [ $# -ne 2 ] ; then
   usage 1
fi
INPUT_FILENAME="$1"
OUTPUT_FILENAME="$2"

if [ ! -f "${INPUT_FILENAME}" ] ; then
   echo >&2 "ERROR: Input file ${INPUT_FILENAME} does not exist!"
   exit 1
fi


# ====== Detect GIMP, and determine version-specific call options ===========
GIMP_CONSOLE="$(which gimp-console 2>/dev/null || true)"
if [ "${GIMP_CONSOLE}" == "" ] ; then
   echo >&2 "ERROR: GIMP is not installed!"
   echo >&2 "* Ubuntu:  sudo apt install -y gimp"
   echo >&2 "* Fedora:  sudo dnf install -y gimp"
   echo >&2 "* SuSE     sudo zypper install -y gimp"
   echo >&2 "* Alpine:  sudo apk add gimp"
   echo >&2 "* FreeBSD: sudo pkg install -y gimp3-app"
   exit 1
fi
GIMP_VERSION="$(LC_ALL=C.UTF-8 "${GIMP_CONSOLE}" --version | sed -e 's/GNU Image Manipulation Program version //')"
if [[ "${GIMP_VERSION}" =~ ^[012] ]] ; then
   # GIMP 2.x:
   GIMP_OPTIONS="--new-instance --no-interface --no-splash --batch-interpreter plug-in-script-fu-eval --batch -"
else
   # GIMP 3.x:
   GIMP_OPTIONS="--quit --new-instance --no-interface --no-splash --batch-interpreter plug-in-script-fu-eval --batch -"
fi

# This script does not use fonts, or gradients/palettes/brushes:
GIMP_OPTIONS="${GIMP_OPTIONS} --no-fonts --no-data"


# ====== Call GIMP ==========================================================
rm -f "${OUTPUT_FILENAME}"
# shellcheck disable=SC2086
( cat <<EOF
(let*
   ; ------ Initialise and load image ---------------------------------------
   ((inputFileName    "${INPUT_FILENAME}")
    (outputFileName   "${OUTPUT_FILENAME}")
    (blurX            ${BLUR_X})
    (blurY            ${BLUR_Y})
    (azimut           ${AZIMUT})
    (elevation        ${ELEVATION})
    (depth            ${DEPTH})
    (image            (car (gimp-file-load RUN-NONINTERACTIVE inputFileName inputFileName)))
    (imageType        (if (not (defined? 'gimp-image-get-active-layer))
                         ; New GIMP 3.0 API:
                         (car (gimp-image-get-base-type image))
                         ; Old GIMP 2.x API:
                         (car (gimp-image-base-type image)))
                      )
    (layer            (if (not (defined? 'gimp-image-get-active-layer))
                         ; New GIMP 3.0 API:
                         (car (list (vector-ref (car (gimp-image-get-selected-layers image)) 0)))
                         ; Old GIMP 2.x API:
                         (car (gimp-image-get-active-layer image)))
                      )
   )

   ; ------ Apply filter ----------------------------------------------------
   ; First, make sure that the image is RGB, not indexed color mode!
   (if (not (= RGB imageType))
      (gimp-image-convert-rgb image))

   (cond( (not(defined? 'plug-in-bump-map))
      ; New GIMP 3.0 API:
      (script-fu-clothify image (vector layer) blurX blurY azimut elevation depth)
   )
   (else
      ; Old GIMP 2.x API:
      (script-fu-clothify image layer blurX blurY azimut elevation depth)
   ))

   ; ------ Save result -----------------------------------------------------
   (if (defined? 'gimp-drawable-merge-new-filter)
      (begin  ; New GIMP >= 3.0 API:
         ; FIXME: This does not work when running in GIMP 2.x!
         ; (file-png-export
         ;    #:run-mode         RUN-NONINTERACTIVE
         ;    #:image            image
         ;    #:file             outputFileName
         ;    #:options          -1
         ;    #:interlaced       TRUE
         ;    #:compression      6
         ;    #:bkgd             TRUE
         ;    #:offs             FALSE
         ;    #:phys             TRUE
         ;    #:time             TRUE
         ;    #:save-transparent FALSE
         ;    #:optimize-palette TRUE
         ; )
         (file-png-export RUN-NONINTERACTIVE image
            outputFileName -1
            TRUE 6 TRUE FALSE TRUE TRUE FALSE TRUE)
      )
      (begin  ; Old GIMP < 3.0 API:
         (file-png-save2 RUN-NONINTERACTIVE image
            (car (gimp-image-get-active-layer image))
            outputFileName outputFileName
            TRUE 6 TRUE TRUE FALSE TRUE TRUE FALSE TRUE)
      )
   )

   ; ------ Clean up --------------------------------------------------------
   (gimp-image-delete image)
)
(gimp-quit TRUE)
EOF
) | env LC_ALL=C.UTF-8 HOME=/tmp "${GIMP_CONSOLE}" ${GIMP_OPTIONS} 2>&1 | \
(
   if [ ${VERBOSE} -ne 0 ] ; then
      cat
   else
      grep -vE "^ts>|(#t)|^Copyright|Welcome to (GIMP|TinyScheme)|^using gegl copy|^gimp_color_transform_new: using babl|scriptfu-WARNING|-WARNING|^Please use named arguments:|^script quit with code:|^$" || true
   fi
)


# ====== Check result =======================================================
if [ ! -e "${OUTPUT_FILENAME}" ] ; then
   echo >&2 "ERROR: ${OUTPUT_FILENAME} has not been produced. Something went wrong!"
   exit 1
fi
