summaryrefslogtreecommitdiffstats
path: root/bin/pimport.sh
blob: 7102cd5a5754c25ec0b8eb9ecbaa9c7e9fad08b4 (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#!/bin/sh

set -e

case "$(basename ${0})" in
	d*)
		MODE="debian"
		;;

	p*)
		MODE="progress-linux"
		;;

	b*)
		MODE="bfh"
		;;
esac

case "${MODE}" in
	debian)
		DOWNSTREAM_BRANCH=""
		;;

	progress-linux)
		MAINTAINER="Progress Linux Maintainers <maintainers@lists.progress-linux.org>"
		UPLOADERS="Daniel Baumann <daniel.baumann@progress-linux.org>"
		BUGS="mailto:maintainers@lists.progress-linux.org"

		if echo ${PWD} | grep -qs engywuck-backports
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/engywuck-backports"
			VCS_GIT="https://git.progress-linux.org/packages/engywuck-backports"
		elif echo ${PWD} | grep -qs fuchur-backports
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/fuchur-backports"
			VCS_GIT="https://git.progress-linux.org/packages/fuchur-backports"
		elif echo ${PWD} | grep -qs graograman-backports
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/graograman-backports"
			VCS_GIT="https://git.progress-linux.org/packages/graograman-backports"
		elif echo ${PWD} | grep -qs engywuck
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/engywuck"
			VCS_GIT="https://git.progress-linux.org/packages/engywuck"
		elif echo ${PWD} | grep -qs fuchur
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/fuchur"
			VCS_GIT="https://git.progress-linux.org/packages/fuchur"
		elif echo ${PWD} | grep -qs graograman
		then
			VCS_BROWSER="https://git.progress-linux.org/packages/graograman"
			VCS_GIT="https://git.progress-linux.org/packages/graograman"
		else
			echo "NO VCS detection"
			exit 1
		fi

		DOWNSTREAM_BRANCH="progress-linux"
		;;

	bfh)
		MAINTAINER="Team Linux \& Infrastructure Services <bfh-linux-sysadmin@lists.bfh.science>"
		UPLOADERS="Daniel Baumann <daniel.baumann@bfh.ch>"
		BUGS="mailto:bfh-linux-users@lists.bfh.science"

		VCS_BROWSER="https://git.bfh.science/packages/buster-backports"
		VCS_GIT="https://git.bfh.science/packages/buster-backports"

		DOWNSTREAM_BRANCH="bfh"
		;;
esac

RED="\033[1;33;31m"
GREEN="\033[1;33;32m"
YELLOW="\033[1;33;33m"
BLUE="\033[1;33;34m"
WHITE="\033[1;33;37m"
NORMAL="\033[0m"
PURPLE="\033[1;33;35m"

IMPORT="${1}"

case "${IMPORT}" in
	*.dsc|*.debian.tar.*)
		;;

	*)
		echo "Usage: ${0} *.dsc"
		echo "Usage: ${0} debian.tar.*"
		exit 1
		;;
esac

case "${IMPORT}" in
	http*)
		IMPORT_TEMPDIR="$(mktemp -d)"

		cd "${IMPORT_TEMPDIR}"

		case "${IMPORT}" in
			*.dsc)
				dget -d "${IMPORT}"
				;;

			*)
				wget "${IMPORT}"
				;;
		esac

		cd "${OLDPWD}"

		IMPORT="${IMPORT_TEMPDIR}/$(basename ${IMPORT})"
		;;
esac

PACKAGE="$(basename ${IMPORT} | awk -F_ '{ print $1 }')"

case "${IMPORT}" in
	*.dsc)
		DEB_VERSION="$(awk '/^Version: / { print $2 }' ${IMPORT})"

		case "${DEB_VERSION}" in
			*-*)
				DEB_UPSTREAM_VERSION="$(echo ${DEB_VERSION} | awk -F- '{ $NF=""; print $0 }' | sed -e 's| |-|g' -e 's|-$||g')"
				;;

			*)
				DEB_UPSTREAM_VERSION="${DEB_VERSION}"
				;;
		esac
		;;
esac

Add_initial_commit ()
{
	if [ ! -e .git ]
	then
		git init --shared
	fi

	if ! git log > /dev/null 2>&1
	then
		echo
		echo "################################################################################"
		echo "git: adding initial commit"
		echo "################################################################################"

		git commit -a -s -S -m "Initial commit." --allow-empty
	fi
}

