2018-08-11 18:57:24 +00:00
|
|
|
#!/hint/bash
|
2024-08-12 07:05:21 +00:00
|
|
|
# Copyright (C) 2018, 2024 Luke Shumaker
|
2024-01-26 01:43:54 +00:00
|
|
|
# Copyright (C) 2023-2024 Umorpha Systems
|
2018-08-18 18:42:42 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-08-11 18:57:24 +00:00
|
|
|
|
2024-08-12 07:05:21 +00:00
|
|
|
# These are the well-known exit codes defined by LSB
|
|
|
|
# https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
|
|
|
|
#
|
2023-10-25 10:59:35 +00:00
|
|
|
# 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
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
# `osi.sh:printf` is the same as `printf`, but feeds the format string
|
2023-10-24 21:42:25 +00:00
|
|
|
# through `gettext`. This is pretty much just a kludge to silence
|
|
|
|
# spurious SC2059 complaints without disabling it entirely or leaving
|
|
|
|
# "shellcheck disable=" comments everywhere.
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:printf() {
|
2024-01-26 06:27:32 +00:00
|
|
|
local args=()
|
2023-10-24 21:42:25 +00:00
|
|
|
if [[ "$1" == '-v' ]]; then
|
2024-01-26 06:27:32 +00:00
|
|
|
args+=("$1" "$2")
|
|
|
|
shift 2
|
2023-10-24 21:42:25 +00:00
|
|
|
fi
|
2024-01-26 06:27:32 +00:00
|
|
|
args+=('--')
|
|
|
|
if [[ "$1" == '--' ]]; then
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
args+=("$(gettext -- "$1")" "${@:2}")
|
2023-10-24 21:42:25 +00:00
|
|
|
# shellcheck disable=SC2059
|
2024-01-26 06:27:32 +00:00
|
|
|
printf "${args[@]}"
|
2023-10-24 21:42:25 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:print() {
|
2018-08-11 18:57:24 +00:00
|
|
|
# shellcheck disable=SC2059
|
2024-01-26 06:27:32 +00:00
|
|
|
printf -- "$(gettext -- "$1")\\n" "${@:2}"
|
2018-08-11 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:error() {
|
2018-08-11 18:57:24 +00:00
|
|
|
local msg
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:printf -v msg -- "${@:2}"
|
2018-08-11 18:57:24 +00:00
|
|
|
printf '%s: %s\n' "${0##*/}" "$msg" >&2
|
|
|
|
if (( $1 != 0 )); then
|
|
|
|
exit "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:in_array() {
|
2018-08-11 18:57:24 +00:00
|
|
|
local needle=$1
|
|
|
|
local straw
|
|
|
|
for straw in "${@:2}"; do
|
|
|
|
if [[ $needle = "$straw" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:is_sudo() {
|
2018-08-11 21:15:13 +00:00
|
|
|
(( UID == 0 && ${SUDO_UID:-0} != 0 ))
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:is_root() {
|
2018-08-11 21:38:08 +00:00
|
|
|
(( UID == 0 ))
|
2018-08-11 21:15:13 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:needs_sudo() {
|
|
|
|
if ! osi.sh:is_sudo; then
|
|
|
|
osi.sh:error $EXIT_NOPERMISSION "Must be invoked through sudo."
|
2018-08-11 18:57:24 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:needs_root() {
|
|
|
|
if ! osi.sh:is_root; then
|
|
|
|
osi.sh:error $EXIT_NOPERMISSION "Must be invoked as root."
|
2018-08-11 18:57:24 +00:00
|
|
|
fi
|
|
|
|
}
|
2024-01-26 01:43:54 +00:00
|
|
|
|
2024-02-14 02:07:36 +00:00
|
|
|
osi.sh:sudo() {
|
2024-01-26 01:43:54 +00:00
|
|
|
sudo \
|
|
|
|
${SOURCE_DATE_EPOCH:+"SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH"} \
|
|
|
|
${TMPDIR:+"TMPDIR=$TMPDIR"} \
|
|
|
|
"$@"
|
|
|
|
}
|
2024-02-13 06:51:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|