compute_up_url: use an Iterator
[conkeror.git] / contrib / debian / nightlybuild.sh
1 #!/bin/sh
2
3 # Script for nightly builds of conkeror .debs
4 #
5 # Inspired by an idea of Nicholas A. Zigarovich <nick@servo.cc>
6 # Code by Axel Beckert <abe@deuxchevaux.org>
7 #
8 # Copyright (C) 2009-2011 Axel Beckert <abe@deuxchevaux.org>
9 #
10 # Needs the following Debian packages and their dependencies installed
11 # (besides "required" packages like coreutils and conkeror build
12 # dependencies) to work: devscripts, git-core
13 #
14 # If you want to use all features of this script, you need also the
15 # following packages: gnupg, openssh-client, dctrl-tools, gzip, bzip2
16 #
17 # And on the machine hosting the APT repository, you'll need the
18 # package reprepro.
19 #
20 # If you do only binary rebuilds for other architectures, you do _not_
21 # need to have the nightly builds APT repository in your
22 # /etc/apt/sources.list since we fetch the Sources list manually,
23 # parse it with grep-dctrl and fetch the source package with dget.
24
25 #
26 # How to use:
27 #
28 # Create the file ~/.conkeror-nightlybuildrc and write at least two
29 # lines like the following into it:
30 #
31 #  WORKDIR=/path/to/the/directory/where/the/builds/should/happen
32 #  CONTACT=you@example.com
33 #
34 # Example ~/.conkeror-nightlybuildrc similar to the one used for the
35 # initial builds on http://noone.org/conkeror-nightly-debs/:
36 #
37 #  # -*- sh -*-
38 #  WORKDIR=$HOME/conkeror.nightly
39 #  CONTACT=abe+conkeror-nightly@noone.org
40 #  SIGNKEY=373B76B4
41 #  UPLOAD=yes
42 #  UPLOAD_SSH_KEY=~/.ssh/id_rsa.conkeror-nightly
43 #  UPLOAD_SSH_HOST=noone.org
44 #  UPLOAD_SSH_USER=abe
45 #  UPLOAD_SSH_DIR=http/htdocs/conkeror-nightly
46 #  USE_REPREPRO=yes
47 #
48 # Example ~/.conkeror-nightlybuildrc similar to the one used for
49 # binary-only builds of conkeror-spawn-process-helper on
50 # http://noone.org/conkeror-nightly-debs/:
51 #
52 #  # -*- sh -*-
53 #  WORKDIR=$HOME/conkeror.nightly
54 #  CONTACT=abe+conkeror-nightly@noone.org
55 #  SIGNKEY=373B76B4
56 #  UPLOAD=yes
57 #  UPLOAD_SSH_KEY=~/.ssh/id_rsa.conkeror-nightly
58 #  UPLOAD_SSH_HOST=noone.org
59 #  UPLOAD_SSH_USER=abe
60 #  UPLOAD_SSH_DIR=http/htdocs/conkeror-nightly
61 #  USE_REPREPRO=yes
62 #  REPREPRO_DISTS="lenny squeeze sid"
63 #  BINARY_ONLY_BUILD=yes
64 #  SOURCES_LIST_URL=http://noone.org/conkeror-nightly-debs/dists/lenny/main/source/Sources.gz
65 #
66 # I think the remaining variable names are quite self-explaining. If
67 # not, look at this script's source code (you're doing that already
68 # ;-) and see where they're used. It's not too hard, the script is
69 # fairly small and straight forward. ;-)
70
71 # Bail out on any error.
72 set -e
73
74 # Check for the the config file and read it
75 RCFILE=$HOME/.conkeror-nightlybuildrc
76 if [ ! -s $RCFILE ]; then
77     echo "$RCFILE does not exist or is empty"
78     exit 1
79 fi
80 . $RCFILE
81
82 # Check if a working directory is defined. Bail out if not.
83 if [ -z "$WORKDIR" ]; then
84     echo '$WORKDIR must be defined in '$RCFILE
85     exit 2
86 fi
87
88 # Check if a contact is defined. Set a dummy value if not.
89 if [ -z "$CONTACT" ]; then
90     CONTACT=package-builder-has-not-set-contact-address@example.com
91 fi
92
93 # Check if $WORKDIR exists, otherwise create it.
94 if [ ! -d "$WORKDIR" ]; then
95     mkdir -p "$WORKDIR"
96 fi
97
98 # Define places and version number
99 MASTERDIR=$WORKDIR/MASTER
100 BUILDDIR=$WORKDIR/BUILD
101 DATE=`date -R`
102 ZULUTIME=`date -u +%y%m%d%H%M`
103
104 # Create build dir
105 mkdir -p $BUILDDIR
106
107 # Remove old builds
108 rm -rf $BUILDDIR/*
109
110 # If we just rebuild a nightly snapshot for a new architecture, we
111 # fetch the list of source packages, extract the file name of the
112 # conkeror package out of it and download the appropriate conkeror
113 # source package with dget.
114 if [ "$BINARY_ONLY_BUILD" = "yes" ]; then
115     SOURCESTMP=`mktemp`
116
117     # Download Sources file
118     SOURCEENC="${SOURCES_LIST_URL##*.}"
119     if [ -n "$SOURCEENC" ]; then
120         SOURCESTMPENC=`mktemp`
121         wget -O $SOURCESTMPENC "$SOURCES_LIST_URL";
122         if [ "$SOURCEENC" = "gz" ]; then
123             zcat $SOURCESTMPENC > $SOURCESTMP
124         elif [ "$SOURCEENC" = "bz2" ]; then
125             bzcat $SOURCESTMPENC > $SOURCESTMP
126         else
127             echo Unsupported suffix $SOURCEENC in "$SOURCES_LIST_URL" 1>&2
128             exit 1;
129         fi
130         rm -vf $SOURCESTMPENC
131     else
132         wget -O $SOURCESTMP "$SOURCES_LIST_URL";
133     fi
134
135     # Find package in Sources file
136     INFOTMP=`mktemp`
137     grep-dctrl -S conkeror $SOURCESTMP | sort-dctrl -k Version:v - | \
138         grep-dctrl -s Checksums-Sha256,Directory conkeror - > $INFOTMP
139     rm -vf $SOURCESTMP
140
141     # Calculate source package URL
142     BASE_URL="`echo \"$SOURCES_LIST_URL\" | sed -e 's:/dists/.*$::'`"
143     POOL_DIR="`grep '^Directory:' $INFOTMP | awk '{print $2}'`"
144     DSC="${BASE_URL}/${POOL_DIR}/`grep '\.dsc$' $INFOTMP | awk '{print $3}'`"
145
146     # Download source package
147     cd "$BUILDDIR"
148     dget -u "$DSC"
149
150     # Determine the correct version
151     RELEASE="`gpg < \"${DSC##*/}\" 2>&1 | grep-dctrl -s Version conkeror - | \
152         awk '{print $2}'`"
153     VERSION="`echo \"$RELEASE\" | sed -e 's/-.*$//'`"
154     DATEDIR="$BUILDDIR/conkeror-$VERSION"
155
156     WHATTOBUILD=-B
157 else
158     # Check if MASTER directory exists, if not, create it (untested code!)
159     cd $WORKDIR
160     if [ ! -d $MASTERDIR ]; then
161         git clone git://repo.or.cz/conkeror.git $MASTERDIR
162     fi
163
164     # Set the right options for debuild (signed vs unsigned packages)
165     DEBUILDOPTIONS='-uc -us'
166     if [ ! -z "$SIGNKEY" ]; then
167         DEBUILDOPTIONS="-k$SIGNKEY"
168     fi
169
170     # Update master copy
171     cd $MASTERDIR
172     git pull
173
174     # Determine the correct version
175     VERSION=`grep ^Version= $MASTERDIR/application.ini | \
176         sed -e 's/^Version=//;s/\(pre\)/~~\1/;s/\(rc\|b\|a\)/~\1/'`+git$ZULUTIME
177     RELEASE="$VERSION-~nightly1"
178     DATEDIR="$BUILDDIR/conkeror-$VERSION"
179
180     # Copy tree into build environment
181     cp -priv $MASTERDIR $DATEDIR
182
183     # Build orig.tar.gz
184     cd $BUILDDIR
185     tar cvzf conkeror_$VERSION.orig.tar.gz --exclude=.git conkeror-$VERSION
186
187     # Add an appropriate changelog entry
188     cd $DATEDIR/debian
189     mv changelog changelog.tmp
190
191     echo "conkeror ($RELEASE) UNRELEASED; urgency=low
192
193   * Automatically built package based on the state of
194     http://repo.or.cz/w/conkeror.git at $DATE
195
196  -- Conkeror Nightly Build <$CONTACT>  $DATE
197 " > changelog
198     cat changelog.tmp >> changelog
199     rm changelog.tmp
200 fi
201
202 # Set the right options for debuild (signed vs unsigned packages)
203 DEBUILDOPTIONS='-uc -us'
204 if [ ! -z "$SIGNKEY" ]; then
205     DEBUILDOPTIONS="-k$SIGNKEY"
206 fi
207
208 # Build the package
209 cd $DATEDIR
210 debuild $DEBUILDOPTIONS $WHATTOBUILD -i'(?:^|/).*~$|(?:^|/)\.#.*$|(?:^|/)\..*\.swp$|(?:^|/),,.*(?:$|/.*$)|(?:^|/)(?:DEADJOE|\.cvsignore|\.arch-inventory|\.bzrignore|\.gitignore|\.hgignore)$|(?:^|/)(?:CVS|RCS|\.deps|\{arch\}|\.arch-ids|\.svn|\.hg|_darcs|\.git|\.shelf|_MTN|\.bzr(?:\.backup|tags)|update\.sh)(?:$|/.*$)'
211
212 # Upload the files
213 cd ..
214
215 if [ "$UPLOAD" = "yes" ]; then
216     if [ "$BINARY_ONLY_BUILD" = "yes" ]; then
217         scp -i $UPLOAD_SSH_KEY -p *build *deb *changes \
218             $UPLOAD_SSH_USER@$UPLOAD_SSH_HOST:$UPLOAD_SSH_DIR
219         if [ "$USE_REPREPRO" = "yes" ]; then
220             ssh -i $UPLOAD_SSH_KEY $UPLOAD_SSH_USER@$UPLOAD_SSH_HOST \
221                 "cd $UPLOAD_SSH_DIR; for i in $REPREPRO_DISTS; do reprepro --ignore=wrongdistribution -v includedeb" '$i' *.deb "; done"
222         fi
223     else
224         scp -i $UPLOAD_SSH_KEY -p *build *deb *changes *dsc *gz \
225             $UPLOAD_SSH_USER@$UPLOAD_SSH_HOST:$UPLOAD_SSH_DIR
226
227         if [ "$USE_REPREPRO" = "yes" ]; then
228             ssh -i $UPLOAD_SSH_KEY $UPLOAD_SSH_USER@$UPLOAD_SSH_HOST \
229                 "cd $UPLOAD_SSH_DIR; for i in $REPREPRO_DISTS; do reprepro --ignore=wrongdistribution -v include" '$i' *.changes "; done"
230         fi
231     fi
232 fi