Create_upstream_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	case "${CURRENT_BRANCH}" in
		master|main)
			if ! git branch | grep -qs upstream
			then
				echo
				echo "################################################################################"
				echo "git: adding upstream branch"
				echo "################################################################################"

				git branch -m ${CURRENT_BRANCH} upstream
			fi
			;;
	esac
}

Switch_upstream_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" != "upstream" ] && git branch | grep -qs upstream
	then
		echo
		echo "################################################################################"
		echo "git: switching to upstream branch"
		echo "################################################################################"

		git checkout upstream
	fi
}

Remove_everything ()
{
	echo
	echo "################################################################################"
	echo "git: removing all files"
	echo "################################################################################"

	echo -n "Removing files for new import..."

	find . -maxdepth 1 -mindepth 1 -and -not -name .git -print0 | xargs -0 -I '{}' rm -rf '{}'

	echo " done."
}

Unpack_upstream ()
{
	echo
	echo "################################################################################"
	echo "dpkg: unpacking upstream files"
	echo "################################################################################"

	TEMPDIR="$(mktemp -d -p . -u)"

	dpkg-source -x --no-copy --skip-debianization ${IMPORT} "${TEMPDIR}"
	rm -rf "${TEMPDIR}/debian"
	find "${TEMPDIR}" -type d -name ".git" -exec rm -rf {} \; || true

	DEBIAN_TAR="$(dirname $(readlink -f ${IMPORT}))/$(awk '/debian.tar.*$/ { print $3 }' ${IMPORT} | head -n1)"

	case "${DEBIAN_TAR}" in
		*.debian.tar.*)
			if [ -e "${DEBIAN_TAR}" ]
			then
				# this includes upstream additions outside debian/ into the upstream branch
				cd "${TEMPDIR}"
				tar xf "${DEBIAN_TAR}"
				rm -rf debian
				rm -rf .git
				cd "${OLDPWD}"
			fi
			;;
	esac

	find "${TEMPDIR}" -maxdepth 1 -mindepth 1 -print0 | xargs -0 -I '{}' mv '{}' .
	rmdir "${TEMPDIR}"
}

Add_upstream_version ()
{
	echo
	echo "################################################################################"
	echo "git: adding upstream version"
	echo "################################################################################"

	# FIXME
	git-upstream-add ${DEB_UPSTREAM_VERSION}
}

Create_debian_branch ()
{
	echo
	echo "################################################################################"
	echo "git: creating debian branch"
	echo "################################################################################"

	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" = "upstream" ] && ! git branch | grep -qs debian
	then
		git branch debian
	fi
}

Switch_debian_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" != "debian" ] && git branch | grep -qs debian
	then
		echo
		echo "################################################################################"
		echo "git: switching to debian branch"
		echo "################################################################################"

		git checkout debian
	fi
}

Unpack_debian ()
{
	echo
	echo "################################################################################"
	echo "dpkg: unpacking debian files"
	echo "################################################################################"

	TEMPDIR="$(mktemp -d -p . -u)"

	dpkg-source -x --no-copy --skip-patches ${IMPORT} ${TEMPDIR}
	find "${TEMPDIR}" -type d -name ".git" -exec rm -rf {} \; || true

	find "${TEMPDIR}" -maxdepth 1 -mindepth 1 -print0 | xargs -0 -I '{}' mv '{}' .

	rmdir ${TEMPDIR}
}

Add_debian_version ()
{
	echo
	echo "################################################################################"
	echo "git: adding debian version"
	echo "################################################################################"

	# FIXME
	git-debian-add
}

Create_downstream_branch ()
{
	echo
	echo "################################################################################"
	echo "git: create ${DOWNSTREAM_BRANCH} branch"
	echo "################################################################################"

	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" = "debian" ] && ! git branch | grep -qs ${DOWNSTREAM_BRANCH}
	then
		git checkout -b ${DOWNSTREAM_BRANCH}
	fi
}

