| 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 | info () { |
| 103 | notify 6 "--- $*" |
| 104 | } |
| 105 | |
| 106 | assign () { |
| 107 | info "$1 = $2" |
| 108 | eval "$1=$2" |
| 109 | } |
| 110 | |
| 111 | runx () { |
| 112 | notify 2 "+++ $*" |
| 113 | "$@" 2>&3 || fail "$1: exit $?" |
| 114 | } |
| 115 | |
| 116 | run () { runx "$@" >&3; } |
| 117 | |
| 118 | yesno () { |
| 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. |
| 133 | while [ ! -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 .. |
| 140 | done |
| 141 | assign srcpath $(pwd) |
| 142 | |
| 143 | ## Construct the output directory. |
| 144 | assign releasepath $srcpath/dist-$build |
| 145 | chmod -R +w $releasepath 2>/dev/null|| : |
| 146 | rm -rf $releasepath 2>/dev/null || : |
| 147 | mkdir $releasepath |
| 148 | case $verbose in |
| 149 | no) |
| 150 | exec 4>$releasepath/mdw-build.log 3>&4 || |
| 151 | fail "Failed to create log." |
| 152 | ;; |
| 153 | esac |
| 154 | |
| 155 | ## Maybe check out a copy of the source. |
| 156 | case "$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 | ;; |
| 173 | esac |
| 174 | |
| 175 | ## Maybe refresh the build machinery. |
| 176 | case "$setup" in |
| 177 | yes) |
| 178 | run mdw-setup |
| 179 | ;; |
| 180 | esac |
| 181 | |
| 182 | ## Initialize the build directory. |
| 183 | if [ -e $srcpath/configure ]; then |
| 184 | assign buildpath $releasepath/_build |
| 185 | mkdir $buildpath |
| 186 | cd $buildpath |
| 187 | run $srcpath/configure |
| 188 | else |
| 189 | info "no configure script" |
| 190 | assign buildpath $srcpath |
| 191 | cd $srcpath |
| 192 | fi |
| 193 | |
| 194 | ## Discover the release name. |
| 195 | cat >find-distdir.mk <<'EOF' |
| 196 | include Makefile |
| 197 | print-distdir: |
| 198 | @echo $(distdir) |
| 199 | EOF |
| 200 | assign distdir $(make -f find-distdir.mk print-distdir) |
| 201 | |
| 202 | ## Get a tarball distribution. |
| 203 | case "$distcheck" in |
| 204 | yes) |
| 205 | run make distcheck |
| 206 | ;; |
| 207 | no) |
| 208 | run make dist |
| 209 | ;; |
| 210 | esac |
| 211 | |
| 212 | cd $releasepath |
| 213 | |
| 214 | if ! tar tfz $buildpath/$distdir.tar.gz | grep -q RELEASE; then |
| 215 | fail "missing RELEASE file in distribution" |
| 216 | fi |
| 217 | |
| 218 | run mv $buildpath/$distdir.tar.gz . |
| 219 | |
| 220 | ## Maybe build the Debian packages. |
| 221 | case "$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 | ;; |
| 231 | esac |
| 232 | |
| 233 | ## Maybe upload Debian packages. |
| 234 | cd $releasepath |
| 235 | case "$upload,$build" in |
| 236 | yes,test) |
| 237 | info "Test build: not uploading." |
| 238 | ;; |
| 239 | yes,release) |
| 240 | run rsync $distdir.tar.gz \ |
| 241 | $(mdw-conf upload-target metalzone.distorted.org.uk:/home/ftp/pub/mdw/) |
| 242 | case "$debian" in |
| 243 | yes) |
| 244 | run dput -f $(mdw-conf dput-target metalzone) *.changes |
| 245 | ;; |
| 246 | esac |
| 247 | esac |
| 248 | |
| 249 | ## Tidy up. |
| 250 | case "$clean" in |
| 251 | yes) |
| 252 | rm -rf $releasepath/$distdir |
| 253 | rm -rf $releasepath/_source |
| 254 | rm -rf $releasepath/_build |
| 255 | ;; |
| 256 | esac |
| 257 | |
| 258 | ## Done. |
| 259 | info "All OK." |
| 260 | |
| 261 | ###----- That's all, folks -------------------------------------------------- |