88f522ee3859c40bc4b73080d4e46fc8a13e1ced
[m6w6/btr] / share / btr / common.sh
1 #!/bin/bash
2
3 export BTR_DATE=$(date +%Y%m%d%H%M%S)
4 export BTR_CPUS=${CPUS:-$(nproc)}
5 export BTR_PROG=$(basename "$0")
6 export BTR_QUIET=false
7 export BTR_VERBOSE=false
8 export 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 "$BTR_PROG 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
30 echo -n "$1 (y/N) "
31 read -r CONTINUE
32 case $CONTINUE in
33 y*|Y*)
34 echo
35 ;;
36 *)
37 exit -1
38 ;;
39 esac
40 fi
41 }
42 export -f btr-confirm
43
44 function btr-setup-rundir {
45 local default_rundir="${1:-/tmp/btr}"
46
47 if test -z "$BTR_RUNDIR"
48 then
49 export BTR_RUNDIR="$default_rundir"
50 else
51 export BTR_RUNDIR=$(realpath "$BTR_RUNDIR")
52 fi
53
54 mkdir -p "$BTR_RUNDIR" || error "Could not create directory '$BTR_RUNDIR'"
55 }
56 export -f btr-setup-rundir
57
58 function btr-setup-verbosity {
59 local for_make=${1:-false}
60
61 if ${BTR_VERBOSE:-false}
62 then
63 BTR_QUIET_FLAG=
64 BTR_SILENT_FLAG=
65 BTR_VERBOSE_FLAG="-v"
66 SAY="echo; echo"
67 elif $BTR_QUIET
68 then
69 BTR_QUIET_FLAG="-q"
70 BTR_SILENT_FLAG="-s"
71 BTR_VERBOSE_FLAG=
72 SAY="true"
73 else
74 BTR_QUIET_FLAG=
75 BTR_SILENT_FLAG="-s"
76 BTR_VERBOSE_FLAG=
77 SAY="echo"
78 fi
79
80 if $for_make
81 then
82 SAY="@$SAY"
83 fi
84
85 export BTR_QUIET BTR_VERBOSE BTR_FORCEYES BTR_QUIET_FLAG BTR_SILENT_FLAG BTR_VERBOSE_FLAG SAY
86 }
87 export -f btr-setup-verbosity
88
89 function btr-shortoptions {
90 (
91 local f
92 local e
93
94 for f in common $BTR_PROG
95 do
96 for e in flags opts
97 do
98 test -e "$BTR_LIBDIR/$f.$e" && cut -sf1 <"$BTR_LIBDIR/$f.$e"
99 done
100 done
101 ) | xargs | tr -d " "
102 }
103 export -f btr-shortoptions
104
105 function btr-longoptions {
106 (
107 local f
108 local e
109
110 for f in common $BTR_PROG
111 do
112 for e in flags opts
113 do
114 test -e "$BTR_LIBDIR/$f.$e" && cut -sf2 <"$BTR_LIBDIR/$f.$e"
115 done
116 done
117 ) | sed -r -e 's/(:+).*/\1/' | xargs | tr " " ","
118 }
119 export -f btr-longoptions
120
121 function btr-flags {
122 (
123 local f
124 local e
125
126 for f in common $BTR_PROG
127 do
128 test -e "$BTR_LIBDIR/$f.flags" && cut -sf1 <"$BTR_LIBDIR/$f.flags"
129 done
130 ) | xargs | tr -d " "
131 }
132 export -f btr-flags
133
134 function btr-help-options {
135 local f o=$(
136 for f in common $BTR_PROG
137 do
138 test -e "$BTR_LIBDIR/$f.$1" && "$BTR_LIBDIR/opt.awk" <"$BTR_LIBDIR/$f.$1"
139 done
140 )
141 if test "$o"
142 then
143 echo
144 case "$1" in
145 flags)
146 echo " Flags:"
147 ;;
148 opts)
149 echo " Options:"
150 ;;
151 esac
152 echo "$o"
153 fi
154 }
155 export -f btr-help-options
156
157 function btr-help-args {
158 local a d l
159
160 if test -e "$BTR_LIBDIR/$BTR_PROG.args"
161 then
162 echo
163 echo " Arguments:"
164 while read a d
165 do
166 printf "%b\n" "$d" | fold -sw46 | while read l
167 do
168 printf " %-16s %s\n" "$a" "$l"
169 a=
170 done
171 echo
172 done <"$BTR_LIBDIR/$BTR_PROG.args"
173 fi
174 }
175 export -f btr-help-args
176
177 function btr-args {
178 if test -e "$BTR_LIBDIR/$BTR_PROG.args"
179 then
180 cut -sf1 <"$BTR_LIBDIR/$BTR_PROG.args" | xargs
181 fi
182 }
183 export -f btr-args
184
185 function btr-help {
186 echo
187 echo "Usage: $BTR_PROG [-$(btr-flags)] [<options>]" $(btr-args)
188 btr-help-options flags
189 btr-help-options opts
190 btr-help-args
191 if test $BTR_PROG != "btrc"
192 then
193 echo
194 echo " Rules format:"
195 echo " type=arg e.g: notify-send=\"-u low\""
196 echo " mail=\"-c copy@to rcpt@to\""
197 echo " irc=\"tcp://btr@chat.freenode.org/#btr\""
198 echo " git=\$HOME/src/btr.git"
199 echo
200 echo " Note though, that some rules do not use any argument."
201 echo
202 echo " Rulesets:"
203 for ruleset in source build report
204 do
205 printf " %10s: %s\n" $ruleset \
206 "$(find "$BTR_LIBDIR/$ruleset" -name '*.mk' -exec basename {} .mk \; | sort | xargs)"
207 done
208 echo
209 fi
210 exit
211 }
212 export -f btr-help
213
214 function btr-parseopts {
215 local shortoptions="$(btr-shortoptions common btr-flags btr-options)"
216 local longoptions="$(btr-longoptions common btr-flags btr-options)"
217 local options
218
219 options=$(getopt \
220 --name $BTR_PROG \
221 --options "$shortoptions" \
222 --longoptions "$longoptions" \
223 -- "$@" \
224 )
225 if test $? -ne 0
226 then
227 btr-help
228 fi
229
230 eval set -- "$options"
231
232 while test $# -gt 0
233 do
234 case "$1" in
235 -h|--help)
236 btr-banner
237 btr-help
238 ;;
239 -v|--verbose)
240 BTR_QUIET=false
241 BTR_VERBOSE=true
242 ;;
243 -q|--quiet)
244 BTR_QUIET=true
245 BTR_VERBOSE=false
246 ;;
247 -y|--yes)
248 BTR_FORCEYES=true
249 ;;
250 -c|--clean)
251 BTR_BUILD_CLEAN=true
252 ;;
253 -C|--vcsclean)
254 BTR_SOURCE_CLEAN=true
255 ;;
256 ####
257 -f|--config)
258 source "$2"
259 shift
260 ;;
261 ####
262 -B|--branch)
263 BTR_BRANCH="$2"
264 shift
265 ;;
266 -D|--directory)
267 BTR_RUNDIR="$2"
268 shift
269 ;;
270 -S|--suffix)
271 BTR_SUFFIX="$2"
272 shift
273 ;;
274 -T|--test)
275 BTR_TEST_ARGS="$2"
276 shift
277 ;;
278 ####
279 -s|--source)
280 case "$2" in
281 git*)
282 test -z "$BTR_BRANCH" && BTR_BRANCH=master
283 ;;
284 svn*)
285 test -z "$BTR_BRANCH" && BTR_BRANCH=trunk
286 ;;
287 cvs*)
288 test -z "$BTR_BRANCH" && BTR_BRANCH=HEAD
289 ;;
290 esac
291 BTR_SOURCE_RULES="$(cut -d= -f1 <<<$2)"
292 BTR_SOURCE_ARGS="$(cut -s -d= -f2- <<<$2)"
293 shift
294 ;;
295 -b|--build)
296 BTR_BUILD_RULES="$(cut -d= -f1 <<<$2)"
297 BTR_BUILD_ARGS="$(cut -s -d= -f2- <<<$2)"
298 shift
299 ;;
300 -r|--report)
301 BTR_REPORT_RULES="$(cut -d= -f1 <<<$2)"
302 BTR_REPORT_ARGS="$(cut -s -d= -f2- <<<$2)"
303 shift
304 ;;
305 ####
306 --)
307 shift
308 BTR_EXTRA_ARGS="$@"
309 break
310 ;;
311 esac
312 shift
313 done
314 }
315 export -f btr-parseopts
316
317 function btr-conf-dump {
318 echo "BTR_QUIET='$BTR_QUIET'"
319 echo "BTR_VERBOSE='$BTR_VEROSE'"
320 echo "BTR_FORCEYES='$BTR_FORCEYES'"
321 echo "BTR_BRANCH='$BTR_BRANCH'"
322 echo "BTR_SUFFIX='$BTR_SUFFIX'"
323 echo "BTR_RUNDIR='$BTR_RUNDIR'"
324 echo "BTR_SOURCE_RULES='$BTR_SOURCE_RULES'"
325 test ${BTR_SOURCE_ARGS+defined} && echo "BTR_SOURCE_ARGS='$BTR_SOURCE_ARGS'"
326 test ${BTR_SOURC_CLEAN+defined} && echo "BTR_SOURCE_CLEAN='$BTR_SOURCE_CLEAN'"
327 echo "BTR_BUILD_RULES='$BTR_BUILD_RULES'"
328 test ${BTR_BUILD_ARGS+defined} && echo "BTR_BUILD_ARGS='$BTR_BUILD_ARGS'"
329 test ${BTR_BUILD_CLEAN+defined} && echo "BTR_BUILD_CLEAN='$BTR_BUILD_CLEAN'"
330 test ${BTR_TEST_ARGS+defined} && echo "BTR_TEST_ARGS='$BTR_TEST_ARGS'"
331 echo "BTR_REPORT_RULES='$BTR_REPORT_RULES'"
332 test ${BTR_REPORT_ARGS+defined} && echo "BTR_REPORT_ARGS='$BTR_REPORT_ARGS'"
333 }
334 export -f btr-conf-dump
335
336 # vim: noet