101 lines
2.1 KiB
Bash
101 lines
2.1 KiB
Bash
#!/hint/bash
|
|
# Copyright (C) 2018 Luke Shumaker
|
|
# Copyright (C) 2023-2024 Umorpha Systems
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# shellcheck disable=SC2034
|
|
readonly EXIT_SUCCESS=0
|
|
readonly EXIT_FAILURE=1
|
|
readonly EXIT_INVALIDARGUMENT=2
|
|
readonly EXIT_NOTIMPLEMENTED=3
|
|
readonly EXIT_NOPERMISSION=4
|
|
readonly EXIT_NOTINSTALLED=5
|
|
readonly EXIT_NOTCONFIGURED=6
|
|
readonly EXIT_NOTRUNNING=7
|
|
|
|
# `osi.sh:printf` is the same as `printf`, but feeds the format string
|
|
# through `gettext`. This is pretty much just a kludge to silence
|
|
# spurious SC2059 complaints without disabling it entirely or leaving
|
|
# "shellcheck disable=" comments everywhere.
|
|
osi.sh:printf() {
|
|
local args=()
|
|
if [[ "$1" == '-v' ]]; then
|
|
args+=("$1" "$2")
|
|
shift 2
|
|
fi
|
|
args+=('--')
|
|
if [[ "$1" == '--' ]]; then
|
|
shift
|
|
fi
|
|
args+=("$(gettext -- "$1")" "${@:2}")
|
|
# shellcheck disable=SC2059
|
|
printf "${args[@]}"
|
|
}
|
|
|
|
osi.sh:print() {
|
|
# shellcheck disable=SC2059
|
|
printf -- "$(gettext -- "$1")\\n" "${@:2}"
|
|
}
|
|
|
|
osi.sh:error() {
|
|
local msg
|
|
osi.sh:printf -v msg -- "${@:2}"
|
|
printf '%s: %s\n' "${0##*/}" "$msg" >&2
|
|
if (( $1 != 0 )); then
|
|
exit "$1"
|
|
fi
|
|
}
|
|
|
|
osi.sh:in_array() {
|
|
local needle=$1
|
|
local straw
|
|
for straw in "${@:2}"; do
|
|
if [[ $needle = "$straw" ]]; then
|
|
return 0
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
osi.sh:is_sudo() {
|
|
(( UID == 0 && ${SUDO_UID:-0} != 0 ))
|
|
}
|
|
|
|
osi.sh:is_root() {
|
|
(( UID == 0 ))
|
|
}
|
|
|
|
osi.sh:needs_sudo() {
|
|
if ! osi.sh:is_sudo; then
|
|
osi.sh:error $EXIT_NOPERMISSION "Must be invoked through sudo."
|
|
fi
|
|
}
|
|
|
|
osi.sh:needs_root() {
|
|
if ! osi.sh:is_root; then
|
|
osi.sh:error $EXIT_NOPERMISSION "Must be invoked as root."
|
|
fi
|
|
}
|
|
|
|
osi.sh:sudo() {
|
|
sudo \
|
|
${SOURCE_DATE_EPOCH:+"SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH"} \
|
|
${TMPDIR:+"TMPDIR=$TMPDIR"} \
|
|
"$@"
|
|
}
|
|
|
|
osi.sh:print_stacktrace() {
|
|
local i
|
|
for (( i=0; i < ${#FUNCNAME[@]}; i++ )); do
|
|
osi.sh:print '\t%s()\tcalled at %s:%s' "${FUNCNAME[$i]}" "${BASH_SOURCE[$((i+1))]:-$0}" "${BASH_LINENO[$i]}"
|
|
done
|
|
}
|
|
|
|
osi.sh:bug() {
|
|
local msg
|
|
osi.sh:printf -v msg -- "$@"
|
|
osi.sh:printf '%s: BUG: %s\n' "${0##*/}" "$msg" >&2
|
|
osi.sh:print_stacktrace >&2
|
|
exit $EXIT_FAILURE
|
|
}
|