# Shared functions of the Das OS helpers (dsv, duser, dfw, dsys). # Sourced, not run: each helper sets "set -euo pipefail" itself. # shellcheck shell=bash self=${0##*/} if [[ ${LC_ALL:-} == uk* || ${LC_MESSAGES:-} == uk* || ${LANG:-} == uk* ]]; then uk=1 else uk="" fi # say UKRAINIAN ENGLISH: prints the message in the user's language say() { if [[ -n $uk ]]; then echo "$1"; else echo "$2"; fi } warn() { say "$self: $1" "$self: $2" >&2 } die() { warn "$1" "$2" exit 1 } # need_root ARGS...: runs the helper again through sudo with the same arguments need_root() { if [[ $EUID -ne 0 ]]; then exec sudo "$0" "$@" fi } # Questions are only asked on a terminal; scripts must pass every argument interactive() { [[ -t 0 ]] } # ask VAR UKRAINIAN ENGLISH [DEFAULT]: reads a line into VAR; the prompt shows # the default, which an empty answer keeps ask() { local -n _answer=$1 local prompt prompt=$(say "$2" "$3") if [[ -n ${4:-} ]]; then prompt+=" [$4]" fi read -r -e -p "$prompt: " _answer || exit 1 [[ -n $_answer ]] || _answer=${4:-} } # confirm UKRAINIAN ENGLISH: a yes/no question, "no" by default confirm() { local reply read -r -p "$(say "$1 [т/Н]" "$2 [y/N]") " reply || return 1 [[ ${reply,,} == @(y|yes|т|так) ]] } # pad TEXT WIDTH: TEXT padded with spaces to WIDTH characters. printf pads by # bytes, ${#s} counts characters, so Cyrillic columns stay aligned. pad() { local width=$(($2 - ${#1})) ((width > 0)) || width=0 printf '%s%*s ' "$1" "$width" '' } # set_rc VAR VALUE: sets VAR in /etc/rc.conf, uncommenting Void's example line set_rc() { local rc=${RC_CONF:-/etc/rc.conf} if grep -q "^#\?$1=" "$rc"; then sed -i "s|^#\?$1=.*|$1=\"$2\"|" "$rc" else echo "$1=\"$2\"" >>"$rc" fi }