#
# This file contains 'scripting' run-time support utility functions.
#
# NOTE: This file must be "sourced" (not executed).
#

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#
# Low level developer's support utility functions.
#
# WARNING: No NLS support here.
#

#
# Outputs arguments to "stderr".
#
_report() {
	>&2 echo "$@"
}

_report_error() {
	_report "INTERNAL ERROR:" "$@"
}

#
# Aborts execution printing diagnostic message.
#
_abort_execution() {
	if [ $# -ne 0 ] ; then
		_report_error "ABORT: '$@', execution aborted"
	else
		_report_error "ABORT: execution aborted"
	fi
	exit 1
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#
# Scripts debugging support.
#

#
# Outputs arguments to "stderr" if "DEBUG_STDERR" environment variable is not
# empty.
#
# Appends arguments to "${DEBUG_LOG_FILE}" file if "DEBUG_LOG_FILE" environment
# variable is not empty.
#
# Appends arguments to "${SCRIPT_LOG_FILE}" file if "SCRIPT_LOG_FILE"
# environment variable is not empty.
#
# Appends arguments to "${PACKAGE_LOG_FILE}" file if "PACKAGE_LOG_FILE"
# environment variable is not empty.
#
log_message() {
	local LOG_MSG
	local PID

	PID="$$"
	LOG_MSG="${PID} $@"

	if [ -n "${DEBUG_STDERR}" ] ; then
		>&2 echo "DEBUG: ${LOG_MSG}"
	fi

	if [ -n "${DEBUG_LOG_FILE}" ] ; then
		>>"${DEBUG_LOG_FILE}" echo "${LOG_MSG}"
	fi

	if [ -n "${SCRIPT_LOG_FILE}" ] ; then
		>>"${SCRIPT_LOG_FILE}" echo "${LOG_MSG}"
	fi

	if [ -n "${PACKAGE_LOG_FILE}" ] ; then
		>>"${PACKAGE_LOG_FILE}" echo "${LOG_MSG}"
	fi
}

#
# Depending on enabled debug outputs copies everything from "stdin" into one of
# "${PACKAGE_LOG_FILE}", "${SCRIPT_LOG_FILE}", "${DEBUG_LOG_FILE}", "stderr" or
# simply discards everything.
#
log_redirected_output() {
	#log_message "vvvvv redirected_output vvvvv"
	if [ -n "${PACKAGE_LOG_FILE}" ] ; then
		>>"${PACKAGE_LOG_FILE}" cat
	elif [ -n "${SCRIPT_LOG_FILE}" ] ; then
		>>"${SCRIPT_LOG_FILE}" cat
	elif [ -n "${DEBUG_LOG_FILE}" ] ; then
		>>"${DEBUG_LOG_FILE}" cat
	elif [ -n "${DEBUG_STDERR}" ] ; then
		>&2 cat
	else
		>/dev/null cat
	fi
	#log_message "^^^^^ redirected_output ^^^^^"
}

#
# Outputs variable name and value to debug log.
#
log_variable() {
	if [ -z "$1" ] ; then _abort_execution "variable name unspecified" ; fi
	#log_message $(eval echo "${1}=\${${1}}")
	log_message $(eval echo "\"${1}='\${${1}}'\"")
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#
# If "sh" is "bash" then debugging can be a bit more comfortable.
#
# "${SCRIPTS_DIR}/bash_debugging" contains "bash"-specific
# "_abort_execution()", "log_message()", "log_redirected_output()",
# "log_variable()" implementations.
#
if [ -n "${BASH_VERSION}" ] && [ -n "${SCRIPTS_DIR}" ] && [ -r "${SCRIPTS_DIR}/bash_debugging" ] ; then
	. "${SCRIPTS_DIR}/bash_debugging"
fi

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

script_log_init() {
	local SCRIPT
	local USER_NAME

	SCRIPT="$1" ; if [ -z "${SCRIPT}" ] ; then _abort_execution "'SCRIPT' is unspecified" ; fi
	log_variable SCRIPT
	USER_NAME="$(id -un)"

	SCRIPT_LOG_FILE="/tmp/${SCRIPT}_${USER_NAME}.log"
	log_variable SCRIPT_LOG_FILE
	export SCRIPT_LOG_FILE
	rm -f "${SCRIPT_LOG_FILE}"
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Converts possibly relative directory path into absolute form
absolute_dir_path() {
	(cd "$1" && pwd)
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

log_message "EOF"
