# no shebang necessary - this is a library to be sourced
# SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck disable=SC1091,SC1117,SC2002,SC2004,SC2034,SC2046,SC2059,SC2086,SC2129,SC2148,SC2154,SC2155,SC2162,SC2166,SC2181,SC2193
# make sure we have a UID
[ -z "${UID}" ] && UID="$(id -u)"
# -----------------------------------------------------------------------------
# checking the availability of commands
which_cmd() {
# shellcheck disable=SC2230
which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null
}
check_cmd() {
which_cmd "${1}" >/dev/null 2>&1 && return 0
return 1
}
# -----------------------------------------------------------------------------
setup_terminal() {
TPUT_RESET=""
TPUT_BLACK=""
TPUT_RED=""
TPUT_GREEN=""
TPUT_YELLOW=""
TPUT_BLUE=""
TPUT_PURPLE=""
TPUT_CYAN=""
TPUT_WHITE=""
TPUT_BGBLACK=""
TPUT_BGRED=""
TPUT_BGGREEN=""
TPUT_BGYELLOW=""
TPUT_BGBLUE=""
TPUT_BGPURPLE=""
TPUT_BGCYAN=""
TPUT_BGWHITE=""
TPUT_BOLD=""
TPUT_DIM=""
TPUT_UNDERLINED=""
TPUT_BLINK=""
TPUT_INVERTED=""
TPUT_STANDOUT=""
TPUT_BELL=""
TPUT_CLEAR=""
# Is stderr on the terminal? If not, then fail
test -t 2 || return 1
if check_cmd tput; then
if [ $(($(tput colors 2>/dev/null))) -ge 8 ]; then
# Enable colors
TPUT_RESET="$(tput sgr 0)"
TPUT_BLACK="$(tput setaf 0)"
TPUT_RED="$(tput setaf 1)"
TPUT_GREEN="$(tput setaf 2)"
TPUT_YELLOW="$(tput setaf 3)"
TPUT_BLUE="$(tput setaf 4)"
TPUT_PURPLE="$(tput setaf 5)"
TPUT_CYAN="$(tput setaf 6)"
TPUT_WHITE="$(tput setaf 7)"
TPUT_BGBLACK="$(tput setab 0)"
TPUT_BGRED="$(tput setab 1)"
TPUT_BGGREEN="$(tput setab 2)"
TPUT_BGYELLOW="$(tput setab 3)"
TPUT_BGBLUE="$(tput setab 4)"
TPUT_BGPURPLE="$(tput setab 5)"
TPUT_BGCYAN="$(tput setab 6)"
TPUT_BGWHITE="$(tput setab 7)"
TPUT_BOLD="$(tput bold)"
TPUT_DIM="$(tput dim)"
TPUT_UNDERLINED="$(tput smul)"
TPUT_BLINK="$(tput blink)"
TPUT_INVERTED="$(tput rev)"
TPUT_STANDOUT="$(tput smso)"
TPUT_BELL="$(tput bel)"
TPUT_CLEAR="$(tput clear)"
fi
fi
return 0
}
setup_terminal || echo >/dev/null
progress() {
echo >&2 " --- ${TPUT_DIM}${TPUT_BOLD}${*}${TPUT_RESET} --- "
}
# -----------------------------------------------------------------------------
netdata_banner() {
local l1=" ^" \
l2=" |.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-" \
l3=" | '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' " \
l4=" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
sp=" " \
netdata="netdata" start end msg="${*}" chartcolor="${TPUT_DIM}"
[ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$((${#netdata} - ${#msg}))}"
[ ${#msg} -gt $((${#l2} - 20)) ] && msg="${msg:0:$((${#l2} - 23))}..."
start="$((${#l2} / 2 - 4))"
[ $((start + ${#msg} + 4)) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
end=$((start + ${#msg} + 4))
echo >&2
echo >&2 "${chartcolor}${l1}${TPUT_RESET}"
echo >&2 "${chartcolor}${l2:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_GREEN}${netdata}${TPUT_RESET}${chartcolor}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}${TPUT_RESET}"
echo >&2 "${chartcolor}${l3:0:start}${sp:0:2}${TPUT_RESET}${TPUT_BOLD}${TPUT_CYAN}${msg}${TPUT_RESET}${chartcolor}${sp:0:2}${l3:end:$((${#l2} - end))}${TPUT_RESET}"
echo >&2 "${chartcolor}${l4}${TPUT_RESET}"
echo >&2
}
# -----------------------------------------------------------------------------
# portable service command
service_cmd="$(which_cmd service)"
rcservice_cmd="$(which_cmd rc-service)"
systemctl_cmd="$(which_cmd systemctl)"
service() {
local cmd="${1}" action="${2}"
if [ ! -z "${systemctl_cmd}" ]; then
run "${