summaryrefslogtreecommitdiffstats
path: root/makerelease.lib
blob: dbcbe55db4d8539e375bbd47068c90bb96aaf2c2 (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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
#
# Release script library
#
# Copyright (c) 2012-2022 Michael Buesch <m@bues.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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#


default_hook_pre_checkout()
{
	true
}

default_hook_post_checkout()
{
	cd "$1"
	find "$1" \( \
		\( -name 'makerelease*' \) -o \
		\( -name '.git*' \) \
	\) -print0 | xargs -0 rm -r
}


default_hook_pre_relocate_checkout()
{
	true
}

default_hook_post_relocate_checkout()
{
	true
}

default_hook_pre_documentation()
{
	true
}

default_hook_post_documentation()
{
	true
}

default_hook_pre_archives()
{
	true
}

default_hook_post_archives()
{
	true
}

default_hook_pre_doc_archives()
{
	true
}

default_hook_doc_archives()
{
	info "By default no documentation archives are created."
	info "Please override hook_doc_archives."
}

default_hook_post_doc_archives()
{
	true
}

default_hook_testbuild()
{
	cd "$1"
	if ! echo "$conf_testbuild" | grep -q no-configure &&\
	   [ -x ./configure ]; then
		./configure
	fi
	if ! echo "$conf_testbuild" | grep -q no-cmake &&\
	   [ -r ./CMakeLists.txt ]; then
		cmake .
	fi
	if ! echo "$conf_testbuild" | grep -q no-make &&\
	   [ -r ./GNUmakefile -o -r ./makefile -o -r ./Makefile ]; then
		make
	fi
	if ! echo "$conf_testbuild" | grep -q no-setuppy &&\
	   [ -x ./setup.py ]; then
		./setup.py --no-user-cfg build
	fi
	if [ -r ./Cargo.toml ]; then
		if ! echo "$conf_testbuild" | grep -q no-cargo-build; then
			info "Running cargo DEBUG build"
			do_cargo build
			info "Running cargo RELEASE build"
			do_cargo build --release
		fi
		if ! echo "$conf_testbuild" | grep -q no-cargo-examples; then
			info "Running cargo EXAMPLES build"
			do_cargo build --examples
			info "Running cargo EXAMPLES RELEASE build"
			do_cargo build --examples --release
		fi
		if ! echo "$conf_testbuild" | grep -q no-cargo-clippy; then
			info "Running cargo CLIPPY"
			do_cargo clippy -- --deny warnings
			if ! echo "$conf_testbuild" | grep -q no-cargo-tests; then
				info "Running cargo CLIPPY on tests"
				do_cargo clippy --tests -- --deny warnings
			fi
			if ! echo "$conf_testbuild" | grep -q no-cargo-examples; then
				info "Running cargo CLIPPY on examples"
				do_cargo clippy --examples -- --deny warnings
			fi
		fi
		if ! echo "$conf_testbuild" | grep -q no-cargo-audit; then
			info "Running cargo AUDIT"
			do_cargo audit --deny warnings
		fi
	fi
}

default_hook_regression_tests()
{
	cd "$1"
	if [ -r ./Cargo.toml ]; then
		do_cargo test
		do_cargo test --examples
	fi
}

default_hook_pre_archive_signatures()
{
	true
}

default_hook_post_archive_signatures()
{
	true
}

default_hook_pre_tag()
{
	true
}

default_hook_post_tag()
{
	true
}

default_hook_pre_move_files()
{
	true
}

default_hook_post_move_files()
{
	true
}

default_hook_version()
{
	cd "$1"
	if [ -r "./$conf_package/Cargo.toml" ]; then
		version="$(grep -Ee '^version\s+=\s+' "./$conf_package/Cargo.toml" | head -n1 | cut -d'"' -f2)"
	else
		die "ERROR: Must supply hook_version()"
	fi
}

default_hook_pre_upload()
{
	true
}

default_hook_post_upload()
{
	true
}

default_hook_pre_debian_packages()
{
	true
}

default_hook_post_debian_packages()
{
	true
}

# Assign default hooks to actual hooks
hook_pre_checkout()		{ default_hook_pre_checkout "$@"; }
hook_post_checkout()		{ default_hook_post_checkout "$@"; }
hook_pre_relocate_checkout()	{ default_hook_pre_relocate_checkout "$@"; }
hook_post_relocate_checkout()	{ default_hook_post_relocate_checkout "$@"; }
hook_pre_documentation()	{ default_hook_pre_documentation "$@"; }
hook_post_documentation()	{ default_hook_post_documentation "$@"; }
hook_pre_archives()		{ default_hook_pre_archives "$@"; }
hook_post_archives()		{ default_hook_post_archives "$@"; }
hook_pre_doc_archives()		{ default_hook_pre_doc_archives "$@"; }
hook_doc_archives()		{ default_hook_doc_archives "$@"; }
hook_post_doc_archives()	{ default_hook_post_doc_archives "$@"; }
hook_testbuild()		{ default_hook_testbuild "$@"; }
hook_regression_tests()		{ default_hook_regression_tests "$@"; }
hook_pre_archive_signatures()	{ default_hook_pre_archive_signatures "$@"; }
hook_post_archive_signatures()	{ default_hook_post_archive_signatures "$@"; }
hook_pre_tag()			{ default_hook_pre_tag "$@"; }
hook_post_tag()			{ default_hook_post_tag "$@"; }
hook_pre_move_files()		{ default_hook_pre_move_files "$@"; }
hook_post_move_files()		{ default_hook_post_move_files "$@"; }
hook_get_version()		{ default_hook_version "$@"; }
hook_pre_upload()		{ default_hook_pre_upload "$@"; }
hook_post_upload()		{ default_hook_post_upload "$@"; }
hook_pre_debian_packages()	{ default_hook_pre_debian_packages "$@"; }
hook_post_debian_packages()	{ default_hook_post_debian_packages "$@"; }

cleanup()
{
	[ -d "$tmpdir" ] && {
		if [ $opt_keeptmp -eq 0 ]; then
			rm -rf "$tmpdir"
		else
			info "Keeping temporary directory '$tmpdir' in place."
		fi
	}
}

# $1=code
abort()
{
	cleanup
	exit $1
}

# $*=message
die()
{
	echo "$*"
	abort 1
}

terminating_signal()
{
	die "Terminating signal received"
}

# $*=message
info()
{
	echo "--- $*"
}

# $*=message
warn()
{
	echo "--- WARNING: $*" >&2
}

is_dry_run()
{
	[ $opt_dryrun -ne 0 ]
}

dry_run_prefix()
{
	is_dry_run && echo -n "echo dry-run " || true
}

# $1=program_name, $2+=program_args
dry_run()
{
	$(dry_run_prefix) "$@"
}

# $1=program_name
have_program()
{
	which "$1" >/dev/null 2>&1
}

# $1=program_name, ($2=description)
assert_program()
{
	local bin="$1"
	local desc="$2"
	[ -n "$desc" ] || desc="$bin"
	have_program "$bin" || die "$bin not found. Please install $desc."
}

# $1=path_to_git_repo
git_main_branch()
{
	if GIT_DIR="$1" git branch | grep -qE '^\*? ?main$'; then
		echo "main"
	else
		echo "master"
	fi
}

# $1=hook_name, $2+=hook_parameters
execute_hook()
{
	local hook_name="hook_$1"
	local oldpwd="$(pwd)"
	shift
	set -e
	eval $hook_name "$@"
	set +e
	[ "$(pwd)" != "$oldpwd" ] && {
		cd "$oldpwd" || die "execute_hook: Failed to switch back to old PWD '$oldpwd'"
	}
}

maketemp()
{
	local suffix="$1"

	mktemp --suffix="$suffix" --tmpdir="$tmpdir" "makerelease-tmpfile.XXXXXXXX"
}

detect_repos_type()
{
	[ -z "$repos_type" -a -d "$srcdir/.git" ] && repos_type=git
	[ -z "$repos_type" ] && repos_type=none
	case "$repos_type" in
		none|git) ;; # ok
		*) die "Invalid \$repos_type=$repos_type" ;;
	esac
}

