2# -*- coding: utf-8; mode: sh -*-
3# shellcheck disable=SC2059,SC1117,SC2162,SC2004
5if [[ -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 )
13if [[ -z ${TEMPLATES} ]]; then
14 TEMPLATES="${REPO_ROOT}/utils/templates"
17if [[ -z "$CACHE" ]]; then
18 CACHE="${REPO_ROOT}/cache"
21if [[ -z "$SYSTEMD_UNITS" ]]; then
22 SYSTEMD_UNITS="/lib/systemd/system"
25if [[ -z ${DIFF_CMD} ]]; then
27 if command -v colordiff >/dev/null; then
28 DIFF_CMD="colordiff -u"
35 if [ ! "$(id -u)" -eq 0 ]; then
36 err_msg "this command requires root (sudo) privilege!" >&2
42 # usage: rst_title <header-text> [part|chapter|section]
45 part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
46 chapter) printf "\n${1}\n${1//?/=}\n";;
47 section) printf "\n${1}\n${1//?/-}\n";;
49 err_msg "invalid argument '${2}' in line $(caller)"
55if command -v fmt >/dev/null; then
62 # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
64 if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
65 prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
66 echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
68 echo -en "\n$*\n" | $FMT
72err_msg() { echo -e "ERROR: $*" >&2; }
73warn_msg() { echo -e "WARN: $*" >&2; }
74info_msg() { echo -e "INFO: $*"; }
77 if [[ $(uname -s) != 'Darwin' ]]; then
78 while read -n1 -t 0.1; do : ; done
83 # usage: waitKEY [<timeout in sec>]
87 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
88 [[ ! -z $_t ]] && _t="-t $_t"
89 # shellcheck disable=SC2086
90 read -s -n1 $_t -p "** press any [KEY] to continue **"
96 # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
98 local EXIT_YES=0 # exit status 0 --> successful
99 local EXIT_NO=1 # exit status 1 --> error code
102 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
103 [[ ! -z $_t ]] && _t="-t $_t"
106 local exit_val=${EXIT_YES}
107 local choice="[YES/no]"
111 local exit_val=${EXIT_NO}
112 local choice="[NO/yes]"
119 printf "$1 ${choice} "
120 # shellcheck disable=SC2086
122 if [[ -z $REPLY ]]; then
123 printf "$default\n"; break
124 elif [[ $REPLY =~ ^[Yy]$ ]]; then
128 elif [[ $REPLY =~ ^[Nn]$ ]]; then
134 err_msg "invalid choice"
143 # tee_stderr 1 <<EOF | python -i
151 if [[ ! -z $1 ]] ; then _t="$1"; fi
154 # shellcheck disable=SC2086
162 # usage: <cmd> | prefix_stdout [prefix]
166 if [[ ! -z $1 ]] ; then prefix="$1"; fi
168 (while IFS= read line; do
169 echo -e "${prefix}$line"
175 # usage: append_line <line> <file>
177 # Append line if not exists, create file if not exists. E.g::
179 # append_line 'source ~/.foo' ~/bashrc
183 grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
188 # usage: cache_download <url> <local-filename>
192 if [[ ! -z ${SUDO_USER} ]]; then
193 sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
198 if [[ -f "${CACHE}/$2" ]] ; then
199 info_msg "already cached: $1"
200 info_msg " --> ${CACHE}/$2"
203 if [[ ! -f "${CACHE}/$2" ]]; then
204 info_msg "caching: $1"
205 info_msg " --> ${CACHE}/$2"
206 if [[ ! -z ${SUDO_USER} ]]; then
207 sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
209 wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
212 err_msg "failed to download: $1"
221 # DEFAULT_SELECT= 2 \
222 # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
224 local default=${DEFAULT_SELECT-1}
226 local env_name=$1 && shift
230 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
231 [[ ! -z $_t ]] && _t="-t $_t"
235 for ((i=1; i<= $(($max -1)); i++)); do
236 if [[ "$i" == "$default" ]]; then
237 echo -e " $i.) ${list[$i]} [default]"
239 echo -e " $i.) ${list[$i]}"
244 printf "$1 [$default] "
246 if (( 10 > $max )); then
247 # shellcheck disable=SC2086
250 # shellcheck disable=SC2086,SC2229
254 [[ $REPLY =~ ^-?[0-9]+$ ]] && (( $REPLY > 0 )) && (( $REPLY < $max )) && break
257 [[ -z $REPLY ]] && REPLY=$default && break
260 err_msg "invalid choice"
264 eval "$env_name"='${list[${REPLY}]}'
271 # install_template [--no-eval] {file} [{owner} [{group} [{chmod}]]]
273 # install_template --no-eval /etc/updatedb.conf root root 644
276 if [[ "$1" == "--no-eval" ]]; then
280 local owner=${2-$(id -un)}
281 local group=${3-$(id -gn)}
285 info_msg "install: ${dst}"
287 if [[ ! -f "${TEMPLATES}${dst}" ]] ; then
288 err_msg "${TEMPLATES}${dst} does not exists"
289 err_msg "... can't install $dst / exit installation with error 42"
294 local template_file="${TEMPLATES}${dst}"
295 if [[ "$do_eval" == "1" ]]; then
296 info_msg "BUILD template ${template_file}"
297 if [[ -f "${TEMPLATES}${dst}" ]] ; then
298 template_file="${CACHE}${dst}"
299 mkdir -p "$(dirname "${template_file}")"
300 # shellcheck disable=SC2086
301 eval "echo \"$(cat ${TEMPLATES}${dst})\"" > "${template_file}"
303 err_msg "failed ${template_file}"
308 mkdir -p "$(dirname "${dst}")"
310 if [[ ! -f "${dst}" ]]; then
311 info_msg "install: ${template_file}"
312 sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
313 "${template_file}" "${dst}" | prefix_stdout
317 if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
318 info_msg "file ${dst} allready installed"
322 info_msg "file ${dst} allready exists on this host"
325 choose_one _reply "choose next step with file $dst" \
327 "leave file unchanged" \
333 info_msg "install: ${template_file}"
334 sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
335 "${template_file}" "${dst}" | prefix_stdout
338 "leave file unchanged")
342 echo "// edit ${dst} to your needs"
343 echo "// exit with CTRL-D"
344 sudo -H -u "${owner}" -i
345 $DIFF_CMD "${dst}" "${template_file}"
346 if ask_yn "did you edit ${template_file} to your needs?"; then
351 $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list