summaryrefslogtreecommitdiff
path: root/lib/osi.sh
blob: bff13f8a4dd8c2e0b5d4cc89d231075426851d54 (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
#!/hint/bash
# 2018 Luke Shumaker

print() {
	# shellcheck disable=SC2059
	printf "$(gettext -- "$1")\\n" "${@:2}"
}

error() {
	local msg
	msg="$(print "${@:2}")"
	printf '%s: %s\n' "${0##*/}" "$msg" >&2
	if (( $1 != 0 )); then
		exit "$1"
	fi
}

in_array() {
	local needle=$1
	local straw
	for straw in "${@:2}"; do
		if [[ $needle = "$straw" ]]; then
			return 0
		fi
	done
	return 1
}

is_sudo() {
	(( UID == 0 && ${SUDO_UID:-0} != 0 ))
}

is_root() {
	(( UID == 0 ))
}

needs_sudo() {
	if ! is_sudo; then
		error 4 "Must be invoked through sudo."
	fi
}

needs_root() {
	if ! is_root; then
		error 4 "Must be invoked as root."
	fi
}