do_cargo()
{
	local command="$1"
	shift

	local opt_package=
	if [ "$command" != "audit" ]; then
		if [ -n "$conf_package" ]; then
			local opt_package="--package $conf_package"
		fi
	fi

	info cargo "$command" $opt_package "$@"
	cargo "$command" $opt_package "$@"
}

make_checkout()
{
	checkout_dir="$tmpdir/$project-checkout"
	mkdir -p "$checkout_dir" || die "Failed to make checkout dir"
	execute_hook pre_checkout "$checkout_dir"
	case "$repos_type" in
	none)
		info "Copying source tree"
		cp -r "$srcdir" "$checkout_dir" || \
			die "Failed to copy source tree"
		;;
	git)
		info "Creating git checkout"
		assert_program git
		local branch="$(git_main_branch "$srcdir/.git")"
		[ -n "$opt_ref" ] && branch="$opt_ref"
		export GIT_DIR="$checkout_dir/.git"
		git clone --recursive --shared --no-checkout \
			"$srcdir/.git" "$checkout_dir" || \
			die "Failed to clone git repository"
		cd "$checkout_dir" || die "Internal error: cd"
		git checkout -b "__tmp_makerelease-$branch" "$branch" || \
			die "git checkout failed (1)"
		git checkout -f || \
			die "git checkout failed (2)"
		if [ -f "$checkout_dir/.gitmodules" ]; then
			git submodule update --init --recursive || \
				die "git submodule update failed."
		fi
		;;
	*)
		die "checkout: Unknown repos_type"
		;;
	esac
	execute_hook post_checkout "$checkout_dir"
}

