A functions shell library. User assumes all risk.
#!/usr/bin/env bash
# --- here begins the evolution
#	of this cross-platform shell library
#		--> functions (lite) [ functions.sh ]
#
# created 2024095(+/-3)
# author: @jakimfett
# inspiration: functions.sh
# @todo pull in nano settings from functions.sh

# Set DEBUG equal to 0 for least info, \
#	9 for most info, \
#		default is 1.
[ -z ${DEBUG} ] && DEBUG=1

# Inform the user of their choice.
if [[ "${DEBUG}" -gt 0 ]]; then
	echo 'Shim loaded (DEBUG='${DEBUG}'), script continues.'
	echo
fi

# Make it easier to inform the user.
function logThis {
	if [[ "${DEBUG}" -gt 3 ]]; then
		echo "function logThis got value '${@}'"
	fi
	if [[ ! -z "${@}" ]]; then
		echo -e "${@}"
	else
		echo
	fi
}
function lt {
	logThis "${@}"
}
[ "${DEBUG}" -ne 0 ] && lt "@function:logThis (lt) loaded"
[ "${DEBUG}" -gt 1 ] && lt "@function:logThis (& 'lt' alias) testing..." &&\
	lt "@function:logThis success" && lt "---"
[ "${DEBUG}" -gt 0 ] && lt
# @todo exit (w/ trapping?) for if `lt` fails


# Get realpath (and inform user)
pathCmd='srcRealpath="$(realpath "${BASH_SOURCE}")"'
if [[ "${DEBUG}" -gt 2 ]]; then 
	time eval "${pathCmd}"
else 
	eval "${pathCmd}"
fi

if [[ "${DEBUG}" -gt 1 ]]; then
	lt "BASH_SOURCE realpath: '${srcRealpath}'"
fi

# ---

# Getting the source directory path
dirCmd='srcDirName="$(dirname "${srcRealpath}")"'
if [[ "${DEBUG}" -gt 2 ]]; then 
	time eval "${dirCmd}"
else 
	eval "${dirCmd}"
fi

if [[ "${DEBUG}" -gt 1 ]]; then
	lt "realpath dirname: '${srcDirName}'"
fi


if [[ "${DEBUG}" -gt 1 ]]; then
	lt -n 'Sourcing mixins...'
fi

#mixSrc=". \"${srcDirName}/mixins.src\""

function loadMixins {

	for srcFile in $(find "${srcDirName}/util" -name '*.src' ); do
		if [[ "${DEBUG}" -gt 2 ]]; then 
			time . "${srcFile}"
		else
			. "${srcFile}"
		fi
		if [[ $(type -t lt) == function ]]; then
			if [[ "${DEBUG}" -gt 1 ]]; then 
				lt "loaded from '${srcFile}'"
			fi
		else
			echo "Could not load logThis function!"
		fi
	done
	return 0
}

if [[ "${DEBUG}" -gt 2 ]]; then 
	time loadMixins
else
	loadMixins
fi

#if [[ "${DEBUG}" -gt 2 ]]; then 
#	time eval "${mixSrc}"
#else 
#	eval "${mixSrc}"
#fi

if [[ "${DEBUG}" -ne 0 ]]; then
	lt "mixins sourced from '${srcDirName}/util'" && lt
fi

function selfTest {
	# todo shellcheck
	lt "\tdiscreet testing function not yet implemented,"
	lt "\tedit DEBUG variable in '${BASH_SOURCE}'"
	lt "\t\t then source again to see inline tests"
}
[ "${DEBUG}" -gt 0 ] && lt "@function:selfTest loaded"
lt
lt "running self-test function 'selfTest':"
selfTest

#@todo port over function registration
lt "\n> shim loading complete, see above for available functions"