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