summary refs log tree commit diff
path: root/noarch/scripting_utils
blob: 92d394345e17d0a200a42dcd1d4f119a281e6822 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
# 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"