#!/bin/bash

# Copyright: 2010-2024, gregor herrmann <gregoa@debian.org>
#            2014-2015, Salvatore Bonaccorso <carnil@debian.org>
#            2015-2023, intrigeri <intrigeri@debian.org>
#            2019, Clément Hermann <nodens@nodens.org>
# Licensed under the same terms as Perl (Artistic | GPL-1+)

# to be run from source directory

# environment:
# - BUILDDIR, defaults to ".."
# - ARCH, defaults to arch of .changes file

# initialize begin
for f in ~/.dpt.conf ~/.config/dpt.conf; do
	if [ -r "$f" ]; then
		. "$f"
	fi
done
# shellcheck source=../scripts/lib/dpt-lib.sh
OOT=1 . "${DPT__SCRIPTS:-/usr/share/pkg-perl-tools}/lib/dpt-lib.sh"
# initialize end

# functions #

notify() {
	local command=$1
	local urgency=${2:-normal}
	if [ -x /usr/bin/notify-send ] && [ -n "$DISPLAY" ] && [ -n "$command" ] ; then
		notify-send -u "$urgency" -t 5000 "$command finished" "$(dpkg-parsechangelog | grep -E '^(Source|Version)')"
	fi
}

# main #

BUILDDIR=${BUILDDIR:-".."}
if [ ! -d "$BUILDDIR" ] ; then
	die "Build directory \"$BUILDDIR\" does not exist.\n" \
	            "  Maybe you forgot to set the BUILDDIR variable?\n" \
		    "  Exiting ..."
fi

