summaryrefslogtreecommitdiff
path: root/bin/itzks-build-package
blob: 6f8cbeee26ca6f266065d4a2eab124370ac293c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash

# Copyright (C) 2011 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This programme is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This programme is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

test -z $1 && { echo "usage: <itzks-git-project> {main,main/<codename>,nightly,nightly/<codename>} [<git-checkout>]"; exit -1; }

set -ex

export GIT_SERVER="code.it-zukunft-schule.de"
export DEBEMAIL=itzks-packages@it-zukunft-schule.de
export DEBFULLNAME="ITZKS Packages"
export GPG_KEY="E41B37C658842183"

set_vars() {
	USE_SUDO="yes"
	PDEBUILD="pdebuild --pbuilder qemubuilder"
	TEMP_BASE="$HOME/tmp/"

	# first argv is the name of the Git project
	PROJECT=$1

	# grab repository component area from command line (2nd argv) or guess it
	ARGV2_COMPONENT=$(echo "$2/" | cut -d"/" -f1)
	ARGV2_CODENAME=$(echo "$2/" | cut -d"/" -f2)
	COMPONENT=${ARGV2_COMPONENT:-${COMPONENT:-nightly}}
	CODENAMES=${ARGV2_CODENAME:-${CODENAMES}}
	[ -n "$ARGV2_CODENAME" ] && FORCE_BUILD=0 || FORCE_BUILD=-1
	if [ "x$COMPONENT" = "xmain" ]; then
		CHECKOUT=${3:-build-main}
	elif [ "x$COMPONENT" = "xnightly" ]; then
		CHECKOUT=${3:-master}
		DATE="~${DATE:-$(date +%Y%m%d)}"
	else
		echo "error: no such package component area for ITZKS packages. Aborting..."
		exit -1
	fi
	# the DATE might be given as ,,today'' from the command line
	[ "x$DATE" = "xtoday" ] && DATE="~$(date +%Y%m%d)"

	# setting paths
	PROJECT_DIR=$HOME/build/$COMPONENT/$PROJECT
	DIST_SUPPORTED="debian ubuntu"
	PKGDIST="$HOME/pkg-dist/$COMPONENT/$PROJECT"

	# build for other architectures than amd64/i386
	EXTRA_ARCHS="${EXTRA_ARCHS:-}"
	EXTRA_ARCHS_ONLY="${EXTRA_ARCHS_ONLY:-}"

	# creating paths
	mkdir -p "$TEMP_BASE"
	mkdir -p $PROJECT_DIR
	mkdir -p $PKGDIST

	return 0
}

