bin/mdw-build: Missing `;;' in `case'.
[profile] / bin / mdw-build
1 #! /bin/bash
2 ###
3 ### Build script for Debian packages
4 ###
5 ### (c) 2008 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This program is free software; you can redistribute it and/or modify
11 ### it under the terms of the GNU General Public License as published by
12 ### the Free Software Foundation; either version 2 of the License, or
13 ### (at your option) any later version.
14 ###
15 ### This program is distributed in the hope that it will be useful,
16 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ### GNU General Public License for more details.
19 ###
20 ### You should have received a copy of the GNU General Public License
21 ### along with this program; if not, write to the Free Software Foundation,
22 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 ###--------------------------------------------------------------------------
25 ### Conventions for build systems.
26 ###
27 ### This script is designed to work with a variety of `make'-based build
28 ### systems, but there are a number of conventions which must be followed if
29 ### this is going to work properly.
30 ###
31 ### * There must be a `configure.ac', `configure.in', or `.links' file, or
32 ### a `.git' directory in the project top-level, so that we can find it.
33 ###
34 ### * The following `make' variables must be assigned in the top-level
35 ### Makefile, after `mdw-build' has constructed it.
36 ###
37 ### distdir The name of the top-level project directory in the
38 ### source distribution, and the base name for
39 ### distribution archives; should be of the form
40 ### `PROJECT-VERSION'.
41 ###
42 ### The following `make' targets must be available in the top-level
43 ### Makefile.
44 ###
45 ### dist Write to $(distdir).tar.gz a source distribution of
46 ### the package.
47 ###
48 ### distcheck As for `dist', but also build and test the project.
49 ###
50 ### * The source distribution constructed by `make dist' must contain a file
51 ### $(distdir)/RELEASE containing the release name. This isn't currently
52 ### tested, but it might be later.
53
54 set -e
55
56 ###--------------------------------------------------------------------------
57 ### Parse options.
58
59 usage () {
60 cat <<EOF
61 Usage: $0 [-vr] BUILDOPT
62
63 Build options:
64
65 [no]checkout[=REV]
66 [no]release
67 [no]setup[=RUNE]
68 [no]distcheck
69 [no]debian
70 [no]upload
71 [no]clean
72 [no]vpath
73 [no]native
74 EOF
75 }
76
77 ## Parse simple options.
78 verbose=no
79 while getopts "hvr" opt; do
80 case "$opt" in
81 h) usage; exit 0 ;;
82 v) verbose=yes ;;
83 *) exit 1 ;;
84 esac
85 done
86 shift $((OPTIND - 1))
87
88 ## Parse the build options.
89 checkout=yes
90 checkoutrev=HEAD
91 build=test
92 setup=yes
93 setupcmd=mdw-setup
94 distcheck=yes
95 debian=yes
96 upload=yes
97 clean=yes
98 vpath=yes
99 native=yes
100 for opt; do
101 case "$opt" in
102 checkout) checkout=yes checkoutrev=HEAD ;;
103 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
104 release) build=release ;;
105 norelease) build=test ;;
106 setup) setup=yes setupcmd=mdw-setup ;;
107 setup=*) setup=yes setupcmd=${opt#*=} ;;
108
109 distcheck | debian | upload | clean | vpath | native)
110 eval "$opt=yes"
111 ;;
112 nocheckout | nosetup | nodistcheck | nodebian | \
113 noupload | noclean | novpath | nonative)
114 eval "${opt#no}=no"
115 ;;
116 *)
117 usage >&2
118 exit 1
119 ;;
120 esac
121 done
122
123 ## Parse DEB_BUILD_OPTIONS.
124 jobs=1
125 set -- $DEB_BUILD_OPTIONS
126 for opt; do
127 case "$opt" in
128 parallel=*) jobs=${opt#*=} ;;
129 esac
130 done
131
132 makeopts=""
133 case $jobs in 1) ;; *) makeopts="$makeopts -j$jobs" ;; esac
134
135 ###--------------------------------------------------------------------------
136 ### Utility functions.
137
138 exec 3>&2 4>/dev/null 5>&2
139
140 notify () {
141 colour=$1 message=$2
142 echo $message >&4
143 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
144 }
145
146 fail () {
147 notify 1 "!!! $*"
148 exit 1
149 }
150
151 warn () {
152 case $build in
153 release) fail "$*" ;;
154 *) notify 5 "??? $*" ;;
155 esac
156 }
157
158 info () {
159 notify 6 "--- $*"
160 }
161
162 assign () {
163 info "$1 = $2"
164 eval "$1=$2"
165 }
166
167 runx () {
168 notify 2 "+++ $*"
169 "$@" 2>&3 || fail "$1: exit $?"
170 }
171
172 run () { runx "$@" >&3; }
173
174 yesno () {
175 echo -n "(test $*)" >&4
176 if "$@" >&4 2>&4; then
177 echo "(yes)" >&4
178 echo yes
179 else
180 echo "(no)" >&4
181 echo no
182 fi
183 }
184
185 ###--------------------------------------------------------------------------
186 ### Do the building.
187
188 ## Find the top-level package directory.
189 while [ ! -f configure.ac -a ! -f configure.in -a \
190 ! -f .links -a ! -d .git ]; do
191 case "$(pwd)" in
192 /)
193 fail "couldn't find top-level directory"
194 ;;
195 esac
196 cd ..
197 done
198 assign srcpath $(pwd)
199
200 ## Construct the output directory.
201 assign releasepath $srcpath/dist-$build
202 chmod -R +w $releasepath 2>/dev/null || :
203 rm -rf $releasepath 2>/dev/null || :
204 mkdir $releasepath
205 case $verbose in
206 no)
207 exec 4>$releasepath/mdw-build.log 3>&4 ||
208 fail "Failed to create log."
209 ;;
210 esac
211
212 ## Do we have a Git repository?
213 case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
214 yes,no,*)
215 fail "Inconsistent options: can't check out without setup."
216 ;;
217 yes,yes,no)
218 info "No Git repository found."
219 checkout=no gitver=none
220 ;;
221 yes,yes,yes)
222 cd $srcpath
223 [ "$(git ls-files -m)" = "" ] ||
224 warn "working tree has uncommitted changes"
225 gitver=$(git describe --abbrev=4)
226 ;;
227 esac
228
229 ## Is there Debian build equipment?
230 case "$debian,$(yesno [ -d $srcpath/debian ])" in
231 yes,no)
232 info "No debian directory found."
233 debian=no debver=none
234 ;;
235 no,*)
236 debver=none
237 ;;
238 yes,yes)
239 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p' | tr \~ -)
240 debsrc=$(dpkg-parsechangelog | sed -n 's/^Source: //p')
241 debname=$(git config user.name) debemail=$(git config user.email)
242 ;;
243 esac
244
245 ## Check the version number.
246 hack_dch_p=no
247 case "$gitver,$debver" in
248 none,* | *,none)
249 ;;
250 *)
251 if [ "$gitver" != "$debver" ]; then
252 warn "Git version $gitver doesn't match Debian version $debver"
253 hack_dch=yes
254 fi
255 ;;
256 esac
257
258 ## Maybe check out a copy of the source.
259 case "$checkout" in
260 yes)
261 cd $releasepath
262 run git clone -sn $srcpath/.git _source
263 assign srcpath $releasepath/_source
264 cd $srcpath
265 run git checkout -b mdw-build $checkoutrev
266 ;;
267 esac
268
269 ## Maybe refresh the build machinery.
270 case "$setup" in
271 yes)
272 run $setupcmd
273 ;;
274 esac
275
276 ## Initialize the build directory.
277 case "$vpath,$(yesno [ -e $srcpath/configure ])" in
278 yes,yes)
279 assign buildpath $releasepath/_build
280 mkdir $buildpath
281 cd $buildpath
282 run $srcpath/configure
283 ;;
284 no,yes)
285 info "VPATH build disabled"
286 assign buildpath $srcpath
287 distcheck=no
288 cd $srcpath
289 run ./configure
290 ;;
291 *,no)
292 info "no configure script"
293 assign buildpath $srcpath
294 cd $srcpath
295 ;;
296 esac
297
298 ## Discover the release name.
299 cat >find-distdir.mk <<'EOF'
300 include Makefile
301 print-distdir:
302 @echo >&3 $(distdir)
303 EOF
304 assign distdir \
305 $({ make -f find-distdir.mk print-distdir >/dev/null 2>&1; } 3>&1)
306
307 ## Get a tarball distribution.
308 case "$distcheck" in
309 yes)
310 run make $makeopts distcheck
311 ;;
312 no)
313 run make $makeopts dist
314 ;;
315 esac
316
317 cd $releasepath
318
319 case $native in
320 yes)
321 if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE
322 then
323 fail "missing RELEASE file in distribution"
324 fi
325 ;;
326 esac
327
328 run mv $buildpath/$distdir.tar.gz .
329 case $build in
330 release)
331 run gpg -u$(mdw-conf releasekey) -ab -o$distdir.tar.gz.gpg \
332 $distdir.tar.gz
333 ;;
334 esac
335
336 ## Maybe build the Debian packages.
337 case "$debian" in
338 yes)
339 run tar xvfz $distdir.tar.gz
340 cd $distdir
341 case $hack_dch in
342 yes)
343 dver=$(echo $gitver | sed 's/-/+/; s/-/./g')
344 now=$(date -R)
345 cat - debian/changelog >debian/changelog.new <<EOF
346 $debsrc ($dver) experimental; urgency=low
347
348 * Hacking in process, not intended for release.
349
350 -- $debname <$debemail> $now
351
352 EOF
353 mv debian/changelog.new debian/changelog
354 ;;
355 esac
356 run dpkg-buildpackage -k$(mdw-conf releasekey)
357 ;;
358 esac
359
360 ## Maybe upload Debian packages.
361 cd $releasepath
362 case "$upload,$build" in
363 yes,test)
364 info "Test build: not uploading."
365 ;;
366 yes,release)
367 run rsync $distdir.tar.gz $distdir.tar.gz.gpg \
368 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
369 case "$debian" in
370 yes)
371 run dput -f $(mdw-conf dput-target distorted) *.changes
372 ;;
373 esac
374 esac
375
376 ## Tidy up.
377 case "$clean" in
378 yes)
379 rm -rf $releasepath/$distdir
380 rm -rf $releasepath/_source
381 rm -rf $releasepath/_build
382 ;;
383 esac
384
385 ## Done.
386 info "All OK."
387
388 ###----- That's all, folks --------------------------------------------------