on 39feb14 help sign in
searx/utils/lib.sh
354 lines 8.8 kB history raw
1#!/usr/bin/env bash
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}")
9 done
10 REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
11fi
13if [[ -z ${TEMPLATES} ]]; then
14 TEMPLATES="${REPO_ROOT}/utils/templates"
15fi
17if [[ -z "$CACHE" ]]; then
18 CACHE="${REPO_ROOT}/cache"
19fi
21if [[ -z "$SYSTEMD_UNITS" ]]; then
22 SYSTEMD_UNITS="/lib/systemd/system"
23fi
25if [[ -z ${DIFF_CMD} ]]; then
26 DIFF_CMD="diff -u"
27 if command -v colordiff >/dev/null; then
28 DIFF_CMD="colordiff -u"
29 fi
30fi
32sudo_or_exit() {
33 # usage: sudo_or_exit
35 if [ ! "$(id -u)" -eq 0 ]; then
36 err_msg "this command requires root (sudo) privilege!" >&2
37 exit 42
38 fi
41rst_title() {
42 # usage: rst_title <header-text> [part|chapter|section]
44 case ${2-chapter} in
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";;
48 *)
49 err_msg "invalid argument '${2}' in line $(caller)"
50 return 42
51 ;;
52 esac
55if command -v fmt >/dev/null; then
56 export FMT="fmt -u"
57else
58 export FMT="cat"
59fi
61rst_para() {
62 # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
63 local prefix=''
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"
67 else
68 echo -en "\n$*\n" | $FMT
69 fi
72err_msg() { echo -e "ERROR: $*" >&2; }
73warn_msg() { echo -e "WARN: $*" >&2; }
74info_msg() { echo -e "INFO: $*"; }
76clean_stdin() {
77 if [[ $(uname -s) != 'Darwin' ]]; then
78 while $(read -n1 -t 0.1); do : ; done
79 fi
82wait_key(){
83 # usage: waitKEY [<timeout in sec>]
85 clean_stdin
86 local _t=$1
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 **"
91 echo
92 clean_stdin
95ask_yn() {
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
101 local _t=$3
102 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
103 [[ ! -z $_t ]] && _t="-t $_t"
104 case "${2}" in
105 Yn)
106 local exit_val=${EXIT_YES}
107 local choice="[YES/no]"
108 local default="Yes"
109 ;;
110 *)
111 local exit_val=${EXIT_NO}
112 local choice="[NO/yes]"
113 local default="No"
114 ;;
115 esac
116 echo
117 while true; do
118 clean_stdin
119 printf "$1 ${choice} "
120 # shellcheck disable=SC2086
121 read -n1 $_t
122 if [[ -z $REPLY ]]; then
123 printf "$default\n"; break
124 elif [[ $REPLY =~ ^[Yy]$ ]]; then
125 exit_val=${EXIT_YES}
126 printf "\n"
127 break
128 elif [[ $REPLY =~ ^[Nn]$ ]]; then
129 exit_val=${EXIT_NO}
130 printf "\n"
131 break
132 fi
133 _t=""
134 err_msg "invalid choice"
135 done
136 clean_stdin
137 return $exit_val
140tee_stderr () {
142 # usage::
143 # tee_stderr 1 <<EOF | python -i
144 # print("hello")
145 # EOF
146 # ...
147 # >>> print("hello")
148 # hello
150 local _t="0";
151 if [[ ! -z $1 ]] ; then _t="$1"; fi
153 (while read line; do
154 # shellcheck disable=SC2086
155 sleep $_t
156 echo -e "$line" >&2
157 echo "$line"
158 done)
161prefix_stdout () {
162 # usage: <cmd> | prefix_stdout [prefix]
164 local prefix=" | "
166 if [[ ! -z $1 ]] ; then prefix="$1"; fi
168 (while IFS= read line; do
169 echo -e "${prefix}$line"
170 done)
173append_line() {
175 # usage: append_line <line> <file>
176 #
177 # Append line if not exists, create file if not exists. E.g::
178 #
179 # append_line 'source ~/.foo' ~/bashrc
181 local LINE=$1
182 local FILE=$2
183 grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
186cache_download() {
188 # usage: cache_download <url> <local-filename>
190 local exit_value=0
192 if [[ ! -z ${SUDO_USER} ]]; then
193 sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
194 else
195 mkdir -p "${CACHE}"
196 fi
198 if [[ -f "${CACHE}/$2" ]] ; then
199 info_msg "already cached: $1"
200 info_msg " --> ${CACHE}/$2"
201 fi
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=$?
208 else
209 wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
210 fi
211 if $exit_value; then
212 err_msg "failed to download: $1"
213 fi
214 fi
217choose_one() {
219 # usage:
220 #
221 # DEFAULT_SELECT= 2 \
222 # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
224 local default=${DEFAULT_SELECT-1}
225 local REPLY
226 local env_name=$1 && shift
227 local choice=$1;
228 local max="${#@}"
229 local _t
230 [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
231 [[ ! -z $_t ]] && _t="-t $_t"
233 list=("$@")
234 echo -e "Menu::"
235 for ((i=1; i<= $(($max -1)); i++)); do
236 if [[ "$i" == "$default" ]]; then
237 echo -e " $i.) ${list[$i]} [default]"
238 else
239 echo -e " $i.) ${list[$i]}"
240 fi
241 done
242 while true; do
243 clean_stdin
244 printf "$1 [$default] "
246 if (( 10 > $max )); then
247 # shellcheck disable=SC2086
248 read -n1 $_t
249 else
250 # shellcheck disable=SC2086,SC2229
251 read $_t
252 fi
253 # selection fits
254 [[ $REPLY =~ ^-?[0-9]+$ ]] && (( $REPLY > 0 )) && (( $REPLY < $max )) && break
256 # take default
257 [[ -z $REPLY ]] && REPLY=$default && break
259 _t=""
260 err_msg "invalid choice"
261 done
262 echo
263 clean_stdin
264 eval "$env_name"='${list[${REPLY}]}'
267install_template() {
269 # usage:
270 #
271 # install_template [--no-eval] {file} [{owner} [{group} [{chmod}]]]
272 #
273 # install_template --no-eval /etc/updatedb.conf root root 644
275 local do_eval=1
276 if [[ "$1" == "--no-eval" ]]; then
277 do_eval=0; shift
278 fi
279 local dst="${1}"
280 local owner=${2-$(id -un)}
281 local group=${3-$(id -gn)}
282 local chmod=${4-644}
283 local _reply=""
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"
290 wait_key 30
291 return 42
292 fi
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}"
302 else
303 err_msg "failed ${template_file}"
304 return 42
305 fi
306 fi
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
314 return $?
315 fi
317 if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
318 info_msg "file ${dst} allready installed"
319 return 0
320 fi
322 info_msg "file ${dst} allready exists on this host"
324 while true; do
325 choose_one _reply "choose next step with file $dst" \
326 "replace file" \
327 "leave file unchanged" \
328 "interactiv shell" \
329 "diff files"
331 case $_reply in
332 "replace file")
333 info_msg "install: ${template_file}"
334 sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
335 "${template_file}" "${dst}" | prefix_stdout
336 break
337 ;;
338 "leave file unchanged")
339 break
340 ;;
341 "interactiv shell")
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
347 break
348 fi
349 ;;
350 "diff files")
351 $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
352 esac
353 done