#!/bin/sh

. "${DPT__SCRIPTS:-/usr/share/pkg-perl-tools}/lib/dpt-lib.sh"

set -e
set -u

not_is_dirty() {
	git diff --quiet
}

# defaults for configuration variable(s)

DPT_FIXUP_DOUBLECHECK=${DPT_FIXUP_DOUBLECHECK:-''}

# check at the beginning
not_is_dirty || die "Your Git repo is dirty. Please fix."

# reformat before actual changes
header "Reformat debian/control"
cme modify dpkg-control -save
not_is_dirty || git commit -a -m 'Reformat debian/control with cme' -m 'Gbp-Dch: ignore' || true

# autopkgtest
header "Testsuite: autopkgtest-pkg-perl"
if is_pkg_perl && ! grep -qF "Testsuite: autopkgtest-pkg-perl" debian/control; then
	cme modify dpkg-control source 'Testsuite="autopkgtest-pkg-perl"'
	not_is_dirty || git commit -a -m 'Mark package as autopkgtest-able.' || true
fi

# Standards-Version
header "Standards-Version"
if ! grep -qF "Standards-Version: 4.7.2" debian/control; then
	cme modify dpkg-control source 'Standards-Version="4.7.2"'
	not_is_dirty || git commit -a -m 'Declare compliance with Debian Policy 4.7.2.' || true
fi

# R³
header "Rules-Requires-Root: no"
if grep -E -q "^Rules-Requires-Root:\s*no" debian/control; then
	sed -i -e '/^Rules-Requires-Root:\s*no/ d' debian/control
	not_is_dirty || git commit -a -m 'Remove «Rules-Requires-Root: no», which is the current default.' || true
fi

## TODO: comment in once lintian-brush is fixed
## Priority: optional
#header "Priority: optional"
#if grep -E -q "^Priority:\s*optional" debian/control; then
#	sed -i -e '/^Priority:\s*optional/ d' debian/control
#	not_is_dirty || git commit -a -m 'Remove «Priority: optional», which is the current default.' || true
#fi

#debhelper
header "debhelper"
if ! grep -E -q "debhelper-compat.*(13|14)" debian/control; then
	rm -f debian/compat
	perl -pi -e 's/debhelper\s*?(,|$)/debhelper-compat (= 13)$1/;'  \
		-e 's/debhelper[^)]+/debhelper-compat (= 13/' debian/control
	not_is_dirty || git commit -a -m 'Bump debhelper-compat to 13.' || true
fi

# other fixes, mostly versioned dependencies.
if [ -n "$DPT_FIXUP_DOUBLECHECK" ]; then
	header "cme fix dpkg-control" $(colored "MAGENTA" "(!)")
	cme fix dpkg-control
	not_is_dirty || git commit -a -m 'Drop unneeded version constraints from (build) dependencies.' || true
fi

# cross builds
if is_pkg_perl && grep -E -q "Architecture: (linux-)?any" debian/control; then
	grep -qF perl-xs-dev debian/control || cme modify dpkg-control 'source Build-Depends:.insort(perl-xs-dev)'
	if ! grep -qF perl:native debian/control; then
		cme modify dpkg-control 'source Build-Depends:=~s/^perl$/perl:native/'
		cme modify dpkg-control 'source Build-Depends:=~"s/^perl \(>/perl:native (>/"'
		grep -qF perl:native debian/control || cme modify dpkg-control 'source Build-Depends:.insort(perl:native)'
	fi
	not_is_dirty || git commit -a -m "Update Build-Depends for cross builds." || true
fi

# hardening
header "Hardening flags"
if grep -q "DEB_BUILD_MAINT_OPTIONS.*bindnow" debian/rules; then
	perl -pi -e 's/hardening=\+bindnow/hardening=+all/' debian/rules
	not_is_dirty || git commit -a -m "Update 'DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow' to '=+all'." || true
fi
if grep -qF "Architecture: any" debian/control && ! grep -Lq "DEB_BUILD_MAINT_OPTIONS.*hardening" debian/rules; then
	perl -pi -e 's/%:/export DEB_BUILD_MAINT_OPTIONS = hardening=+all\n\n%:/' debian/rules
	not_is_dirty || git commit -a -m "Enable all hardening flags." || true
fi

