#!/bin/sh
set -e

log_debug() {
	echo "Debug: piuparts exception for package $PIUPARTS_OBJECTS"
}

is_installed()
{
	local pkg="$1"
	dpkg-query -s "$pkg" >/dev/null 2>&1 || return 1
	local status="$(dpkg-query -W -f '${Status}' $pkg)"
	test "$status" != "unknown ok not-installed" || return 1
	test "$status" != "deinstall ok config-files" || return 1
	return 0
}

case ${PIUPARTS_OBJECTS%%=*} in
	apt-listbugs)		log_debug
				# when installed apt-listbugs is run on installations / upgrades
				# and will cause them to fail due to prompting
				# if packages being installed currently have RC bugs.
				# so disable it here.
				dpkg-divert --rename /usr/sbin/apt-listbugs

				ln -svf /bin/true /usr/sbin/apt-listbugs
				;;
esac

if is_installed dkms; then
	# remember for post_purge_exceptions
	touch /var/run/piuparts-stamp-dkms-installed
	# if dkms built a kernel module, there might be a
	# leftover 'sleep' process from progress reporting
	sleep 3
fi

for pgconf in /etc/postgresql/*/main/postgresql.conf
do
	test -f "$pgconf" && test -r "$pgconf" || continue
	port=$(awk '/^port/ { print $3 }' "$pgconf")
	if [ "-n" $port ] && [ "$port" != "5432" ]
	then
		echo "Non-default port in $pgconf"
	fi
done
