btrc & btrd
[m6w6/btr] / share / btr / common.sh
1 #!/bin/bash
2
3 export DATE=$(date +%Y%m%d%H%M%S)
4 export CPUS=${CPUS:-$(nproc)}
5
6 BTR_QUIET=false
7 BTR_VERBOSE=false
8 BTR_FORCEYES=false
9
10 function error {
11 echo "$@" >&2
12 exit 1
13 }
14 export -f error
15
16 function btr-banner {
17 echo "$(basename ${0:btr}) v0.4.0, (c) Michael Wallner <mike@php.net>"
18 if test "$BTR_BANNER"
19 then
20 echo "$BTR_BANNER"
21 fi
22 }
23 export -f btr-banner
24
25 function btr-confirm {
26 local CONTINUE
27 if ! $BTR_FORCEYES
28 then
29 echo -n "$1 (y/N) "
30 read -r CONTINUE
31 case $CONTINUE in
32 y*|Y*)
33 echo
34 ;;
35 *)
36 exit -1
37 ;;
38 esac
39 fi
40 }
41 export -f btr-confirm
42
43 function btr-setup-rundir {
44 local default_rundir="${1:-/tmp/btr}"
45
46 if test -z "$BTR_RUNDIR"
47 then
48 export BTR_RUNDIR="$default_rundir"
49 else
50 export BTR_RUNDIR=$(realpath "$BTR_RUNDIR")
51 fi
52
53 mkdir -p "$BTR_RUNDIR" || error "Could not create directory '$BTR_RUNDIR'"
54 }
55 export -f btr-setup-rundir
56
57 function btr-setup-verbosity {
58 local for_make=${1:-false}
59
60 if $BTR_VERBOSE
61 then
62 BTR_QUIET_FLAG=
63 BTR_SILENT_FLAG=
64 BTR_VERBOSE_FLAG="-v"
65 SAY="echo; echo"
66 elif $BTR_QUIET
67 then
68 BTR_QUIET_FLAG="-q"
69 BTR_SILENT_FLAG="-s"
70 BTR_VERBOSE_FLAG=
71 SAY="true"
72 else
73 BTR_QUIET_FLAG=
74 BTR_SILENT_FLAG="-s"
75 BTR_VERBOSE_FLAG=
76 SAY="echo"
77 fi
78
79 if $for_make
80 then
81 SAY="@$SAY"
82 fi
83
84 export BTR_QUIET BTR_VERBOSE BTR_FORCEYES BTR_QUIET_FLAG BTR_SILENT_FLAG BTR_VERBOSE_FLAG SAY
85 }
86 export -f btr-setup-verbosity
87
88 # vim: noet