#!/bin/bash
#
# script-name
# Author <email@example.com> 2020-02-02
# Last updated 2025-11-16
#
# Quick description of what the script does.
#
###############################################################################
#
# Portions of the script are from a template created by John Simpson, who has
# released them under the MIT License.
#
# MIT License
#
# Copyright (C) 2025 John Simpson
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
###############################################################################
#
# The remaining contents of the script are covered under this license:
#
# (License Name)
#
# Copyright (C) 2025 whoever
#
# Details about this script's licensing
#
###############################################################################

set -e

PATH="/usr/bin:/bin"

########################################
# Global variables
#   MYDIR   '/Users/jms1/bin'
#   MYNAME  'xyzzy'
#   MYSELF  '/Users/jms1/bin/xyzzy'

MYDIR="$(cd -- "$(dirname -- "$0")" >/dev/null 2>&1 || exit 1; pwd -P)"
MYNAME="$(basename -- "$0")"
MYSELF="${MYDIR}/${MYNAME}"

###############################################################################
#
# Coloured line functions

############################################################
# Slice and dice a message into lines, print each line in the selected colour
# - first parameter is colour, will be inserted into "ESC [ ... m" sequence
# - if more parmeters are present, each is a line (or multi-line)
# - if no more parameters are present, STDIN will be split into lines

function coloured_line {
    local COL P IFS LINE LINES

    COL="${1:?no colour code specified}"
    shift

    if [[ -n "$1" ]]
    then
        ########################################
        # each parameter is a line

        for P in "$@"
        do
            IFS=$'\n' read -a LINES -d '' -r <<< "$P" || true
            for LINE in "${LINES[@]}"
            do
                printf "\e[%sm%s\e[0K\e[0m\n" "$COL" "$LINE"
            done
        done
    else
        ########################################
        # STDIN will contain one or more lines of text

        while IFS='' read -r LINE
        do
            printf "\e[%sm%s\e[0K\e[0m\n" "$COL" "$LINE"
        done
    fi
}

############################################################
# Wrappers which specify different colours

function redline {
    coloured_line '0;1;37;41' "$@"
}

function yellowline {
    coloured_line '0;30;43' "$@"
}

function blueline {
    coloured_line '0;1;37;44' "$@"
}

function greenline {
    coloured_line '0;1;37;42' "$@"
}

function cyanline {
    coloured_line '0;1;37;46' "$@"
}

function purpleline {
    coloured_line '0;1;37;45' "$@"
}

function whiteline {
    coloured_line '0;1;37;47' "$@"
}

function fail {
    redline "$@"
    exit 1
}

###############################################################################
#
# Maybe print a command before executing it

SET_X="${SET_X:-false}"

function set_x {
    local IFS=' '
    if [[ "${SET_X:-false}" == "true" ]]
    then
        cyanline "$PS4$*" 1>&2
    fi
    "$@"
}

########################################
# Show the same output that set_x would show,
# but don't actually run the command

function show_x {
    local IFS=' '
    if [[ "${SET_X:-false}" == "true" ]]
    then
        cyanline "$PS4$*" 1>&2
    fi
}

###############################################################################
#
# Debugging message

DO_DEBUG="${DO_DEBUG:-false}"
DEBUG_PREFIX="LOC"

function debug {
    local IFS LINE LINES PREFIX

    if [[ "${DO_DEBUG:-false}" == "true" ]]
    then
        if [[ "$DEBUG_PREFIX" == "LOC" ]]
        then
            local CALLER C_LINE C_FILE
            CALLER="$( caller )"
            C_LINE="${CALLER% *}"
            C_FILE="${CALLER#* }"
            PREFIX="$C_FILE:$C_LINE: "
        else
            PREFIX="$DEBUG_PREFIX"
        fi

        IFS=$'\n' read -a LINES -d '' -r <<< "$*" || true
        for LINE in "${LINES[@]}"
        do
            yellowline "$PREFIX$LINE"
        done
    fi
}

###############################################################################
#
# Usage
# - https://docopt.org/ is the "standard" for how to describe options

function usage {
    local MSG="${*:-}"

    cat <<EOF
$0 [OPTIONS] [ARGS]

Detailed description of what the script does.

-b      Enable BVAR

-s ___  Specify SVAR

-x      Show the important commands before executing them.

-D      Show debugging messages. Also enables '-x'.

-h      Show this help message.

Note: two arguments are required in order to do anything.

Last updated 2025-11-16

EOF

    if [[ -n "$MSG" ]]
    then
        fail "$MSG"
    fi

    exit 0
}

###############################################################################
###############################################################################
###############################################################################
#
# Process the command line

BVAR=false
SVAR="(default)"

while getopts ':hbs:xD' OPT
do
    case $OPT in
        h)  usage
            ;;
        b)  BVAR=true
            ;;
        s)  SVAR="$OPTARG"
            ;;
        x)  SET_X=true
            ;;
        D)  DO_DEBUG=true
            SET_X=true
            ;;
        *)  usage "ERROR: unknown option '-${OPTARG}'"
            ;;
    esac
done

shift $(( OPTIND - 1 ))

ARG1="${1:-}"
ARG2="${2:-}"

if [[ -z "$ARG1" ]]
then
    usage
fi

if [[ -z "$ARG2" ]]
then
    usage "ERROR: missing ARG2"
fi

###############################################################################
#
# Do the things

echo "MYDIR='${MYDIR}'"
echo "MYNAME='${MYNAME}'"
echo "MYSELF='${MYSELF}'"
echo "CONFIG_DIR='${CONFIG_DIR}'"
echo "CONFIG_FILE='${CONFIG_FILE}'"
echo "BVAR='${BVAR}'"
echo "SVAR='${SVAR}'"
echo "ARG1='${ARG1}'"
echo "ARG2='${ARG2}'"