Update_maintainer_field ()
{
	if [ -e debian/templates/control.source.in ]
	then
		CONTROL="debian/templates/control.source.in"
	elif [ -e debian/control.in ]
	then
		CONTROL="debian/control.in"
	else
		CONTROL="debian/control"
	fi

	if ! grep -qs "^XSBC-Original-Maintainer: ${MAINTAINER}$" "${CONTROL}"
	then
		echo
		echo "################################################################################"
		echo "git: updating maintainer field"
		echo "################################################################################"

		if grep -qs "^Uploaders:" "${CONTROL}"
		then
			# sorting maintainer and uploaders fields
			ORIGINAL_MAINTAINER="$(grep '^Maintainer:' ${CONTROL})"

			# remove original maintainer field
			TEMPFILE="$(mktemp -p .)"
			grep -v "^Maintainer:" "${CONTROL}" > "${TEMPFILE}"
			rm -f "${CONTROL}"
			mv "${TEMPFILE}" "${CONTROL}"

			# readd original maintainer field
			sed -i	-e "s|^\(Uploaders.*\)$|${ORIGINAL_MAINTAINER}\n\1|" \
			"${CONTROL}"
		fi

		sed -i	-e "s|^Maintainer:|Maintainer: ${MAINTAINER}\nXSBC-Original-Maintainer:|" "${CONTROL}"

		git commit -a -s -S -m "Updating maintainer field."
	fi

	if [ -n "$(grep -s -r "^Maintainer:" debian | grep -v ^debian\/control)" ]
	then
		echo "WARNING: additional 'Maintainer:' fields outside debian/control{,.in} found!"
	fi
}

Update_uploaders_field ()
{
	if [ -e debian/templates/control.source.in ]
	then
		CONTROL="debian/templates/control.source.in"
	elif [ -e debian/control.in ]
	then
		CONTROL="debian/control.in"
	else
		CONTROL="debian/control"
	fi

	# FIXME: single uploader on one line, no multi-line
	if ! grep -qs "^XSBC-Uploaders: ${UPLOADERS}$" "${CONTROL}"
	then
		echo
		echo "################################################################################"
		echo "git: updating uploaders field"
		echo "################################################################################"

		sed -i	-e "s|^\(Maintainer:.*$\)|\1\nXSBC-Uploaders: ${UPLOADERS}|" \
			-e "s|^Uploaders:|XSBC-Original-Uploaders:|" \
		"${CONTROL}"

		git commit -a -s -S -m "Updating uploaders field."
	fi

	if [ -n "$(grep -s -r "^Uploaders:" debian | grep -v ^debian\/control)" ]
	then
		echo "WARNING: additional 'Uploaders:' fields outside debian/control{,.in} found!"
	fi
}

Update_bugs_field ()
{
	if [ -e debian/templates/control.source.in ]
	then
		CONTROL="debian/templates/control.source.in"
	elif [ -e debian/control.in ]
	then
		CONTROL="debian/control.in"
	else
		CONTROL="debian/control"
	fi

	if ! grep -qs "^Bugs: ${BUGS}$" "${CONTROL}"
	then
		echo
		echo "################################################################################"
		echo "git: updating bugs field"
		echo "################################################################################"

		# remove original bugs fields
		TEMPFILE="$(mktemp -p .)"
		grep -v "^Bugs:" "${CONTROL}" > "${TEMPFILE}"
		rm -f "${CONTROL}"
		mv "${TEMPFILE}" "${CONTROL}"

		# add new bugs field
		if grep -qs "^XSBC-Original-Uploaders:" "${CONTROL}"
		then
			NEXT_FIELD_AFTER_UPLOADERS="$(grep -v '^ ' ${CONTROL} | grep -v '^	' | grep -A1 -m1 '^XSBC-Original-Uploaders:' | tail -n1 | awk -F: '{ print $1 }')"
		else
			NEXT_FIELD_AFTER_UPLOADERS="$(grep -v '^ ' ${CONTROL} | grep -v '^	' | grep -A1 -m1 '^XSBC-Original-Maintainer:' | tail -n1 | awk -F: '{ print $1 }')"
		fi

		case "${NEXT_FIELD_AFTER_UPLOADERS}" in
			Package:*)
				LAST_LINE_OF_SOURCE_BLOCK="$(grep -B2 -m1 '^Package:' ${CONTROL} | head -n1)"

				sed -i	-e "s|\(^${LAST_LINE_OF_SOURCE_BLOCK}$\)|\1\nBugs: ${BUGS}|" \
				"${CONTROL}"
				;;

			*)
				sed -i	-e "s|^\(${NEXT_FIELD_AFTER_UPLOADERS}\)|Bugs: ${BUGS}\n\1|" \
				"${CONTROL}"
				;;
		esac

		git commit -a -s -S -m "Updating bugs field."

		if [ $(grep -c "^Bugs:" ${CONTROL}) -gt 1 ]
		then
			echo "${RED}WARNING:${NORMAL} duplicates of 'Bugs:' fields in debian/control{,.in} found!"
		fi
	fi
}