detect_versioning()
{
	version=
	release_name=
	execute_hook get_version "$checkout_dir/$srcsubdir"
	[ -n "$(echo "$version" | tr -d '.[:blank:]')" ] ||\
		die "\$version not set correctly in hook_get_version()"
	version="${version}${opt_extraversion}"
	[ -n "$release_name" ] || release_name="$project-$version"
}

relocate_checkout()
{
	execute_hook pre_relocate_checkout "$tmpdir" "$checkout_dir"
	local new_checkout_dir="$tmpdir/$release_name"
	mv "$checkout_dir/$srcsubdir" "$new_checkout_dir" || \
		die "Failed to relocate checkout"
	rm -rf "$checkout_dir"
	checkout_dir="$new_checkout_dir"
	execute_hook post_relocate_checkout "$tmpdir" "$checkout_dir"
}

make_debian_packages()
{
	[ $opt_nodebian -eq 0 ] || return
	[ $opt_nobuild -eq 0 ] || return
	[ -d "$checkout_dir/debian" ] || return

	info "Creating Debian packages"
	have_program debuild || {
		warn "No 'debuild' available. Skipping Debian build."
		return
	}
	grep -qe quilt "$checkout_dir/debian/source/format" &&\
		die "Debian package format 'quilt' is not supported."

	local ver_without_suffix="$(printf '%s' "$version" | grep -oEe '[^\-]+' | head -n1)"
	grep -qEe "${project}"'\s*\(\s*'"${ver_without_suffix}" \
		"$checkout_dir/debian/changelog" ||\
		die "Debian changelog does not contain the version $version."

	local debuild_dir="${checkout_dir}_debuild"
	cp -a "$checkout_dir" "$debuild_dir" ||\
		die "Failed to copy checkout_dir for debuild."

	execute_hook pre_debian_packages "$debuild_dir"

	cd "$debuild_dir" || die "Failed to cd to debuild_dir"

	local debuild_opts=
	if [ $opt_nosign -eq 0 -a -n "$DEB_SIGN_KEYID" ]; then
		info "(Signing Debian packages with key '$DEB_SIGN_KEYID', from DEB_SIGN_KEYID)"
		local debuild_opts="-k$DEB_SIGN_KEYID"
	elif [ $opt_nosign -eq 0 -a -n "$GPG_KEY_RELEASE" ]; then
		info "(Signing Debian packages with key '$GPG_KEY_RELEASE', from GPG_KEY_RELEASE)"
		local debuild_opts="-k$GPG_KEY_RELEASE"
	else
		info "(Creating unsigned Debian packages)"
		local debuild_opts="-uc -us"
	fi
	CFLAGS= CPPFLAGS= CXXFLAGS= LDFLAGS= debuild $debuild_opts ||\
		die "Failed to build debian package"

	local deb_archive_dir="$archive_dir/debian"
	mkdir -p "$deb_archive_dir" ||\
		die "Failed to create Debian archive directory"
	mv "$debuild_dir/../"*.deb "$deb_archive_dir/" ||\
		die "Failed to move .deb files to archive directory"
	mv "$debuild_dir/../"*.dsc "$deb_archive_dir/" ||\
		die "Failed to move .dsc files to archive directory"
	mv "$debuild_dir/../"*.changes "$deb_archive_dir/" ||\
		die "Failed to move .changes files to archive directory"

	execute_hook post_debian_packages "$debuild_dir" "$deb_archive_dir"
}