clear_pkgdist() {

	# pkgdist directory cleanup
	cat BUILDS_FOR | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST=$(echo $line | cut -d":" -f1 | tr [A-Z] [a-z])
		CODENAMES=${CODENAMES:-$(echo $line | cut -d":" -f2- | tr [A-Z] [a#-z])}
		echo "$DIST_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $CODENAMES; do
				if [ "x$EXTRA_ARCHS_ONLY" = "x" ]; then
					for l_ARCH in amd64 i386; do
						mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_*.changes
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_*.upload
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_*.build
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_*.dsc
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_*.tar.gz
						rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/*.deb
					done
				fi
				for l_EXTRA_ARCH in $EXTRA_ARCHS; do 
					mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_*.changes
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_*.upload
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_*.build
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_*.dsc
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_*.tar.gz
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/*.deb
				done	
			done
		}
	done
	return 0
}

prepare_workspace() {
	# in any case remove the BUILDS_FOR file
	rm -f $PROJECT_DIR/BUILDS_FOR

	# make sure our local working copy is up to date...

	if [ -d $PROJECT_DIR/.git ]; then 
		cd $PROJECT_DIR && git reset --hard
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT
		git pull origin $CHECKOUT
		# and again, get the $CHECKOUT refspec in pure state
		git reset --hard
	else
		cd $(dirname $PROJECT_DIR)
		git clone git://$GIT_SERVER/$PROJECT.git
		cd $PROJECT 
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT;
	fi
	cd $PROJECT_DIR

	test -f debian/source/format && cat debian/source/format | egrep '^3.0.*\(quilt\)$' >/dev/null && {
		UPSTREAM_VERSION=`head -n1 debian/changelog | sed 's,.*(\(.*\)).*,\1,' | sed 's,\(\.dfsg.*\),,' | cut -d"-" -f1`
		ORIG_TARBALL="${PROJECT}_${UPSTREAM_VERSION}.orig.tar.gz"
		git checkout --force pristine-tar && pristine-tar checkout ../${ORIG_TARBALL}
	}
	# by default we build for all current debian versions
	test -f BUILDS_FOR || cat > BUILDS_FOR <<EOF
debian: squeeze wheezy sid
#ubuntu: lucid maverick natty
EOF
	return 0
}

build_packages() {
	# use pbuilder for building all variants of this package
	cat BUILDS_FOR | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST=$(echo $line | cut -d":" -f1 | tr [A-Z] [a-z])
		CODENAMES=${CODENAMES:-$(echo $line | cut -d":" -f2- | tr [A-Z] [a#-z])}
		echo "$DIST_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $CODENAMES; do
				TEMP_DIR="$(mktemp -d --tmpdir=$TEMP_BASE)"
				mkdir -p $TEMP_DIR/$PROJECT
				git clone --local $PROJECT_DIR $TEMP_DIR/$PROJECT/ 
				cd $TEMP_DIR/$PROJECT
				git checkout $CHECKOUT || git checkout master
				GITREV=$(itzks-gitrevno)
				# translate the version name for Debian releases
				[ "x$l_CODENAME" = "xsid" ] && VERSION=unstable
				[ "x$l_CODENAME" = "xwheezy" ] && VERSION=testing
				[ "x$l_CODENAME" = "xsqueeze" ] && VERSION=stable
				[ "x$l_CODENAME" = "xlenny" ] && VERSION=oldstable
				
				# modify the section for non-main package builds
				[ "x$COMPONENT" = "xmain" ] || {
					mv debian/control debian/control.tmp
					cat debian/control.tmp | sed  "s#Section:[\ ]*\(.*\)#Section: $COMPONENT/\1#g" > debian/control
				}
				
				# modify changelog for this build
				dch --distribution $VERSION --force-distribution -l "+$l_CODENAME~$COMPONENT$DATE~$GITREV~build" "Auto-built $l_DIST $l_CODENAME package for packages.it-zukunft-schule.de repository."
				mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/{amd64,i386}
				OTHERMIRROR="deb http://packages.it-zukunft-schule.de/debian $l_CODENAME $COMPONENT"
				[ "x$USE_SUDO" != "xyes" ] && {
					[ "x$EXTRA_ARCHS_ONLY" = "x" ] && {
						cat debian/control | egrep 'Architecture.*(all|any|amd64)' >/dev/null && {
							DIST=$l_DIST CODENAME=$l_CODENAME ARCH=amd64 $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/amd64
						}
						cat debian/control | egrep 'Architecture.*(any|i386)' >/dev/null && {
							DIST=$l_DIST CODENAME=$l_CODENAME ARCH=i386  $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/i386  -- --binary-arch
						}
					}
					for extra_arch in $EXTRA_ARCHS; do
						mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/$extra_arch
						cat debian/control | egrep "Architecture.*(any|$extra_arch)" >/dev/null && {
							DIST=$l_DIST CODENAME=$l_CODENAME ARCH=$extra_arch $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/$extra_arch -- --binary-arch
						}
					done
				}
				[ "x$USE_SUDO" = "xyes" ] && {
					[ "x$EXTRA_ARCHS_ONLY" = "x" ] && {
						cat debian/control | egrep 'Architecture.*(all|any|amd64)' >/dev/null && {
							sudo DIST=$l_DIST CODENAME=$l_CODENAME ARCH=amd64 OTHERMIRROR="$OTHERMIRROR" $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/amd64
						}
						cat debian/control | egrep 'Architecture.*(any|i386)' >/dev/null && {
							sudo DIST=$l_DIST CODENAME=$l_CODENAME ARCH=i386  OTHERMIRROR="$OTHERMIRROR" $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/i386  -- --binary-arch
						}
					}
					for extra_arch in $EXTRA_ARCHS; do
						mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/$extra_arch
						cat debian/control | egrep "Architecture.*(any|$extra_arch)" >/dev/null && {
						    sudo DIST=$l_DIST CODENAME=$l_CODENAME ARCH=$extra_arch OTHERMIRROR="$OTHERMIRROR" $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult $PKGDIST/$l_DIST/$l_CODENAME/$extra_arch -- --binary-arch
						}
					done
				}
				cd -
				rm -Rf $TEMP_DIR
			done
			echo
		}
		echo
	done
	return 0
}

upload_packages() {
	# dupload the new packages to the reprepro repository
	cd $PKGDIST
	cat $PROJECT_DIR/BUILDS_FOR | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST=$(echo $line | cut -d":" -f1 | tr [A-Z] [a-z])
		CODENAMES=${CODENAMES:-$(echo $line | cut -d":" -f2- | tr [A-Z] [a#-z])}
		for l_CODENAME in $CODENAMES; do
			if [ "x$EXTRA_ARCHS_ONLY" = "x" ]; then	
				for l_ARCH in amd64 i386; do 
					cd $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH
					ls $PROJECT_*.changes &>/dev/null && dupload --to itzks-$l_DIST-$l_CODENAME $PROJECT_*.changes
					cd -
				done
			fi
			for l_EXTRA_ARCH in $EXTRA_ARCHS; do 
				cd $PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH
				ls $PROJECT_*.changes &>/dev/null && dupload --to itzks-$l_DIST-$l_CODENAME $PROJECT_*.changes
				cd -
			done
		done
	done
	cd -
	return 0
}

### MAIN ###
set_vars $@ && {
	if [ "x$(basename $0)" = "xitzks-build-package" ] || [ "x$(basename $0)" = "xitzks-build+upload-package" ]; then
		cd $PROJECT_DIR && itzks-pkgneedsbuild $CHECKOUT || [ "$FORCE_BUILD" -eq 0 ] && {
			clear_pkgdist
			prepare_workspace && {
				build_packages
			}
		}
	fi
	if [ "x$(basename $0)" = "xitzks-upload-package" ] || [ "x$(basename $0)" = "xitzks-build+upload-package" ]; then
		upload_packages
	fi
}