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