summaryrefslogtreecommitdiffstats
path: root/bin/pbuild.sh
blob: 1df64f5f2a488951bed1ae2a1e6b2f4e7342d4e6 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/sh

# Copyright (C) 2015 Daniel Baumann <mail@daniel-baumann.ch>
#
# This program 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 program 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, see <http://www.gnu.org/licenses/>.

set -e

case "$(basename ${0})" in
	p*)
		MODE="progress-linux"
		;;

	b*)
		MODE="bfh"
		;;
esac

PROGRAM="${0}"

case "$(dpkg --print-architecture)" in
#	arm64)
#		CPUS="1"
#		export DEB_BUILD_OPTIONS=parallel=${CPUS}
#		;;

	*)
		CPUS="$(nproc)"
		export DEB_BUILD_OPTIONS=parallel=${CPUS}
		;;
esac

Parameters()
{
	LONG_OPTIONS="auto,build:,distribution:,interactive,nocheck,only-missing-builds,package:,tag:,upload,"
	OPTIONS="a,b:,d:,i,n,o,p:,t:,u,"

	PARAMETERS="$(getopt --longoptions ${LONG_OPTIONS} --name=${PROGRAM} --options ${OPTIONS} --shell sh -- ${@})"

	if [ "${?}" != "0" ]
	then
		echo "${PROGRAM}: getopt exit" >&2
		exit 1
	fi

	eval set -- "${PARAMETERS}"

	while true
	do
		case "${1}" in
			-a|--auto)
				AUTO="true"
				shift 1
				;;

			-b|--build)
				BUILD="${2}"
				shift 2
				;;

			-d|--distribution)
				DISTRIBUTION="${2}"
				shift 2
				;;

			-i|--interactive)
				INTERACTIVE="true"
				shift 1
				;;

			-n|--nocheck)
				NOCHECK="true"
				shift 1
				;;

			-p|--package)
				PACKAGE="$(echo ${2} | sed -e 's|/$||g')"
				shift 2
				;;

			-o|--only-missing-builds)
				ONLY_MISSING_BUILDS="true"
				shift 1
				;;

			-t|--tag)
				TAG="${2}"
				shift 2
				;;

			-u|--upload)
				UPLOAD="true"
				shift
				;;

			--)
				shift
				break
				;;

			*)
				echo "${PROGRAM}: getopt error" >&2
				exit 1
				;;
		esac
	done
}

Parameters "${@}"

if [ -z "${DISTRIBUTION}" ] || [ -z "${PACKAGE}" ]
then
	echo "Usage: ${PROGRAM} -a|--auto [-b|--build BUILD] -d|--distribution DISTRIBUTION -n|--nocheck -p|--package PACKAGE -o|--only-missing-builds -t|--tag ALL|TAG[,TAG,...] -u|--upload" >&2
	exit 1
fi

INTERACTIVE="${INTERACTIVE:=false}"

if [ -e "${PACKAGE}" ]
then
	echo "${PACKAGE}: directory already exists"
	exit 1
fi

TAGS="$(echo ${TAG} | sed -e 's|,| |g')"

case "${TAGS}" in
	ALL)
		TAGS="$(wget -q https://git.progress-linux.org/packages/${DISTRIBUTION}/${PACKAGE}/refs/ -O - | grep "<tr><td><a href='/packages/${DISTRIBUTION}/${PACKAGE}/tag/?h=progress-linux/" | awk -F\> '{ print $4 }' | sed -e 's|<.*||' | sort -V)"
		;;
esac

if [ -z "${BUILD}" ]
then
	case "$(dpkg --print-architecture)" in
		amd64) # FIXME
			BUILD="source,binary-arch,binary-indep"
			;;

		arm*|i386) # FIXME
			BUILD="binary-arch"
			;;
	esac
fi

TARGET="${PACKAGE}"

case "${MODE}" in
	progress-linux)
		REPOSITORY="https://git.progress-linux.org/packages/${DISTRIBUTION}/${PACKAGE}"
		BRANCH="progress-linux"
		SERVER="https://apt.progress-linux.org"
		;;

	bfh)
		REPOSITORY="https://git.bfh.science/packages/${DISTRIBUTION}/${PACKAGE}"
		BRANCH="bfh"
		SERVER="https://apt.bfh.science"
		;;
esac

