summaryrefslogtreecommitdiffstats
path: root/plugins/url_check/ptxlib.bash
blob: 50249916d2b765d19bb1b549612a12f7f643c874 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# -----------------------------------------------------
# Description:	Basic Script Functions
# Author: 	Bjørn Bürger <b.buerger@pengutronix.de> 
# Date:		Wed Mar 21 17:03:39 CET 2007
# -----------------------------------------------------

PTXLIB="true"
MY_VERSION="2"

if [ "$PTX_LIB_VERSION" -lt "$MY_VERSION" ] ; then
	echo "WARNING: ptxlib.bash not compatible"
	echo "WARNING: please audit your script"
	exit 1
fi

FULLARGS="$@"
DEBUG=${DEBUG:="false"}

#
# customized exit functions
#
# $1 --> Error Message
# $2 --> Exit Code
#
ptx_exit(){
	echo "$0: $1"
	exit $2
}
ptx_exit_silent(){
	ptx_debug "$0: $1"
	exit $2
}

#
# print out error message and exit with status 1
#
# $1: error message
# ${PREFIX}: to be printed before message
#
ptx_bailout () {
	echo "${PREFIX}error: $1" >&2
	exit 1
}


#
# print out warning message
#
# $1: warning message
# ${PREFIX}: to be printed before message
#
ptx_warning() {
	echo "${PREFIX}warning: $1" >&2
}

#
# print out classic message
#
# $1: warning message
# ${PREFIX}: to be printed before message
#
ptx_message() {
	echo "${PREFIX}: $1" >&1
}

ptx_message_n() {
	if [ -n "$PREFIX" ]; then
	echo -n "${PREFIX}: $1" >&1
	else
	echo -n "$1" >&1
	fi
}

#
# Debugging Output
#
ptx_debug(){
	[ "$DEBUG" = "true" ] && echo "$0: $1" >&2
}
ptx_debug "Debugging is enabled - Turn off with DEBUG=false"


#
# check if a previously executed pipe returned an error
#
ptx_check_pipe_status() {
	for i in  "${PIPESTATUS[@]}"; do [ $i -gt 0 ] && {
		echo
		echo "error: a command in the pipe returned $i, bailing out"
		echo
		exit $i
	}
	done
}

#
# present a choice [y/n] and execute command
#
# $1: question / message
# $2: command
# $3: yes message
# $4: no message
# ${PREFIX}: to be printed before message
#
#
ptx_yesno_choice(){
	ptx_debug "Message: $1"
	ptx_debug "Command: $2"
	dialog --yesno "$1" 0 0
	case $? in
		0)
		ptx_message "${PREFIX} [YES] - $3"
		$2 || ptx_error "command $2 failed"
		;;
		*)
		ptx_warning "${PREFIX} [NO]  - $4"
		;;
	esac
}

#
# create a directory 
#
ptx_check_create_dir(){
	[ -d "$1" ] || ptx_yesno_choice "$1 does not exist, create it?" "mkdir -pv $1" "[SUCCESS]" "[ABORTED]"
}

#
# dependency check for needed files
#
# if any one of these variables is
# set, a dependency check is performed:
#
# dependency_check_files_recommends
# dependency_check_files_depends
# dependency_check_files_conflicts
#
ptx_dependency_check_files(){
	[ -z "$dependency_check_files_recommends" ] && ptx_debug "[file check] no file recommendation defined"
	for file in $dependency_check_files_recommends; do
		test -e $file
		case $? in
			0)
			ptx_debug "[file check] This Script recommends $file: OK"
			;;
			*)
			ptx_warning "[file check] This Script recommends $file: FILE NOT FOUND"
			;;
		esac
	done
	[ -z "$dependency_check_files_depends" ] && ptx_debug "[file check] no file dependency defined"
	for file in $dependency_check_files_depends; do
		test -e $file
		case $? in
			0)
			ptx_debug "[file check] This Script depends on $file: OK"
			;;
			*)
			ptx_bailout "[file check] This Script depends on $file: FILE NOT FOUND"
			;;
		esac
	done
	[ -z "$dependency_check_files_conflicts" ] && ptx_debug "[file check] no file conflict defined"
	for file in $dependency_check_files_conflicts; do
		test -e $file
		case $? in
			0)
			ptx_bailout "[file check] This Script conflicts with $file: FILE EXISTS"
			;;
			*)
			ptx_debug "[file check] This Script conflicts with $file: OK (does not exist)"
			;;
		esac
	done
}

#
# dependency check for needed directories
#
# if any one of these variables is
# set, a dependency check is performed:
#
# dependency_check_dirs_recommends
# dependency_check_dirs_depends
# dependency_check_dirs_conflicts
#
ptx_dependency_check_dirs(){
	[ -z "$dependency_check_dirs_recommends" ] && ptx_debug "[dir check] no directory recommendation defined"
	for directory in $dependency_check_dirs_recommends; do
		test -d $directory
		case $? in
			0)
			ptx_debug "[dir check] This Script recommends $directory: OK"
			;;
			*)
			ptx_warning "[dir check] This Script recommends $directory: directory NOT FOUND"
			;;
		esac
	done
	[ -z "$dependency_check_dirs_depends" ] && ptx_debug "[dir check] no directory dependency defined"
	for directory in $dependency_check_dirs_depends; do
		test -d $directory
		case $? in
			0)
			ptx_debug "[dir check] This Script depends on $directory: OK"
			;;
			*)
			ptx_bailout "[dir check] This Script depends on $directory: directory NOT FOUND"
			;;
		esac
	done
	[ -z "$dependency_check_dirs_conflicts" ] && ptx_debug "[dir check] no directory conflict defined"
	for directory in $dependency_check_dirs_conflicts; do
		test -d $directory
		case $? in
			0)
			ptx_bailout "[dir check] This Script conflicts with $directory: directory EXISTS"
			;;
			*)
			ptx_debug "[dir check] This Script conflicts with $directory: OK (does not exist)"
			;;
		esac
	done
}

