setup, bin/aspell-hack, dot/emacs: Make `aspell' start in $HOME.
[profile] / bin / mdw-build
CommitLineData
7ee12623
MW
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
24set -e
25
26###--------------------------------------------------------------------------
27### Parse options.
28
29usage () {
30 cat <<EOF
31Usage: $0 [-vr] BUILDOPT
32
33Build options:
34
35 [no]checkout[=REV]
36 [no]release
37 [no]setup
38 [no]distcheck
39 [no]debian
40 [no]upload
41 [no]clean
42EOF
43}
44
45## Parse simple options.
46verbose=no
47while getopts "hvr" opt; do
48 case "$opt" in
49 h) usage; exit 0 ;;
50 v) verbose=yes ;;
51 *) exit 1 ;;
52 esac
53done
54shift $((OPTIND - 1))
55
56## Parse the build options.
57checkout=yes
58checkoutrev=HEAD
59build=test
60setup=yes
61distcheck=yes
62debian=yes
63upload=yes
64clean=yes
65for opt; do
66 case "$opt" in
67 checkout) checkout=yes checkoutrev=HEAD ;;
68 checkout=*) checkout=yes checkoutrev=${opt#*=} ;;
69 nocheckout) checkout=no ;;
70 release) build=release ;;
71 norelease) build=test ;;
72
73 setup | distcheck | debian | upload | clean)
74 eval "$opt=yes"
75 ;;
76 nosetup | nodistcheck | nodebian | noupload | noclean)
77 eval "${opt#no}=no"
78 ;;
79 *)
80 usage >&2
81 exit 1
82 ;;
83 esac
84done
85
86###--------------------------------------------------------------------------
87### Utility functions.
88
89exec 3>&2 4>/dev/null 5>&2
90
91notify () {
92 colour=$1 message=$2
93 echo $message >&4
94 echo "$(tput bold; tput setaf $colour)$message$(tput sgr0; tput op)" >&5
95}
96
97fail () {
98 notify 1 "!!! $*"
99 exit 1
100}
101
f282ba46
MW
102warn () {
103 case $build in
104 release) fail "$*" ;;
105 *) notify 5 "??? $*" ;;
106 esac
107}
108
7ee12623
MW
109info () {
110 notify 6 "--- $*"
111}
112
113assign () {
114 info "$1 = $2"
115 eval "$1=$2"
116}
117
118runx () {
119 notify 2 "+++ $*"
120 "$@" 2>&3 || fail "$1: exit $?"
121}
122
123run () { runx "$@" >&3; }
124
125yesno () {
126 echo -n "(test $*)" >&4
127 if "$@" >&4 2>&4; then
128 echo "(yes)" >&4
129 echo yes
130 else
131 echo "(no)" >&4
132 echo no
133 fi
134}
135
136###--------------------------------------------------------------------------
137### Do the building.
138
139## Find the top-level package directory.
140while [ ! -f configure.ac -a ! -f configure.in -a ! -f .links ]; do
141 case "$(pwd)" in
142 /)
143 fail "couldn't find top-level directory"
144 ;;
145 esac
146 cd ..
147done
148assign srcpath $(pwd)
149
150## Construct the output directory.
151assign releasepath $srcpath/dist-$build
152chmod -R +w $releasepath 2>/dev/null|| :
153rm -rf $releasepath 2>/dev/null || :
154mkdir $releasepath
155case $verbose in
156 no)
157 exec 4>$releasepath/mdw-build.log 3>&4 ||
158 fail "Failed to create log."
159 ;;
160esac
161
f282ba46 162## Do we have a Git repository?
7ee12623
MW
163case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
164 yes,no,*)
165 fail "Inconsistent options: can't check out without setup."
166 ;;
167 yes,yes,no)
168 info "No Git repository found."
f282ba46 169 checkout=no gitver=none
7ee12623
MW
170 ;;
171 yes,yes,yes)
172 cd $srcpath
173 [ "$(git ls-files -m)" = "" ] ||
f282ba46
MW
174 warn "working tree has uncommitted changes"
175 gitver=$(git describe)
176esac
177
178## Is there Debian build equipment?
179case "$debian,$(yesno [ -d $srcpath/debian ])" in
180 yes,no)
181 info "No debian directory found."
182 debian=no debver=none
183 ;;
184 yes,yes)
185 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p')
186 ;;
187esac
188
189## Check the version number.
190case "$gitver,$debver" in
191 none,* | *,none)
192 ;;
193 *)
194 [ "$gitver" = "$debver" ] ||
195 warn "Git version $gitver doesn't match Debian version $debver"
196 ;;
197esac
198
199## Maybe check ot a copy of the source.
200case "$checkout" in
201 yes)
7ee12623
MW
202 cd $releasepath
203 run git clone -sn $srcpath/.git _source
204 assign srcpath $releasepath/_source
205 cd $srcpath
206 run git checkout -b mdw-build $checkoutrev
207 ;;
208esac
209
210## Maybe refresh the build machinery.
211case "$setup" in
212 yes)
213 run mdw-setup
214 ;;
215esac
216
217## Initialize the build directory.
218if [ -e $srcpath/configure ]; then
219 assign buildpath $releasepath/_build
220 mkdir $buildpath
221 cd $buildpath
222 run $srcpath/configure
223else
224 info "no configure script"
225 assign buildpath $srcpath
226 cd $srcpath
227fi
228
229## Discover the release name.
230cat >find-distdir.mk <<'EOF'
231include Makefile
232print-distdir:
233 @echo $(distdir)
234EOF
235assign distdir $(make -f find-distdir.mk print-distdir)
236
237## Get a tarball distribution.
238case "$distcheck" in
239 yes)
240 run make distcheck
241 ;;
242 no)
243 run make dist
244 ;;
245esac
246
247cd $releasepath
248
249if ! tar tfz $buildpath/$distdir.tar.gz | grep -q RELEASE; then
250 fail "missing RELEASE file in distribution"
251fi
252
253run mv $buildpath/$distdir.tar.gz .
254
255## Maybe build the Debian packages.
f282ba46
MW
256case "$debian" in
257 yes)
7ee12623
MW
258 run tar xvfz $distdir.tar.gz
259 cd $distdir
260 run dpkg-buildpackage -k$(mdw-conf releasekey)
261 ;;
262esac
263
264## Maybe upload Debian packages.
265cd $releasepath
266case "$upload,$build" in
267 yes,test)
268 info "Test build: not uploading."
269 ;;
270 yes,release)
9978f25a
MW
271 run rsync $distdir.tar.gz \
272 $(mdw-conf upload-target metalzone.distorted.org.uk:/home/ftp/pub/mdw/)
7ee12623
MW
273 case "$debian" in
274 yes)
275 run dput -f $(mdw-conf dput-target metalzone) *.changes
276 ;;
277 esac
278esac
279
280## Tidy up.
281case "$clean" in
282 yes)
283 rm -rf $releasepath/$distdir
284 rm -rf $releasepath/_source
285 rm -rf $releasepath/_build
286 ;;
287esac
288
289## Done.
290info "All OK."
291
292###----- That's all, folks --------------------------------------------------