for TAG in ${TAGS} HEAD
do
	# if no tag is specified, build head.
	# if a tag is specified, don't (additionally) build head
	if [ "${TAG}" = "HEAD" ]
	then
		if [ -z "${TAGS}" ]
		then
			TAG=""
		else
			continue
		fi
	fi

	if [ -n "${TAG}" ]
	then
		VERSION="$(echo ${TAG} | awk -F/ '{ print $2 }' | sed -e 's|%|:|g' -e 's|_|~|g')"
	fi

	for BUILD in $(echo ${BUILD} | sed -e 's|,| |g')
	do
		echo "################################################################################"
		echo "Building ${BUILD} ($(dpkg --print-architecture)) package: ${PACKAGE} ${VERSION}"
		echo "################################################################################"

		case "${ONLY_MISSING_BUILDS}" in
			true)
				case "${BUILD}" in
					binary-indep)
						URL="https://apt.progress-linux.org/archive/${DISTRIBUTION}/${PACKAGE}/${VERSION}_all"
						;;

					source)
						URL="https://apt.progress-linux.org/archive/${DISTRIBUTION}/${PACKAGE}/${VERSION}_source"
						;;

					*)
						URL="https://apt.progress-linux.org/archive/${DISTRIBUTION}/${PACKAGE}/${VERSION}_$(dpkg --print-architecture)"
						;;
				esac

				if wget -q ${URL} -O /dev/null
				then
					echo "### skipping build, it's already present."
					continue
				fi
				;;
		esac

		case "${BUILD}" in
			source)
				if [ -n "${TAG}" ]
				then
					GIT_BRANCH="${TAG}"
					git clone --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
				else
					GIT_BRANCH="${BRANCH}"
					#git clone --depth 1 --no-single-branch --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
					git clone --no-single-branch --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
				fi

				cd "${TARGET}"

				VERSION="$(dpkg-parsechangelog | awk '/^Version: / { print $2 }')"

				SOURCE_VERSION="$(echo ${VERSION} | awk -F: '{ print $2 }')"
				SOURCE_VERSION="${SOURCE_VERSION:-${VERSION}}"

				UPSTREAM_VERSION="$(echo ${SOURCE_VERSION} | awk -F- '{ $NF=""; print $0 }' | sed -e 's| |-|g' -e 's|-$||')"

				if [ -z "${UPSTREAM_VERSION}" ] && echo "${SOURCE_VERSION}" | grep -qs '+progress[0-9]*'
				then
					UPSTREAM_VERSION="$(echo ${SOURCE_VERSION} | awk -F\\+progress '{ print $1 }')"
				fi

				if [ -z "${UPSTREAM_VERSION}" ] && echo "${SOURCE_VERSION}" | grep -qs '+bfh[0-9]*'
				then
					UPSTREAM_VERSION="$(echo ${SOURCE_VERSION} | awk -F\\+bfh '{ print $1 }')"
				fi

				rm -rf .git
				mv debian ../
				cd "${OLDPWD}"

				mv "${TARGET}" "${TARGET}-${UPSTREAM_VERSION}"

				for DIST in engywuck fuchur graograman
				do
					if [ ! -e "${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz" ]
					then
						wget ${SERVER}/archive/${DIST}/${PACKAGE}/orig/${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz || true
						if [ ! -e ${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz ]
						then
							wget ${SERVER}/archive/${DIST}-backports/${PACKAGE}/orig/${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz || true
							if [ ! -e ${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz ]
							then
								wget ${SERVER}/archive/${DIST}-extras/${PACKAGE}/orig/${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz || true
								if [ ! -e ${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz ]
								then
									wget ${SERVER}/archive/${DIST}-backports-extras/${PACKAGE}/orig/${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz || true
								fi
							fi
						fi
					fi
				done

				if [ ! -e "${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz" ]
				then
					echo "Creating ${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz ..."
					tar cfJ "${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz" "${TARGET}-${UPSTREAM_VERSION}"
				else
					echo "Using existing ${TARGET}_${UPSTREAM_VERSION}.orig.tar.xz ..."
				fi

				mv debian "${TARGET}-${UPSTREAM_VERSION}"
				dpkg-source -b "${TARGET}-${UPSTREAM_VERSION}"

				rm -rf "${TARGET}-${UPSTREAM_VERSION}"

				# upload
				case "${UPLOAD}" in
					true)
						FILES="$(sed -e '1,/^Files:$/ d' ${TARGET}_${SOURCE_VERSION}.dsc | sed -e '/^[0-1a-zA-Z]/,$d' | awk '{ print $3 }')"

						rsync -aP ${FILES} ${TARGET}_${SOURCE_VERSION}.dsc apt.progress-linux.org::upload
						rm -f ${FILES} ${TARGET}_${SOURCE_VERSION}.dsc
						;;
				esac
				;;

			binary*)
				rm -f /tmp/control
				CONTROL="${REPOSITORY}/plain/debian/control"
				wget -q ${CONTROL} -O /tmp/control

				case "${BUILD}" in
					binary-arch)
						if ! grep -qs "^Architecture:.* any" /tmp/control && \
						   ! grep -qs "^Architecture:.* linux-any" /tmp/control && \
						   ! grep -qs "^Architecture:.* $(dpkg --print-architecture)" /tmp/control
						then
							echo "NO BINARY_ARCH, skipping."
							continue
						fi
						;;

					binary-indep)
						if ! grep -qs '^Architecture:.*all' /tmp/control
						then
							echo "NO BINARY_INDEP, skipping."
							continue
						fi
						;;
				esac

				if [ -n "${TAG}" ]
				then
					GIT_BRANCH="${TAG}"
					git clone --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
				else
					GIT_BRANCH="${BRANCH}"
					#git clone --depth 1 --no-single-branch --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
					git clone --no-single-branch --branch ${GIT_BRANCH} "${REPOSITORY}" "${TARGET}"
				fi

				cd "${TARGET}"

				rm -rf .git

				VERSION="$(dpkg-parsechangelog | awk '/^Version: / { print $2 }')"

				SOURCE_VERSION="$(echo ${VERSION} | awk -F: '{ print $2 }')"
				SOURCE_VERSION="${SOURCE_VERSION:-${VERSION}}"

				UPSTREAM_VERSION="$(echo ${SOURCE_VERSION} | awk -F- '{ $NF=""; print $0 }' | sed -e 's| |-|g' -e 's|-$||')"

				SOURCE="${TARGET}-$(echo ${SOURCE_VERSION} | sed -e 's|~|_|g' -e 's|+|.|g')"

				cd ..
				mv "${TARGET}" "${SOURCE}"
				cd "${SOURCE}"

				case "${NOCHECK}" in
					true)
						export DEB_BUILD_OPTIONS="${DEB_BUILD_OPTIONS} nocheck"

						# disable tests
						for RULE in	dh_auto_test dh_auto_test-arch dh_auto_test-indep \
								override_dh_auto_test override_dh_auto_test-arch override_dh_auto_test-indep \
								execute_before_dh_auto_test execute_before_dh_auto_test-arch execute_before_dh_auto_test-indep \
								execute_after_dh_auto_test execute_after_dh_auto_test-arch execute_after_dh_auto_test-indep
						do
							if grep -qs "^${RULE}:" debian/rules
							then
								sed -i -e "s|^${RULE}:|nocheck_${RULE}:|" debian/rules
							else
								echo "${RULE}:" >> debian/rules
							fi
						done
						;;
				esac

				case "${AUTO}" in
					true)
						case "${DISTRIBUTION}" in
							engywuck-backports)
								if grep -qs 'debhelper (>= 13' debian/control || \
								   grep -qs 'debhelper-compat (= 13' debian/control
								then
									sudo apt install -t engywuck-backports -y debhelper
								fi
								;;
						esac

						sudo apt build-dep -y .
						;;
				esac

				case "${INTERACTIVE}" in
					true)
						echo "${PROGRAM}: WAITING... "
						read waiting
						;;
				esac

				case "${BUILD}" in
					binary-any)
						dpkg-buildpackage -sa -us -uc
						;;

					binary-arch)
						dpkg-buildpackage -B -us -uc
						;;

					binary-indep)
						dpkg-buildpackage -A -us -uc
						;;
				esac

				cd "${OLDPWD}"

				rm -rf "${SOURCE}"

				# upload
				case "${UPLOAD}" in
					true)
						FILES="$(sed -e '1,/^Files:$/ d' ${TARGET}_${SOURCE_VERSION}_*.changes | sed -e '/^[0-1a-zA-Z]/,$d' | awk '{ print $5 }')"

						rsync -aP ${FILES} ${TARGET}_${SOURCE_VERSION}_*.changes apt.progress-linux.org::upload
						rm -f ${FILES} ${TARGET}_${SOURCE_VERSION}_*.changes
						;;
				esac
				;;
		esac
	done
done