_gendoc_md_html()
{
	local md="$1"

	local docname="$(basename "$md" .md)"
	local dir="$(dirname "$md")"
	local html="$dir/$docname.html"
	local tmpfile="$(maketemp .md)"

	echo "Generating $docname.md -> $docname.html ..."
	sed -e 's|\.md)|.html)|g' "$md" > "$tmpfile" ||\
		die "Failed to update links during markdown -> html"
	pandoc -s -M "title=$docname" -o "$html" "$tmpfile" ||\
		die "Failed to convert markdown -> html"
}

_gendoc_rst_md()
{
	local rst="$1"

	local docname="$(basename "$rst" .rst)"
	local dir="$(dirname "$rst")"
	local md="$dir/$docname.md"
	local tmpfile="$(maketemp .rst)"

	echo "Generating $docname.rst -> $docname.md ..."
	sed -e 's|\.rst>`_|.md>`_|g' "$rst" > "$tmpfile" ||\
		die "Failed to update links during reStructuredText -> markdown"
	pandoc -s -M "title=$docname" -o "$md" "$tmpfile" ||\
		die "Failed to convert reStructuredText -> markdown"
}

_gendoc_rst_html()
{
	local rst="$1"

	local docname="$(basename "$rst" .rst)"
	local dir="$(dirname "$rst")"
	local html="$dir/$docname.html"
	local tmpfile="$(maketemp .rst)"

	echo "Generating $docname.rst -> $docname.html ..."
	sed -e 's|\.rst>`_|.html>`_|g' "$rst" > "$tmpfile" ||\
		die "Failed to update links during reStructuredText -> html"
	pandoc -s -M "title=$docname" -o "$html" "$tmpfile" ||\
		die "Failed to convert reStructuredText -> html"
}

make_documentation()
{
	[ $opt_nodoc -eq 0 ] || return

	info "Creating documentation"

	assert_program pandoc "pandoc converter"

	execute_hook pre_documentation "$checkout_dir"

	local old_IFS="$IFS"
	IFS='
'

	# Build markdown -> html
	for file in $(find "$checkout_dir" -name '*.md'); do
		_gendoc_md_html "$file"
	done

	# Build reStructuredText -> markdown
	for file in $(find "$checkout_dir" -name '*.rst'); do
		_gendoc_rst_md "$file"
	done

	# Build reStructuredText -> html
	for file in $(find "$checkout_dir" -name '*.rst'); do
		_gendoc_rst_html "$file"
	done

	IFS="$old_IFS"

	execute_hook post_documentation "$checkout_dir"
}