#
# dependency check for needed tools
#
# if any one of these variables is
# set, a dependency check is performed:
#
# dependency_check_tools_recommends
# dependency_check_tools_depends
# dependency_check_tools_conflicts
#
ptx_dependency_check_tools(){
	[ -z "$dependency_check_files_recommends" ] && ptx_debug "[file check] no tool recommendation defined"
	for tool in $dependency_check_tools_recommends; do
		which $tool >/dev/null 2>&1
		case $? in
			0)
			ptx_debug "[tool check] This Script recommends $tool: OK"
			;;
			*)
			ptx_warning "[tool check] This Script recommends $tool: MISSING"
			;;
		esac
	done
	[ -z "$dependency_check_tools_depends" ] && ptx_debug "[file check] no tool dependency defined"
	for tool in $dependency_check_tools_depends; do
		which $tool >/dev/null 2>&1
		case $? in
			0)
			ptx_debug "[tool check] This Script depends on $tool: OK"
			;;
			*)
			ptx_bailout "[tool check] This Script depends on $tool: MISSING"
			;;
		esac
	done
	[ -z "$dependency_check_tools_conflicts" ] && ptx_debug "[file check] no file conflict defined"
	for tool in $dependency_check_tools_conflicts; do
		which $tool >/dev/null 2>&1
		case $? in
			0)
			ptx_bailout "[tool check] This Script conflicts with on $tool: CONFLICT FOUND"
			;;
			*)
			ptx_debug "[tool check] This Script conflicts with $tool: OK"
			;;
		esac
	done
}

ptx_dependency_check_hostname(){
	[ -z "$dependency_check_hostname_recommends" ] && ptx_debug "[hostname check] no hostname recommendation defined"
	for hostname in $dependency_check_hostname_recommends; do
		[ "`hostname`" = "$hostname" ] >/dev/null 2>&1
		case $? in
			0)
			ptx_debug "[hostname check] This Script recommends host $hostname: OK"
			;;
			*)
			ptx_warning "[hostname check] This Script recommends host $hostname: THIS IS NOT ME"
			;;
		esac
	done
	[ -z "$dependency_check_hostname_depends" ] && ptx_debug "[hostname check] no hostname dependency defined"
	for hostname in $dependency_check_hostname_depends; do
		[ "`hostname`" = "$hostname" ] >/dev/null 2>&1
		case $? in
			0)
			ptx_debug "[hostname check] This Script depends on host $hostname: OK"
			;;
			*)
			ptx_bailout "[hostname check] This Script depends on host $hostname: THIS IS NOT ME"
			;;
		esac
	done
	[ -z "$dependency_check_hostname_conflicts" ] && ptx_debug "[hostname check] no hostname conflict defined"
	for hostname in $dependency_check_hostname_conflicts; do
		[ "`hostname`" = "$hostname" ] >/dev/null 2>&1
		case $? in
			0)
			ptx_bailout "[hostname check] This Script conflicts with host $hostname: CONFLICT FOUND"
			;;
			*)
			ptx_debug "[hostname check] This Script conflicts with host $hostname: OK"
			;;
		esac
	done
}

#
# convenience wrapper for the functions above
#
# performs all available checks
#
ptx_dependency_check(){
	ptx_dependency_check_hostname
	ptx_dependency_check_dirs
	ptx_dependency_check_files
	ptx_dependency_check_tools
}

# service specific lib functions, based on script domains:
#
# - dumb_tool
# - tool
# - development
# - system_management
#

if [ -n "$PTX_SCRIPT_DOMAINS" ]; then
for ptx_script_domain in $PTX_SCRIPT_DOMAINS; do
	ptx_debug "loading config for domain: ${ptx_script_domain}"
	SCONFS="$HOME/.ptxsd-${ptx_script_domain}.conf \
	/usr/local/etc/ptxsd-${ptx_script_domain}.conf \
	/etc/ptxsd-${ptx_script_domain}.conf \
	`dirname $0`/ptxsd-${ptx_script_domain}.conf \
	`dirname $0`/../lib/ptxsd-${ptx_script_domain}.conf"
	for scriptconf in $SCONFS; do
		if [ -e "$scriptconf" ] ; then 
			. $scriptconf && { ptx_debug "$scriptconf loaded"; break; }
		fi
	done
	[ "$PTXSD" = "true" ] || ptx_warning " No valid Configuration for Domain: ${ptx_script_domain} found \
	[ $conf (version $MY_VERSION) ] [ caller: $0 ]"
done
fi