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