2018-08-11 18:57:24 +00:00
|
|
|
#!/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
|
|
|
|
}
|
|
|
|
|
2018-08-11 21:15:13 +00:00
|
|
|
is_sudo() {
|
|
|
|
(( UID == 0 && ${SUDO_UID:-0} != 0 ))
|
|
|
|
}
|
|
|
|
|
|
|
|
is_root() {
|
|
|
|
(( UID = 0 ))
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:57:24 +00:00
|
|
|
needs_sudo() {
|
2018-08-11 21:15:13 +00:00
|
|
|
if ! is_sudo; then
|
2018-08-11 18:57:24 +00:00
|
|
|
error 4 "Must be invoked through sudo."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
needs_root() {
|
2018-08-11 21:15:13 +00:00
|
|
|
if ! is_root; then
|
2018-08-11 18:57:24 +00:00
|
|
|
error 4 "Must be invoked as root."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-08-11 21:15:13 +00:00
|
|
|
become_sudo() {
|
|
|
|
if is_sudo; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
if is_root; then
|
|
|
|
needs_sudo
|
|
|
|
fi
|
|
|
|
sudo "${BASH_SOURCE[0]}" "$@"
|
|
|
|
}
|