bin/mdw-build: Accept a `.git' directory as a top-level.
[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 \
141 ! -f .links -a ! -d .git ]; do
142 case "$(pwd)" in
143 /)
144 fail "couldn't find top-level directory"
145 ;;
146 esac
147 cd ..
148 done
149 assign srcpath $(pwd)
150
151 ## Construct the output directory.
152 assign releasepath $srcpath/dist-$build
153 chmod -R +w $releasepath 2>/dev/null || :
154 rm -rf $releasepath 2>/dev/null || :
155 mkdir $releasepath
156 case $verbose in
157 no)
158 exec 4>$releasepath/mdw-build.log 3>&4 ||
159 fail "Failed to create log."
160 ;;
161 esac
162
163 ## Do we have a Git repository?
164 case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
165 yes,no,*)
166 fail "Inconsistent options: can't check out without setup."
167 ;;
168 yes,yes,no)
169 info "No Git repository found."
170 checkout=no gitver=none
171 ;;
172 yes,yes,yes)
173 cd $srcpath
174 [ "$(git ls-files -m)" = "" ] ||
175 warn "working tree has uncommitted changes"
176 gitver=$(git describe)
177 esac
178
179 ## Is there Debian build equipment?
180 case "$debian,$(yesno [ -d $srcpath/debian ])" in
181 yes,no)
182 info "No debian directory found."
183 debian=no debver=none
184 ;;
185 yes,yes)
186 debver=$(dpkg-parsechangelog | sed -n 's/^Version: //p')
187 ;;
188 esac
189
190 ## Check the version number.
191 case "$gitver,$debver" in
192 none,* | *,none)
193 ;;
194 *)
195 [ "$gitver" = "$debver" ] ||
196 warn "Git version $gitver doesn't match Debian version $debver"
197 ;;
198 esac
199
200 ## Maybe check ot a copy of the source.
201 case "$checkout" in
202 yes)
203 cd $releasepath
204 run git clone -sn $srcpath/.git _source
205 assign srcpath $releasepath/_source
206 cd $srcpath
207 run git checkout -b mdw-build $checkoutrev
208 ;;
209 esac
210
211 ## Maybe refresh the build machinery.
212 case "$setup" in
213 yes)
214 run mdw-setup
215 ;;
216 esac
217
218 ## Initialize the build directory.
219 if [ -e $srcpath/configure ]; then
220 assign buildpath $releasepath/_build
221 mkdir $buildpath
222 cd $buildpath
223 run $srcpath/configure
224 else
225 info "no configure script"
226 assign buildpath $srcpath
227 cd $srcpath
228 fi
229
230 ## Discover the release name.
231 cat >find-distdir.mk <<'EOF'
232 include Makefile
233 print-distdir:
234 @echo $(distdir)
235 EOF
236 assign distdir $(make -f find-distdir.mk print-distdir)
237
238 ## Get a tarball distribution.
239 case "$distcheck" in
240 yes)
241 run make distcheck
242 ;;
243 no)
244 run make dist
245 ;;
246 esac
247
248 cd $releasepath
249
250 if ! tar tf $buildpath/$distdir.tar.gz 2>/dev/null | grep -q RELEASE; then
251 fail "missing RELEASE file in distribution"
252 fi
253
254 run mv $buildpath/$distdir.tar.gz .
255
256 ## Maybe build the Debian packages.
257 case "$debian" in
258 yes)
259 run tar xvfz $distdir.tar.gz
260 cd $distdir
261 run dpkg-buildpackage -k$(mdw-conf releasekey)
262 ;;
263 esac
264
265 ## Maybe upload Debian packages.
266 cd $releasepath
267 case "$upload,$build" in
268 yes,test)
269 info "Test build: not uploading."
270 ;;
271 yes,release)
272 run rsync $distdir.tar.gz \
273 $(mdw-conf upload-target ftp.distorted.org.uk:~ftp/pub/mdw/)
274 case "$debian" in
275 yes)
276 run dput -f $(mdw-conf dput-target distorted) *.changes
277 ;;
278 esac
279 esac
280
281 ## Tidy up.
282 case "$clean" in
283 yes)
284 rm -rf $releasepath/$distdir
285 rm -rf $releasepath/_source
286 rm -rf $releasepath/_build
287 ;;
288 esac
289
290 ## Done.
291 info "All OK."
292
293 ###----- That's all, folks --------------------------------------------------