Merge branch 'master' of metalzone:etc/profile
[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
102info () {
103 notify 6 "--- $*"
104}
105
106assign () {
107 info "$1 = $2"
108 eval "$1=$2"
109}
110
111runx () {
112 notify 2 "+++ $*"
113 "$@" 2>&3 || fail "$1: exit $?"
114}
115
116run () { runx "$@" >&3; }
117
118yesno () {
119 echo -n "(test $*)" >&4
120 if "$@" >&4 2>&4; then
121 echo "(yes)" >&4
122 echo yes
123 else
124 echo "(no)" >&4
125 echo no
126 fi
127}
128
129###--------------------------------------------------------------------------
130### Do the building.
131
132## Find the top-level package directory.
133while [ ! -f configure.ac -a ! -f configure.in -a ! -f .links ]; do
134 case "$(pwd)" in
135 /)
136 fail "couldn't find top-level directory"
137 ;;
138 esac
139 cd ..
140done
141assign srcpath $(pwd)
142
143## Construct the output directory.
144assign releasepath $srcpath/dist-$build
145chmod -R +w $releasepath 2>/dev/null|| :
146rm -rf $releasepath 2>/dev/null || :
147mkdir $releasepath
148case $verbose in
149 no)
150 exec 4>$releasepath/mdw-build.log 3>&4 ||
151 fail "Failed to create log."
152 ;;
153esac
154
155## Maybe check out a copy of the source.
156case "$checkout,$setup,$(yesno [ -d $srcpath/.git ])" in
157 yes,no,*)
158 fail "Inconsistent options: can't check out without setup."
159 ;;
160 yes,yes,no)
161 info "No Git repository found."
162 ;;
163 yes,yes,yes)
164 cd $srcpath
165 [ "$(git ls-files -m)" = "" ] ||
166 fail "working tree has uncommitted changes"
167 cd $releasepath
168 run git clone -sn $srcpath/.git _source
169 assign srcpath $releasepath/_source
170 cd $srcpath
171 run git checkout -b mdw-build $checkoutrev
172 ;;
173esac
174
175## Maybe refresh the build machinery.
176case "$setup" in
177 yes)
178 run mdw-setup
179 ;;
180esac
181
182## Initialize the build directory.
183if [ -e $srcpath/configure ]; then
184 assign buildpath $releasepath/_build
185 mkdir $buildpath
186 cd $buildpath
187 run $srcpath/configure
188else
189 info "no configure script"
190 assign buildpath $srcpath
191 cd $srcpath
192fi
193
194## Discover the release name.
195cat >find-distdir.mk <<'EOF'
196include Makefile
197print-distdir:
198 @echo $(distdir)
199EOF
200assign distdir $(make -f find-distdir.mk print-distdir)
201
202## Get a tarball distribution.
203case "$distcheck" in
204 yes)
205 run make distcheck
206 ;;
207 no)
208 run make dist
209 ;;
210esac
211
212cd $releasepath
213
214if ! tar tfz $buildpath/$distdir.tar.gz | grep -q RELEASE; then
215 fail "missing RELEASE file in distribution"
216fi
217
218run mv $buildpath/$distdir.tar.gz .
219
220## Maybe build the Debian packages.
221case "$debian,$(yesno [ -d $srcpath/debian ])" in
222 yes,no)
223 info "No debian directory found."
224 debian=no
225 ;;
226 yes,yes)
227 run tar xvfz $distdir.tar.gz
228 cd $distdir
229 run dpkg-buildpackage -k$(mdw-conf releasekey)
230 ;;
231esac
232
233## Maybe upload Debian packages.
234cd $releasepath
235case "$upload,$build" in
236 yes,test)
237 info "Test build: not uploading."
238 ;;
239 yes,release)
9978f25a
MW
240 run rsync $distdir.tar.gz \
241 $(mdw-conf upload-target metalzone.distorted.org.uk:/home/ftp/pub/mdw/)
7ee12623
MW
242 case "$debian" in
243 yes)
244 run dput -f $(mdw-conf dput-target metalzone) *.changes
245 ;;
246 esac
247esac
248
249## Tidy up.
250case "$clean" in
251 yes)
252 rm -rf $releasepath/$distdir
253 rm -rf $releasepath/_source
254 rm -rf $releasepath/_build
255 ;;
256esac
257
258## Done.
259info "All OK."
260
261###----- That's all, folks --------------------------------------------------