make_archives()
{
	archive_dir="$tmpdir/$project-archives"
	mkdir -p "$archive_dir" || die "Failed to create archive directory"

	setup_py_targets=

	info "Creating archives"

	execute_hook pre_archives "$archive_dir" "$checkout_dir"

	for artype in $opt_archives; do
		local archive=
		local compressor=
		case "$artype" in
		tar)
			archive="$release_name.tar"
			;;
		tar.bz2)
			compressor="bzip2 -9"
			archive="$release_name.tar.bz2"
			;;
		tar.gz)
			compressor="gzip -9"
			archive="$release_name.tar.gz"
			;;
		tar.xz)
			compressor="xz -9"
			archive="$release_name.tar.xz"
			;;
		tar.zst)
			compressor="zstd -T0 -19"
			archive="$release_name.tar.zst"
			;;
		zip)
			archive="$release_name.zip"
			;;
		7z)
			archive="$release_name.7z"
			;;
		py-*)
			local target="$(echo "$artype" | sed -e 's/py-//' | tr '-' '_')"
			setup_py_targets="$setup_py_targets $target"
			continue # Python archives are handled later
			;;
		*)
			die "Internal error: archive type: $artype"
			;;
		esac

		info "Creating $archive"
		cd "$tmpdir" || die "Internal error: cd"
		case "$artype" in
		zip)
			zip -9 -r "$archive" "$release_name" || \
				die "Failed to create ZIP archive"
			;;
		7z)
			"$SEVENZIP" -mx=9 a "$archive" "$release_name" || \
				die "Failed to create 7-ZIP archive"
			;;
		tar)
			tar \
				--numeric-owner --owner=0 --group=0 \
				--mtime='1970-01-01 00:00Z' \
				--sort=name \
				-c "$release_name" \
				> "$archive" || \
				die "Failed to create tarball"
			;;
		tar.*)
			tar \
				--numeric-owner --owner=0 --group=0 \
				--mtime='1970-01-01 00:00Z' \
				--sort=name \
				-c "$release_name" \
				| $compressor \
				> "$archive" || \
				die "Failed to create tarball"
			;;
		*)
			die "Internal error: Archive type"
			;;
		esac
		mv "$archive" "$archive_dir"/ || die "Failed to move archive"
	done

	# Handle Python build targets
	[ -f "./setup.py" ] && [ $opt_upload -ne 0 ] && setup_py_targets="$setup_py_targets sdist_gz"
	if [ -n "$setup_py_targets" ]; then
		cd "$checkout_dir" || die "Internal error: cd"
		[ -x "./setup.py" ] ||\
			die "Used Python archive target, but no executable setup.py found"
		rm -rf ./dist/ || die "Failed to delete ./dist/"
		mkdir "$archive_dir/python" || die "Failed to create Python archive subdir"
		local have_bdist_wininst=0
		local have_sdist=0
		local have_sdist_bz2=0
		local have_sdist_xz=0
		local have_sdist_zip=0
		for target in $setup_py_targets; do
			info "Building Python $target-package"
			local opts=
			local setup_target="$target"
			local doit=0
			if [ "$target" = "bdist_wininst" ]; then
				[ $have_bdist_wininst -ne 0 ] && continue
				local have_bdist_wininst=1
				local opts="--plat-name win32"
				local doit=1
			elif [ "$target" = "sdist" -o "$target" = "sdist_gz" ]; then
				[ $have_sdist -ne 0 ] && continue
				local have_sdist=1
				local setup_target="sdist"
				local opts="--formats=gztar --owner=root --group=root"
				local doit=1
			elif [ "$target" = "sdist_bz2" ]; then
				[ $have_sdist_bz2 -ne 0 ] && continue
				local have_sdist_bz2=1
				local setup_target="sdist"
				local opts="--formats=bztar --owner=root --group=root"
				local doit=1
			elif [ "$target" = "sdist_xz" ]; then
				[ $have_sdist_xz -ne 0 ] && continue
				local have_sdist_xz=1
				local setup_target="sdist"
				local opts="--formats=xztar --owner=root --group=root"
				local doit=1
			elif [ "$target" = "sdist_zip" ]; then
				[ $have_sdist_zip -ne 0 ] && continue
				local have_sdist_zip=1
				local setup_target="sdist"
				local opts="--formats=zip"
				local doit=1
			fi
			if [ $doit -ne 0 ]; then
				./setup.py --no-user-cfg "$setup_target" $opts ||\
					die "Failed to build Python archive"
			fi
		done
		mv ./dist/* "$archive_dir/python/" || die "Failed to move Python archives"
		rmdir ./dist/ || die "Failed to delete ./dist/"
	fi

	execute_hook post_archives "$archive_dir" "$checkout_dir"
}

make_documentation_archives()
{
	[ $opt_nodoc -eq 0 ] || return

	info "Packing documentation archives"

	execute_hook pre_doc_archives "$archive_dir" "$checkout_dir"
	execute_hook doc_archives "$archive_dir" "$checkout_dir"
	execute_hook post_doc_archives "$archive_dir" "$checkout_dir"
}

make_upload()
{
	[ $opt_upload -eq 0 ] && return

	if [ -f "$checkout_dir/setup.py" ]; then
		# This is is a Python package.
		[ -n "$setup_py_targets" ] ||\
			die "PyPi-upload requested, but no Python packages have been built."
		info "Uploading $setup_py_targets archives to PyPi"
		assert_program twine

		local py_archive_dir="$archive_dir/python"
		execute_hook pre_upload "$checkout_dir" "$py_archive_dir"
		twine check "$py_archive_dir"/*.tar.gz* ||\
			die "twine check failed"
		dry_run twine upload --repository pypi "$py_archive_dir"/*.tar.gz* ||\
			die "twine upload failed"
		execute_hook post_upload "$checkout_dir" "$py_archive_dir"
	elif [ -f "$checkout_dir/Cargo.toml" ]; then
		# This is a rust/cargo package.
		info "Uploading archive to crates.io"
		assert_program cargo

		cd "$checkout_dir" || die "Internal error: cd"
		execute_hook pre_upload "$checkout_dir"
		local dry=
		is_dry_run && dry="--dry-run"
		do_cargo publish $dry || die "cargo publish failed."
		execute_hook post_upload "$checkout_dir"
	else
		die "upload: Unknown package format."
	fi
}

make_testbuild()
{
	[ $opt_nobuild -eq 0 ] || return
	info "Running test build"
	execute_hook testbuild "$checkout_dir"
}

make_regression_tests()
{
	[ $opt_nobuild -eq 0 ] || return
	[ $opt_notests -eq 0 ] || return
	info "Running regression tests"
	execute_hook regression_tests "$checkout_dir"
}

# $1=directory
gpg_sign_dir_recursive()
{
	local dir="$1"

	# Don't sign files in the debian directory
	[ "$(basename "$dir")" = "debian" ] && return

	local path=
	for path in "$dir"/*; do
		[ -d "$path" ] && {
			gpg_sign_dir_recursive "$path"
			continue
		}
		[ -r "$path" ] || die "Sign: File not readable: $path"

		local filename="$(basename "$path")"
		local signature="$filename.asc"

		info "Creating signature $signature"
		cd "$dir" || die "Internal error: cd"
		local gpg=gpg
		have_program gpg2 && gpg=gpg2
		assert_program $gpg "GNU Privacy Guard"
		local gpg_opts=
		[ -n "$GPG_KEY_RELEASE" ] && gpg_opts="--default-key $GPG_KEY_RELEASE"
		$gpg $gpg_opts -ab "./$filename" || die "Failed to sign $filename"
	done
}

make_archive_signatures()
{
	[ $opt_nosign -ne 0 ] && return

	execute_hook pre_archive_signatures "$archive_dir"
	gpg_sign_dir_recursive "$archive_dir"
	execute_hook post_archive_signatures "$archive_dir"
}

make_tag()
{
	[ $conf_notag -ne 0 ] && return
	[ $opt_notag -ne 0 ] && return
	[ "$repos_type" = "none" ] && return
	info "Tagging repository"
	tag_name="$project-$version"
	tag_message="$project-$version release"
	execute_hook pre_tag "$srcdir"
	cd "$srcdir" || die "Internal error: cd"
	case "$repos_type" in
	git)
		assert_program git
		local opts=
		[ $opt_nosign -eq 0 ] && {
			[ -n "$GPG_KEY_RELEASE" ] && {
				opts="$opts -u $GPG_KEY_RELEASE"
			} || {
				opts="$opts -s" # default key
			}
		} || {
			opts="$opts -a" # unsigned
		}
		local branch="$(git_main_branch "$srcdir/.git")"
		[ -n "$opt_ref" ] && branch="$opt_ref"
		export GIT_DIR="$srcdir/.git"
		dry_run git tag $opts -m "$tag_message" "$tag_name" "$branch"
		;;
	*)
		die "tagging: Unknown repos_type"
		;;
	esac
	execute_hook post_tag "$srcdir"
}

move_files()
{
	local target_dir="$srcdir/$srcsubdir/release-archives"

	info "Moving files"
	execute_hook pre_move_files "$archive_dir" "$srcdir/$srcsubdir"

	dry_run mkdir -p "$target_dir" || die "Failed to create target directory"
	dry_run cp -r "$archive_dir"/* "$target_dir"/ || \
		die "Failed to copy tarballs"

	execute_hook post_move_files "$archive_dir" "$srcdir/$srcsubdir"

	local dry=
	is_dry_run && dry=" (DRY RUN)"
	echo
	info "Built $project release ${version}${dry}"
}

help()
{
	echo "Usage: $0 [OPTIONS]"
	echo
	echo "Environment:"
	echo "  MAKERELEASE_LIB       May be set to makerelease.lib"
	echo
	echo "Options:"
	echo "  -y|--dry-run          Do not make persistent changes"
	echo "  -t|--no-tag           Do not create the repository tag"
	echo "  -s|--no-sign          Do not sign"
	echo "  -b|--no-build         Do not run build. (Implies -T)"
	echo "  -T|--no-tests         Do not run regression tests"
	echo "  -q|--quick            Quick run. Equivalent to -t -s -b -T -d"
	echo "  -D|--no-doc           Do not build documentation"
	echo "  -r|--ref REF          Checkout version control reference REF"
	echo "  -a|--archives TYPE,TYPE,...   Archive type list. Default: $default_archives"
	echo "                        Possible types: tar, tar.bz2, tar.gz, tar.xz, tar.zst, zip, 7z"
	echo "                        For Python programs: py-sdist,"
	echo "                                             py-sdist-gz, py-sdist-bz2, py-sdist-xz, py-dist-zip,"
	echo "                                             py-bdist, py-bdist-wininst,"
	echo "                                             py-bdist-dumb, py-bdist-rpm"
	echo "  -d|--no-debian        Do not build Debian packages."
	echo "  -U|--upload           Upload the archives to PyPi or crates.io"
	echo "  -O|--upload-only      Run the bare minimum to upload only."
	echo "                        Equivalent to: -U -t -b -T -d"
	echo "  -V|--extraversion XX  Append XX to version string"
	echo "  -K|--keep-tmp         Do not delete temporary files"
	echo "  -h|--help             Show this help text"
}

# This is the main function called from the main script.
# Parameters to this functions must be the main script arguments.
makerelease()
{
	# Backwards compatibility for old default_compress option
	[ -n "$default_compress" -a -z "$default_archives" ] && default_archives="$default_compress"

	[ -n "$project" ] || die "\$project variable not set"
	[ -n "$srcdir" ] || die "\$srcdir variable not set"
	[ -n "$srcsubdir" ] || srcsubdir=""
	[ -n "$conf_package" ] || conf_package=""
	[ -n "$tmp_basedir" ] || tmp_basedir="/tmp"
	[ -n "$default_archives" ] || default_archives="tar.xz"
	[ -n "$conf_testbuild" ] || conf_testbuild=""
	[ -n "$conf_notag" ] || conf_notag=0

	trap terminating_signal TERM INT
	trap cleanup EXIT

	# Reproducible builds.
	export PYTHONHASHSEED=1
	export SOURCE_DATE_EPOCH=0

	local template="makerelease-$project.XXXXXXXX"
	tmpdir="$(mktemp -d --tmpdir="$tmp_basedir" "$template")"
	[ -d "$tmpdir" ] || die "Failed to create temporary directory"

	if have_program 7z; then
		SEVENZIP=7z
	elif have_program 7zz; then
		SEVENZIP=7zz
	else
		die "Program 7-Zip not found"
	fi

	opt_dryrun=0
	opt_notag=0
	opt_nosign=0
	opt_nobuild=0
	opt_nodebian=0
	opt_notests=0
	opt_nodoc=0
	opt_ref=
	opt_archives="$default_archives"
	opt_upload=0
	opt_extraversion=
	opt_keeptmp=0

	while [ $# -ge 1 ]; do
		case "$1" in
		--help|-h)
			help "$@"
			abort 0
			;;
		-y|--dry-run)
			opt_dryrun=1
			;;
		-t|--no-tag)
			opt_notag=1
			;;
		-s|--no-sign)
			opt_nosign=1
			;;
		-b|--no-build)
			opt_nobuild=1
			;;
		-T|--no-tests)
			opt_notests=1
			;;
		-q|--quick)
			opt_notag=1
			opt_nosign=1
			opt_nobuild=1
			opt_nodebian=1
			opt_notests=1
			;;
		-D|--no-doc)
			opt_nodoc=1
			;;
		-r|--ref)
			shift
			opt_ref=$1
			[ -n "$opt_ref" ] || die "Invalid --ref"
			;;
		-a|--archives)
			shift
			opt_archives="$1"
			;;
		-d|--no-debian)
			opt_nodebian=1
			;;
		-U|--upload)
			opt_upload=1
			;;
		-O|--upload-only)
			opt_notag=1
			opt_nobuild=1
			opt_nodebian=1
			opt_notests=1
			opt_upload=1
			;;
		-V|--extraversion)
			shift
			opt_extraversion="$1"
			[ -n "$opt_extraversion" ] || die "Invalid --extraversion"
			;;
		-K|--keep-tmp)
			opt_keeptmp=1
			;;
		*)
			die "Invalid option: $1"
			;;
		esac
		shift
	done

	opt_archives="$(echo "$opt_archives" | tr ',' ' ')"
	for artype in $opt_archives; do
		case "$artype" in
			tar|tar.bz2|tar.gz|tar.xz|tar.zst|zip|7z|py-sdist|py-sdist-gz|py-sdist-bz2|py-sdist-xz|py-sdist-zip|py-bdist|py-bdist-wininst|py-bdist-dump|py-bdist-rpm) ;; # ok
			*) die "Invalid archiving method: $artype" ;;
		esac
	done
	[ -n "$(echo "$opt_archives" | tr -d '[:blank:]')" ] ||\
		die "No archiving method selected"

	detect_repos_type
	make_checkout
	detect_versioning
	relocate_checkout
	make_documentation
	make_archives
	make_documentation_archives
	make_testbuild
	make_regression_tests
	make_debian_packages
	make_archive_signatures
	make_tag
	make_upload
	move_files
	cleanup
}

# vim: syntax=sh
bues.ch cgit interface