@@ -0,0 +1,316 @@
2+# -*- coding: utf-8; mode: sh -*-
3+# shellcheck disable=SC2059,SC1117,SC2162,SC2004
5+if [[ -z "${REPO_ROOT}" ]]; then
6+ REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
7+ while [ -h "${REPO_ROOT}" ] ; do
8+ REPO_ROOT=$(readlink "${REPO_ROOT}")
10+ REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
13+if [[ -z ${TEMPLATES} ]]; then
14+ TEMPLATES="${REPO_ROOT}/utils/templates"
17+if [[ -z "$CACHE" ]]; then
18+ CACHE="${REPO_ROOT}/cache"
21+if [[ -z "$SYSTEMD_UNITS" ]]; then
22+ SYSTEMD_UNITS="/lib/systemd/system/"
26+ # usage: sudo_or_exit
28+ if [ ! "$(id -u)" -eq 0 ]; then
29+ err_msg "this command requires root (sudo) privilege!" >&2
35+ # usage: rst_title <header-text> [part|chapter|section]
38+ part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
39+ chapter) printf "\n${1}\n${1//?/=}\n";;
40+ section) printf "\n${1}\n${1//?/-}\n";;
42+ err_msg "invalid argument '${2}' in line $(caller)"
48+if command -v fmt >/dev/null; then
55+ # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
57+ if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
58+ prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
59+ echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
61+ echo -en "\n$*\n" | $FMT
65+err_msg() { echo -e "ERROR: $*" >&2; }
66+warn_msg() { echo -e "WARN: $*" >&2; }
67+info_msg() { echo -e "INFO: $*"; }
70+ # usage: waitKEY [<timeout in sec>]
73+ [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
74+ [[ ! -z $_t ]] && _t="-t $_t"
75+ # shellcheck disable=SC2086
76+ read -n1 $_t -p "** press any [KEY] to continue **"
81+ # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
83+ local EXIT_YES=0 # exit status 0 --> successful
84+ local EXIT_NO=1 # exit status 1 --> error code
87+ [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
88+ [[ ! -z $_t ]] && _t="-t $_t"
91+ local exit_val=${EXIT_YES}
92+ local choice="[YES/no]"
96+ local exit_val=${EXIT_NO}
97+ local choice="[NO/yes]"
103+ printf "$1 ${choice} "
104+ # shellcheck disable=SC2086
106+ if [[ -z $REPLY ]]; then
107+ printf "$default\n"; break
108+ elif [[ $REPLY =~ ^[Yy]$ ]]; then
109+ exit_val=${EXIT_YES}
112+ elif [[ $REPLY =~ ^[Nn]$ ]]; then
118+ err_msg "invalid choice"
126+ # tee_stderr 1 <<EOF | python -i
130+ # >>> print("hello")
134+ if [[ ! -z $1 ]] ; then _t="$1"; fi
136+ (while read line; do
137+ # shellcheck disable=SC2086
145+ # usage: <cmd> | prefix_stdout [prefix]
147+ local prefix="-->| "
149+ if [[ ! -z $1 ]] ; then prefix="$1"; fi
151+ (while IFS= read line; do
152+ echo -e "${prefix}$line"
158+ # usage: append_line <line> <file>
160+ # Append line if not exists, create file if not exists. E.g::
162+ # append_line 'source ~/.foo' ~/bashrc
166+ grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
171+ # usage: cache_download <url> <local-filename>
175+ if [[ ! -z ${SUDO_USER} ]]; then
176+ sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
181+ if [[ -f "${CACHE}/$2" ]] ; then
182+ info_msg "already cached: $1"
183+ info_msg " --> ${CACHE}/$2"
186+ if [[ ! -f "${CACHE}/$2" ]]; then
187+ info_msg "caching: $1"
188+ info_msg " --> ${CACHE}/$2"
189+ if [[ ! -z ${SUDO_USER} ]]; then
190+ sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
192+ wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
194+ if $exit_value; then
195+ err_msg "failed to download: $1"
204+ # DEFAULT_SELECT= 2 \
205+ # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
207+ local default=${DEFAULT_SELECT-1}
209+ local env_name=$1 && shift
213+ [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
214+ [[ ! -z $_t ]] && _t="-t $_t"
218+ for ((i=1; i<= $(($max -1)); i++)); do
219+ if [[ "$i" == "$default" ]]; then
220+ echo -e " $i.) ${list[$i]} [default]"
222+ echo -e " $i.) ${list[$i]}"
226+ printf "$1 [$default] "
228+ if (( 10 > $max )); then
229+ # shellcheck disable=SC2086
232+ # shellcheck disable=SC2086,SC2229
236+ [[ $REPLY =~ ^-?[0-9]+$ ]] && (( $REPLY > 0 )) && (( $REPLY < $max )) && break
239+ [[ -z $REPLY ]] && REPLY=$default && break
242+ err_msg "invalid choice"
245+ eval "$env_name"='${list[${REPLY}]}'
252+ # install_template [--no-eval] {file} [{owner} [{group} [{chmod}]]]
254+ # install_template --no-eval /etc/updatedb.conf root root 644
257+ if [[ "$1" == "--no-eval" ]]; then
261+ local owner=${2-$(id -un)}
262+ local group=${3-$(id -gn)}
263+ local chmod=${4-644}
266+ info_msg "install: ${dst}"
268+ if [[ ! -f "${TEMPLATES}${dst}" ]] ; then
269+ err_msg "${TEMPLATES}${dst} does not exists"
270+ err_msg "... can't install $dst / exit installation with error 42"
275+ local template_file="${TEMPLATES}${dst}"
276+ if [[ "$do_eval" == "1" ]]; then
277+ info_msg "BUILD template ${template_file}"
278+ if [[ -f "${TEMPLATES}${dst}" ]] ; then
279+ template_file="${CACHE}${dst}"
280+ mkdir -p "$(dirname "${template_file}")"
281+ # shellcheck disable=SC2086
282+ eval "echo \"$(cat ${TEMPLATES}${dst})\"" > "${template_file}"
284+ err_msg "failed ${template_file}"
289+ if [[ -f "${dst}" ]] ; then
290+ info_msg "file ${dst} allready exists on this host"
291+ choose_one _reply "choose next step with file $dst" \
293+ "leave file unchanged"
297+ info_msg "install: ${template_file}"
298+ sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
299+ "${template_file}" "${dst}"
301+ "leave file unchanged")
304+ echo "// exit with STRG-D"
305+ sudo -H -u "${owner}" -i
310+ info_msg "install: ${template_file}"
311+ sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
312+ "${template_file}" "${dst}"