Update_vcs_fields ()
{
	if [ -e debian/templates/control.source.in ]
	then
		CONTROL="debian/templates/control.source.in"
	elif [ -e debian/control.in ]
	then
		CONTROL="debian/control.in"
	else
		CONTROL="debian/control"
	fi

	if ! grep -qs "^Vcs-Browser: ${VCS_BROWSER}/${PACKAGE}$" "${CONTROL}" && \
	   ! grep -qs "^Vcs-Git: ${VCS_BROWSER}/${PACKAGE}$" "${CONTROL}"
	then
		echo
		echo "################################################################################"
		echo "git: updating vcs fields"
		echo "################################################################################"

		sed -i	-e 's|^Vcs-browser|Vcs-Browser|' \
			-e 's|^Vcs-git|Vcs-Git|' \
		"${CONTROL}"

		# replace original vcs fields
		sed -i	-e 's|^Vcs-|XSBC-Original-Vcs-|g' \
		"${CONTROL}"

		if ! grep -qs "^XSBC-Original-Vcs-" "${CONTROL}"
		then
			# no vcs in original file
			LAST_LINE_OF_SOURCE_BLOCK="$(grep -B2 -m1 '^Package:' ${CONTROL} | head -n1)"

			sed -i	-e "s|\(^${LAST_LINE_OF_SOURCE_BLOCK}.*$\)|\1\nVcs-Browser: ${VCS_BROWSER}/${PACKAGE}\nVcs-Git: ${VCS_GIT}/${PACKAGE}|" \
			"${CONTROL}"
		else
			# vcs fields in original file
			ORIGINAL_VCS_BROWSER="$(grep '^XSBC-Original-Vcs-Browser' ${CONTROL})"

			TEMPFILE="$(mktemp -p . -u)"
			grep -v "^XSBC-Original-Vcs-Browser" "${CONTROL}" > "${TEMPFILE}"
			rm -f "${CONTROL}"
			mv "${TEMPFILE}" "${CONTROL}"

			# FIXME: handle cases where only Vcs-Browser is there, but no Vcs-Git
			ORIGINAL_VCS_OTHER="$(grep '^XSBC-Original-Vcs' ${CONTROL})"
			sed -i	-e "s|^\(XSBC-Original-Vcs-.*\)$|Vcs-Browser: ${VCS_BROWSER}/${PACKAGE}\nVcs-Git: ${VCS_GIT}/${PACKAGE}\n${ORIGINAL_VCS_BROWSER}\n\1|" \
			"${CONTROL}"
		fi
	fi

	if [ -n "$(git status --porcelain)" ]
	then
		git commit -a -s -S -m "Updating vcs fields."
	fi
}

Update_source_format ()
{
	if ! grep -qs '^3.0 (quilt)' debian/source/format
	then
		echo
		echo "################################################################################"
		echo "git: updating source format"
		echo "################################################################################"

		mkdir -p debian/source
		echo "3.0 (quilt)" > debian/source/format

		git add .
		git commit -a -s -S -m "Updating source format."
	fi
}

echo "################################################################################"
echo "git: importing ${IMPORT}"
echo "################################################################################"

case "${IMPORT}" in
	*.debian.tar.*)
		git checkout debian

		rm -rf debian
		tar xf ${IMPORT_TEMPDIR}/*.debian.tar.*
		git-debian-add

		git checkout -

		rm -rf "${IMPORT_TEMPDIR}"
		exit 0
		;;
esac

Add_initial_commit

Create_upstream_branch
Switch_upstream_branch
Remove_everything
Unpack_upstream
Add_upstream_version

Create_debian_branch
Switch_debian_branch
Remove_everything
Unpack_debian
Add_debian_version

if [ -n "${IMPORT_TEMPDIR}" ]
then
	rm -rf "${IMPORT_TEMPDIR}"
fi

# progress-linux / bfh
if [ -n "${DOWNSTREAM_BRANCH}" ]
then
	Create_downstream_branch

	Update_maintainer_field
	Update_uploaders_field
	Update_bugs_field
	Update_vcs_fields
	Update_source_format

	if [ -e debian/control.in ] || [ -e debian/templates/control.source.in ] || [ -e debian/control.md5sum ]
	then
		echo "################################################################################"
		echo "${RED}WARNING:${NORMAL} debian/control.in exists,"
		echo "likely the debian files need to be regenerated (manually)."
		echo "################################################################################"
	fi

	if [ "$(grep -rs '^Maintainer:' debian | wc -l )" -gt 1 ]
	then
		echo "################################################################################"
		echo "${RED}WARNING:${NORMAL} more than one Maintainer field exists,"
		echo "likely further debian files need to be changed (manually)."
		echo "################################################################################"
	fi
fi