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

# gettext options:
export TEXTDOMAIN="try-hard"
# export TEXTDOMAINDIR="${PWD}/locale"   # Default: "/usr/share/locale"

# shellcheck disable=SC1091
. gettext.sh


# ###### Usage ##############################################################
usage () {
   local exitCode="$1"
   echo >&2 "$(gettext "Usage:") $0 max_trials max_delay [-m|--min-delay min_delay] [-A|--additive-increase delay_increment] [-M|multiplicative-increase delay_factor] [-T|--truncate-delay truncated_max_delay] [-D|--deterministic] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version] -- command ..."
   exit "${exitCode}"
}


# ###### Version ############################################################
version () {
   echo "try-hard 2.5.0"
   exit 0
}



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

# ====== Handle arguments ===================================================
GETOPT="$(PATH=/usr/local/bin:${PATH} which getopt)"
options="$(${GETOPT} -o m:A:M:T:Dqwhv --long min-delay:,additive-increase:,multiplicative-increase:,truncate-delay:,deterministic,quiet,verbose,help,version -a -- "$@")"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
   usage 1
fi
eval set -- "${options}"

VERBOSE=0
MIN_DELAY="0.0"
ADDITIVE_INCREASE="0.0"
MULTIPLICATIVE_INCREASE="1.0"
TRUNCATE_DELAY="31536000.0"   # 1 year
DETERMINISTIC=0
while [ $# -gt 0 ] ; do
   case "$1" in
      -m | --min-delay)
         MIN_DELAY="$2"
         if [[ ! "${MIN_DELAY}" =~ ^([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
            eval_gettext >&2 "ERROR: Invalid delay value \$2!"
            echo >&2
            exit 1
         fi
         shift 2
         ;;
      -A | --additive-increase)
         ADDITIVE_INCREASE="$2"
         if [[ ! "${ADDITIVE_INCREASE}" =~ ^([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
            eval_gettext >&2 "ERROR: Invalid delay value \$2!"
            echo >&2
            exit 1
         fi
         shift 2
         ;;
      -M | --multiplicative-increase)
         MULTIPLICATIVE_INCREASE="$2"
         if [[ ! "${MULTIPLICATIVE_INCREASE}" =~ ^([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
            eval_gettext >&2 "ERROR: Invalid delay value \$2!"
            echo >&2
            exit 1
         fi
         shift 2
         ;;
      -T | --truncate-delay)
         TRUNCATE_DELAY="$2"
         if [[ ! "${TRUNCATE_DELAY}" =~ ^([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
            eval_gettext >&2 "ERROR: Invalid delay value \$2!"
            echo >&2
            exit 1
         fi
         shift 2
         ;;
      -D | --deterministic)
         DETERMINISTIC=1
         shift
         ;;
      -q | --quiet)
         VERBOSE=0
         shift
         ;;
      -w | --verbose)
         VERBOSE=1
         shift
         ;;
      -h | --help)
         usage 0
         # shift
         ;;
      -v | --version)
         version
         # shift
         ;;
      --)
         shift
         break
         ;;
  esac
done
if [ $# -lt 3 ] ; then
   usage 1
fi

MAX_TRIALS="$1"
if [[ ! "${MAX_TRIALS}" =~ ^[0-9]*$ ]] || [ "${MAX_TRIALS}" -lt 1 ] ; then
   eval_gettext >&2 "ERROR: Invalid maximum trials \${MAX_TRIALS}!"
   echo >&2
   exit 1
fi
MAX_DELAY="$2"
if [[ ! "${MAX_DELAY}" =~ ^([0-9]+|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)$ ]] ; then
   eval_gettext >&2 "ERROR: Invalid delay value \$2!"
   echo >&2
   exit 1
fi
shift 2


# ====== Ensure that min delay < max delay ==================================
read -r minDelay maxDelay <<<"$(awk "\
function min(a, b) { return (a < b ? a : b); }
function max(a, b) { return (a > b ? a : b); }
BEGIN {
   minDelay=min(${MIN_DELAY}, ${MAX_DELAY});
   maxDelay=max(${MIN_DELAY}, ${MAX_DELAY});
   printf(\"%g %g\n\", minDelay, maxDelay);
}
"
)"


# ====== Run loop ===========================================================
trial=0
while true ; do
   # ====== Start next trial ================================================
   trial=$(( trial+1 ))
   if [ "${VERBOSE}" -eq 1 ] ; then
      echo -e "\x1b[34m$(date +%FT%H:%M:%S): $(eval_gettext "Trial \${trial}/\${MAX_TRIALS}: \x1b[37m$*")\x1b[0m"
   fi

   # ====== Run the command =================================================
   # shellcheck disable=SC2068
   $@ && exit

   # ====== Check whether trial limit has been reached ======================
   if [ ${trial} -ge "${MAX_TRIALS}" ] ; then
      if [ "${VERBOSE}" -eq 1 ] ; then
         echo -e "\x1b[34m$(date +%FT%H:%M:%S): $(eval_gettext "Tried \${MAX_TRIALS} times -> giving up now!")\x1b[0m"
      fi
      exit 1
   fi

   # ====== Increase delay for next trial ===================================
   if [ ${trial} -gt 1 ] ; then
      read -r minDelay maxDelay <<<"$(awk "\
function min(a, b) { return (a < b ? a : b); }
function max(a, b) { return (a > b ? a : b); }
BEGIN {
   minDelay=min(${minDelay}, ${maxDelay});
   maxDelay=max(${minDelay}, ${maxDelay});
   additiveIncrease=${ADDITIVE_INCREASE};
   multiplicativeIncrease=${MULTIPLICATIVE_INCREASE};
   minDelayBoundary=${MIN_DELAY};
   maxDelayBoundary=${TRUNCATE_DELAY};

   newMinDelay=max(min(minDelay*multiplicativeIncrease + additiveIncrease, maxDelayBoundary), minDelayBoundary);
   newMaxDelay=max(min(maxDelay*multiplicativeIncrease + additiveIncrease, maxDelayBoundary), minDelayBoundary);
   printf(\"%g %g\n\", min(newMinDelay, newMaxDelay),max(newMinDelay, newMaxDelay));
}
")"
   fi

   # ====== Wait a random time span =========================================
   if [ "${DETERMINISTIC}" -eq 0 ] ; then
      if [ "${VERBOSE}" -eq 1 ] ; then
         echo -e "\x1b[34m$(date +%FT%H:%M:%S): $(eval_gettext "Trial \${trial}/\${MAX_TRIALS} failed -> waiting random time from [\${minDelay}, \${maxDelay}] s ...")\x1b[0m"
         random-sleep -w "${minDelay}" "${maxDelay}"
      else
         random-sleep "${minDelay}" "${maxDelay}"
      fi
   else
      if [ "${VERBOSE}" -eq 1 ] ; then
         echo -e "\x1b[34m$(date +%FT%H:%M:%S): $(eval_gettext "Trial \${trial}/\${MAX_TRIALS} failed -> waiting for \${maxDelay} s ...")\x1b[0m"
      fi
      sleep "${maxDelay}"
   fi
done