# <!nocheck>
if [ -n "$DPT_FIXUP_DOUBLECHECK" ]; then
	header "<!nocheck> annotations" $(colored "MAGENTA" "(!)")
	if is_pkg_perl; then
		PERL5LIB="$DPT__SCRIPTS/../lib" "$DPT__SCRIPTS/lib/guess-nocheck.pl" && \
		cme modify dpkg-control -save
		not_is_dirty || git commit -a -m 'Annotate test-only build dependencies with <!nocheck>.' || true
	fi
fi

# "3.0 (quilt)"
header "Source format"
if [ ! -f debian/source/format ]; then
	mkdir -p debian/source
	echo '3.0 (quilt)' > debian/source/format
	git add debian/source
	not_is_dirty || git commit -m 'Switch to source format "3.0 (quilt)".'
fi

# Module::Build
header "libmodule-build-perl"
if [ -f Build.PL ] && ! grep -q libmodule-build debian/control; then
	cme modify dpkg-control 'source Build-Depends:.insort(libmodule-build-perl)'
	not_is_dirty || git commit -a -m "Add explicit build dependency on libmodule-build-perl." || true
fi

# sort
header "Sort dependencies"
cme modify dpkg-control 'source Build-Depends:.sort Build-Depends-Indep:.sort Build-Conflicts:.sort'
cme modify dpkg-control "binary:~/./ Depends:.sort Recommends:.sort Suggests:.sort Breaks:.sort Replaces:.sort Conflicts:.sort Provides:.sort"
not_is_dirty || git commit -a -m '(Re-)sort dependencies' -m 'Gbp-Dch: ignore' || true

# d/watch
header "debian/watch"
if [ -f debian/watch ] && grep -qF 'version=3' debian/watch; then
	sed -i -e 's/version=3/version=4/' debian/watch
	perl -pi -e 's,/(\S+)-v.+$,/$1-v?\@ANY_VERSION\@\@ARCHIVE_EXT\@\$,g' debian/watch
	not_is_dirty || git commit -a -m 'debian/watch: use uscan version 4.' || true
fi

# d/copyright
header "Packaging copyright years"
cme run update-my-copyright-year --commit="Update years of packaging copyright." || true

# whitespace
header "Trailing whitespace"
for f in $(find debian -maxdepth 1 -type f) debian/source/* debian/upstream/* ; do
	[ ! -f "$f" ] || sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' -e 's@[[:space:]]*$@@g' "$f"
done
not_is_dirty || git commit -a -m 'Remove trailing whitespace from debian/*.' -m 'Gbp-Dch: ignore' || true

# lintian-brush
if [ -n "$DPT_FIXUP_DOUBLECHECK" ]; then
	header "lintian-brush" $(colored "MAGENTA" "(!)")
	if is_command lintian-brush; then
		[ -d .pc ] && ! quilt applied >/dev/null 2>&1 && rm -rf .pc || true
		lintian-brush --modern --no-update-changelog --opinionated --uncertain || true
	fi
fi

## TODO: remove once lintian-brush is fixed
# Priority: optional
header "Priority: optional"
if grep -E -q "^Priority:\s*optional" debian/control; then
	sed -i -e '/^Priority:\s*optional/ d' debian/control
	not_is_dirty || git commit -a -m 'Remove «Priority: optional», which is the current default.' || true
fi

# debputy
if [ -n "$DPT_FIXUP_DOUBLECHECK" ]; then
	header "debputy" $(colored "MAGENTA" "(!)")
	if is_command debputy; then
		debputy lint --auto-fix || true
		not_is_dirty || echo "Fixes by deputy lint."
	fi
fi

POD=<<'EOF'
=head1 NAME

dpt-fixup - apply some automatic fixes to packaging

=head1 SYNOPSIS

B<dpt fixup>

=head1 DESCRIPTION

B<dpt fixup> does some automatic cleanups and fixups for the packaging, with
a focus on but not limited to Debian Perl Group packages.  It is basically a
wrapper around L<cme(1)> and other tools.

=head1 CONFIGURATION

Some of the changes done by tools called by B<dpt fixup> need human
double-checking.  These fixes are only run if the C<DPT_FIXUP_DOUBLECHECK>
environment variable is set.

If you set this variable, please do double-check the changes made in the
steps denoted by '(!)' in the output.

See L<dpt-config(5)> for details.

=head1 COPYRIGHT & LICENSE

=over

=item Copyright 2022-2025 gregor herrmann L<gregoa@debian.org>

=back

This program is free software, licensed under the same terms as perl.

=cut
EOF