rm -rf "$BUILDDIR"/*obsolete*
rm -f  "$BUILDDIR"/*.dsc.asc

PACKAGE=$(dpkg-parsechangelog -S Source)
VERSION=$(dpkg-parsechangelog -S Version | perl -pe 's|^\d+:||')

if [ -z "$ARCH" ] ; then
	# Since we use a ls sorting option, implementing this without ls|grep
	# would require quite some efforts.
	# shellcheck disable=SC2010
	CHANGES=$(ls -1rt "$BUILDDIR/${PACKAGE}_${VERSION}"_*.changes \
		      | grep -v -E '_source\.changes$' \
		      | tail -n 1)
	ARCH=$(basename "${CHANGES##"$BUILDDIR/${PACKAGE}_${VERSION}"_}" .changes)
else
	CHANGES="$BUILDDIR/${PACKAGE}_${VERSION}_${ARCH}.changes"
fi

BUILD="$BUILDDIR/${PACKAGE}_${VERSION}_${ARCH}.build"
DSC="$BUILDDIR/${PACKAGE}_${VERSION}.dsc"

if [ ! -r "$CHANGES" ] || ! [ -r "$DSC" ] ; then
	die "Can't read \"$CHANGES\" or \"$DSC\".\n" \
	            "  Is BUILDDIR set correctly: \"$BUILDDIR\"?\n" \
	            "  Exiting ..."
fi

if is_command lintian; then
	header "lintian"
	#lintian -i -I --show-overrides --pedantic --color auto --no-tag-display-limit "$CHANGES"
	#~/.lintianrc, lintian 2.5.1;
	#--output-width since 2.105.0
	LINTIANVERSION=$(dpkg-query -f '${Version}\n' -W lintian)
	if dpkg --compare-versions "$LINTIANVERSION" ge 2.105.0 ; then
		lintian --output-width 80 "$CHANGES"
	else
		lintian "$CHANGES"
	fi
fi

if [ -r "$BUILD" ] && ! grep -E -q "^Architecture: all$" "$DSC" ; then
	if is_command blhc; then
		header "blhc"
		blhc --buildd "$BUILD"
	fi
fi

if is_command duck; then
	header "duck"
	timeout 30s duck
fi
echo

if promptyesno "debc?"; then
	debc -a "$ARCH" "$CHANGES" | less
fi
echo

if promptyesno "piuparts?"; then
	PIUPARTSLOG="${CHANGES%.changes}_piuparts.log"
	CHROOT=
	# default is --no-minimize; add --minimize for pbuilder/cowbuilder, not for schroot
	[ -f /var/cache/pbuilder/base.tar.gz ] && CHROOT="--pbuilder --minimize"
	[ -f /var/cache/pbuilder/base.tgz ] && CHROOT="--pbuilder --minimize"
	[ -d /var/cache/pbuilder/base.cow ] && CHROOT="--existing-chroot /var/cache/pbuilder/base.cow --minimize"
	# no eatmydata in chroot; proxy not resolvable
	if [ -x /usr/bin/schroot ] && schroot -l | grep -q default; then
		CHROOT="--schroot default --no-eatmydata --proxy=DIRECT"
	fi
	CHANGESPIU="${CHANGES%.changes}_piuparts.changes"
	if grep -qE "^Binary: .+-dbgsym.*$" "$CHANGES" ; then
		grep -vE '\-dbgsym_.+\.deb$' "$CHANGES" > "$CHANGESPIU"
	else
		cp "$CHANGES" "$CHANGESPIU"
	fi
	echo
	# since 1.5.0 /usr/sbin/piuparts -> /usr/bin/piuparts
	PIUPARTSVERSION=$(dpkg-query -f '${Version}\n' -W piuparts)
	if dpkg --compare-versions "$PIUPARTSVERSION" ge 1.5.0 ; then
		PIUPARTSBIN=/usr/bin/piuparts
	else
		PIUPARTSBIN=/usr/sbin/piuparts
	fi
	# log-level: error < info < debug <  dump
	# Deliberately word splitting CHROOT
	# shellcheck disable=SC2086
	sudo $PIUPARTSBIN \
		$CHROOT -t "${TMPDIR:-/tmp}" \
		--warn-on-others --warn-on-debsums-errors --skip-logrotatefiles-test \
		--log-level=debug --log-file="$PIUPARTSLOG" \
		"$CHANGESPIU"
	rm "$CHANGESPIU"
	notify piuparts
fi
echo

if promptyesno "reprotest?"; then
	REPROTESTLOG="${CHANGES%.changes}_reprotest.log"
	if [ -x /usr/bin/schroot ] && schroot -l | grep -q default; then
		REPROTEST_VIRT_SERVER=schroot
		REPROTEST_VIRT_SERVER_ARGS=default
	fi
	if [ -n "$REPROTEST_VIRT_SERVER" ]; then
		REPROTESTPARAMS=("--variations=+all,-user_group,-domain_host,-fileordering,-time" "--vary=environment.variables+=SHELL=/bin/dash" "--verbosity 1" . )
		echo
		# Deliberately word splitting variables
		# shellcheck disable=SC2086
		# Deliberately word splitting array
		# shellcheck disable=SC2068
		env -u TMPDIR CCACHE_DIR=/tmp/.ccache reprotest ${REPROTESTPARAMS[@]} -- $REPROTEST_VIRT_SERVER $REPROTEST_VIRT_SERVER_ARGS 2>&1 | \
			tee $REPROTESTLOG
		notify reprotest
	else
		warn "Could not find reprotest virtualization server."
	fi
fi
echo

if grep -q 'Testsuite: autopkgtest' debian/control || [ -f debian/tests/control ]; then
	if promptyesno "autopkgtest?"; then
		AUTOPKGTESTLOG="${CHANGES%.changes}_autopkgtest.log"
		if ! [ -v AUTOPKGTEST_VIRT_SERVER ]; then
			if [ -x /usr/bin/schroot ] && schroot -l | grep -q default; then
				AUTOPKGTEST_VIRT_SERVER=schroot
				AUTOPKGTEST_VIRT_SERVER_ARGS=default
			fi
		fi
		if [ -n "$AUTOPKGTEST_VIRT_SERVER" ]; then
			echo
			# Deliberately word splitting variables
			# shellcheck disable=SC2086
			autopkgtest "$CHANGES" --log-file="$AUTOPKGTESTLOG" --shell-fail $AUTOPKGTEST_ARGS -- \
				$AUTOPKGTEST_VIRT_SERVER $AUTOPKGTEST_VIRT_SERVER_ARGS
			SUCCESS=$?
			case $SUCCESS in
				0)
					URGENCY=normal
					;;
				2)
					warn "autopkgtest: at least one test was skipped (or at least one flaky test failed)."
					URGENCY=normal
					;;
				8)
					warn "autopkgtest: no tests in this package, or all non-superficial tests were skipped."
					URGENCY=normal
					;;
				*)
					colored "RED" "autopkgtest: failed ($SUCCESS)."
					URGENCY=critical
					;;
			esac
			exec 0</dev/tty
			notify autopkgtest $URGENCY
		else
			warn "Could find no autopkgtest virtualization server."
		fi
		# TODO:
		# since 3.7 we get colours but only without --log-file=. and on STDERR.
		# See https://git-tails.immerda.ch/tails/tree/run_test_suite?h=testing
		# for an example of how to get colours on the terminal,
		# while still creating a log file without control chars.
	fi
fi
echo

PKGVER=$(echo "${PACKAGE}-${VERSION}" | perl -pe 's;^(.+)(?:-.+)$;$1;')
AUTOPATCH="debian/patches/debian-changes-${VERSION}"
DIFFGZ="$BUILDDIR/${PACKAGE}_${VERSION}.diff.gz"
DEBGZ="$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.gz"
DEBBZ2="$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.bz2"
DEBXZ="$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.xz"
[ -e "$DEBGZ" ] || DEBGZ="$DEBBZ2"
[ -e "$DEBGZ" ] || DEBGZ="$DEBXZ"

FORMAT=$(grep ^Format: "$DSC" | cut -f2 -d" ")

if [ "$FORMAT" = "1.0" ] && [ -e "$DIFFGZ" ] && zgrep -E "^\+\+\+ " "$DIFFGZ" | grep -q -v "$PKGVER/debian"; then
	echo
	warn "Changes in .diff.gz outside debian/!"
	if promptyesno "View .diff.gz? y/N"; then
		filterdiff -z -x "$PKGVER/debian/*" "$DIFFGZ" | colordiff | /usr/bin/less -R
	fi
	echo
	info "Maybe: mkdir -p debian/patches && diff2patches $DIFFGZ"
	echo
elif [ "$FORMAT" = "3.0" ] && [ -e "$DEBGZ" ] && tar tvf "$DEBGZ" | grep -q "$AUTOPATCH"; then
	echo
	warn "Automatically created patch in .debian.tar.gz/.bz2/.xz!"
	if promptyesno "View .debian.tar.gz/.bz2/.xz? y/N"; then
		tar xf "$DEBGZ" "$AUTOPATCH" --to-stdout | colordiff | /usr/bin/less -R
	fi
	echo
	info "Maybe: tar xf $DEBGZ $AUTOPATCH"
	echo
else
	if promptyesno "Ready for upload?"; then
		echo
		cat "$CHANGES"
		echo
		BUILDINFO="${CHANGES%%.changes}".buildinfo
		if grep -q "\.u\?deb" "$CHANGES"; then
			if promptyesno "Strip binary .(u)debs?"; then
				SOURCEONLYCHANGES="${CHANGES%%_"$ARCH".changes}_sourceonly.changes"
				mergechanges --source "$CHANGES" "$CHANGES" > "$SOURCEONLYCHANGES"
				if [ -f "$BUILDINFO" ]; then
					if [ -x /usr/bin/changestool ]; then
						SOURCEONLYBUILDINFO="${BUILDINFO%%_"$ARCH".buildinfo}_sourceonly.buildinfo"
						cp "$BUILDINFO" "$SOURCEONLYBUILDINFO"
						changestool "$SOURCEONLYCHANGES" add "$SOURCEONLYBUILDINFO"
					else
						echo
						warn "Can't find changestool (from reprepro package)."
					fi
				fi
				echo
				cat "$SOURCEONLYCHANGES"
				CHANGES="$SOURCEONLYCHANGES"
			else
				echo
				# Fallback to question if only want to strip arch:any (u)debs
				if promptyesno "Strip arch:any .(u)debs"; then
					ALLONLYCHANGES="${CHANGES%%_"$ARCH".changes}_allonly.changes"
					mergechanges --indep "$CHANGES" "$CHANGES" > "$ALLONLYCHANGES"
					if [ -f "$BUILDINFO" ]; then
						if [ -x /usr/bin/changestool ]; then
							ALLONLYBUILDINFO="${BUILDINFO%%_"$ARCH".buildinfo}_allonly.buildinfo"
							cp "$BUILDINFO" "$ALLONLYBUILDINFO"
							changestool "$ALLONLYCHANGES" add "$ALLONLYBUILDINFO"
						else
							echo
							warn "Can't find changestool (from reprepro package)."
						fi
					fi
					echo
					cat "$ALLONLYCHANGES"
					CHANGES="$ALLONLYCHANGES"
				fi
			fi
			echo
		fi
		debsign "$CHANGES"
		echo
		echo dput "$(readlink -f "$CHANGES")"
	fi
	echo
fi
