#!/bin/sh
set -e

# Return a non-zero error code to have piuparts skip testing an
# uninstallable (or otherwise untestable) package.

# Packages in main don't qualify for "external services out of our
# control". They only qualify for "utterly broken" once on archive.d.o.
# There are very few exceptions to this rule:
# - usrmerge, which is one-way.
# - localepurge, which seems hard to undo without a complete --reinstall.

skip_test()
{
	echo "Skipping piuparts test: $*"
	exit 1
}

fail_test()
{
	echo "Failing piuparts test: $*"
	exit 2
}

skip_always()
{
	skip_test "$@"
}

skip_distupgrade()
{
	if [ "$PIUPARTS_TEST" = "distupgrade" ]; then
		skip_test "$@"
	fi
}


# * For uninstallable *broken* packages we usually run the normal
#   installation+purge tests (which will fail and get the package marked
#   as bugged) and only skip the distupgrade tests since there is no point
#   in upgrading something that could not be installed as a start.
#   Automatically marking the upgrade logs as bugged would also be more
#   difficult, since the bug is in some *old* version.
# * For packages requiring *external resources* not available in piuparts
#   we usually skip the failing initial install, too, since that is
#   *not a bug* in the package.


if [ "${PIUPARTS_DISTRIBUTION}" = "lenny" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		blootbot)
			skip_distupgrade "#502753: fails to install: prompts without using debconf"
			;;
		distributed-net)
			skip_distupgrade "#655922: noninteractive install: postinst loops infinitely"
			;;
		dnscache-run)
			skip_distupgrade "#664848: breaks dns resolution in chroot"
			;;
		libpils0|\
		libpils-dev|\
		libstonith0|\
		libstonith-dev)
			skip_distupgrade "#702055: transitional packages uninstallable in lenny"
			;;
		mhc)
			skip_distupgrade "#696231: fails to install: emacs-package-install failure"
			;;
		multex-bin)
			skip_distupgrade "#668791: fails to install: The format file of 'multex' is NOT built successfully."
			;;
		python-milter|\
		spf-milter-python)
			skip_always "python-milter contains virus signatures that may get blocked by the firewall"
			;;
		runit-run)
			skip_distupgrade "#542157: fails to install"
			;;
		sun-java5-doc|\
		sun-java6-doc)
			skip_distupgrade "#709967: fails to install: download location no longer available"
			;;
		trn)
			skip_distupgrade "fails to install: prompts without using debconf"
			;;
		ttf-root-installer)
			skip_distupgrade "#678807: fails to install: ftp:// download location disappeared"
			;;
	esac

fi

if [ "${PIUPARTS_DISTRIBUTION}" = "squeeze" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		distributed-net)
			skip_distupgrade "#655922: noninteractive install: postinst loops infinitely"
			;;
		firmware-b43-lpphy-installer)
			skip_distupgrade "#623703: download location no longer available"
			;;
		fossology|\
		fossology-agents|\
		fossology-agents-single|\
		fossology-scheduler|\
		fossology-web|\
		fossology-web-single)
			# removed after squeeze
			skip_distupgrade "#595593: fails to install"
			;;
		mini-buildd-bld|\
		mini-buildd-rep)
			skip_distupgrade "#632955, #656746: time limit exceeded during install"
			fail_test "#632955, #656746: time limit exceeded during install"
			;;
	esac

fi

if [ "${PIUPARTS_DISTRIBUTION}" = "wheezy" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		firmware-b43-lpphy-installer)
			skip_distupgrade "#623703: download location no longer available"
			;;
		geoip-database-contrib)
			skip_distupgrade "#885441: download location no longer available"
			;;
		nautilus-dropbox)
			skip_distupgrade "#770402: downloads awfully slow, hangs during installation"
			;;
	esac

fi

if [ "${PIUPARTS_DISTRIBUTION}" = "jessie" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		geoip-database-contrib)
			skip_distupgrade "#885441: download location no longer available"
			;;
		steam)
			skip_distupgrade "#772598: cannot preseed licence acceptance"
			;;
		susv4)
			skip_distupgrade "#900882: download location no longer available"
			;;
	esac

fi

if [ "${PIUPARTS_DISTRIBUTION}" = "stretch" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		geoip-database-contrib)
			skip_distupgrade "#885441: download location no longer available"
			;;
	esac

fi

case ${PIUPARTS_OBJECTS%%=*}@${PIUPARTS_DISTRIBUTION} in
	acgvision-agent@squeeze)
		# removed after squeeze
		skip_distupgrade "#688231: modifies conffiles: /etc/acgvision/agent.ini"
		;;
	arden@jessie)
		if [ "$(dpkg --print-architecture)" = "i386" ]; then
			skip_distupgrade "arch:all package no longer installable in stretch/i386"
		fi
		;;
	bugzilla3@lenny)
		# fixed in squeeze, removed after squeeze, excluded from squeeze-lts
		skip_distupgrade "was excluded from squeeze-lts"
		;;
	chef-solr@wheezy|\
	chef-expander@wheezy|\
	chef-server-api@wheezy)
		skip_distupgrade "#681573: chef-solr: fails to install if policy-rc.d forbids to start services"
		;;
	console-setup-freebsd@*)
		if [ "$(uname)" = "Linux" ]; then
			skip_always "non-Linux package"
		fi
		;;
	cpushare@wheezy)
		# removed after wheezy
		skip_distupgrade "#689885: Ships a folder in /var/run or /var/lock"
		;;
	dict-stardic@lenny)
		# removed after lenny
		skip_distupgrade "#523770: must use invoke-rc.d"
		;;
	dms-core@jessie|\
	dms@jessie|\
	dms-dr@jessie|\
	dms-wsgi@jessie)
		# fixed after jessie, not in stable after jessie
		skip_distupgrade '#852519: fails to install: NZST FATAL: configuration file "/etc/postgresql/9.4/dms/postgresql.conf" contains errors'
		;;
	docbookwiki@lenny)
		# removed after lenny
		skip_distupgrade "unconditionally uses mysql-client during purge"
		;;
	drupal6-trans-ru@squeeze)
		# removed after squeeze
		skip_distupgrade "#689252: installs files in /usr/share/drupal6/profiles which is a symlink in the drupal package"
		;;
	dynare-matlab@squeeze)
		skip_distupgrade "requires a matlab installation"
		;;
	dynare-matlab@*|\
	matlab-gdf@*|\
	matlab-support@*)
		skip_always "requires a matlab installation"
		;;
	firebird2.0-classic@lenny)
		# removed after lenny
		skip_distupgrade "#574220: package purge (after dependencies removal) fails"
		;;
	gclcvs@squeeze)
		# removed after squeeze
		skip_distupgrade "modifies conffiles: /etc/default/gclcvs"
		;;
	geordi@lenny)
		# removed after lenny
		skip_distupgrade "modifies conffiles: /etc/geordi/irc-config"
		;;
	ldap2dns@lenny)
		skip_distupgrade "#671671: fails to install"
		;;
	libpam-rsa@lenny)
		# removed after lenny
		skip_distupgrade "modifies conffiles: /etc/security/pam_rsa.conf"
		;;
	localepurge@*)
		skip_distupgrade "requires a full --reinstall to restore disappeared files"
		;;
	mailreader@lenny)
		# removed after lenny
		skip_distupgrade "modifies conffiles: /var/lib/mailreader/ads/debian/ad.cfg, /etc/mailreader/{main,server}.cfg"
		;;
	nessusd@lenny|\
	harden-remoteaudit@lenny)
		# removed after lenny
		skip_distupgrade "nessusd: modifies conffiles: /etc/nessus/nessusd.conf"
		;;
	openvas-server@squeeze|\
	harden-remoteaudit@squeeze|\
	openvas-server-dev@squeeze)
		# removed after squeeze
		skip_distupgrade "#688379: openvas-server: modifies conffiles: /etc/openvas/openvasd.conf"
		;;
	pdbv@lenny)
		# removed after lenny
		skip_distupgrade "modifies conffiles: /etc/pdbv/pdbvrc"
		;;
	poker-web@squeeze)
		# removed after squeeze
		skip_distupgrade "#698533: modifies a shipped file: /usr/share/poker-web/currency_one.php"
		;;
	python-poker-stats@squeeze)
		# removed after squeeze
		skip_distupgrade "#608646: cronjob exits with error after package removal"
		;;
	sdic-eijiro@lenny)
		skip_distupgrade "requires a dictionary CD"
		;;
	sdic-eijiro@*)
		skip_always "requires a dictionary CD"
		;;
	usrmerge@*)
		skip_always "performs piuparts-unfriendy filesystem layout changes"
		;;
	xul-ext-compactheader@wheezy)
		if [ "$(apt-cache show xul-ext-compactheader | sed -n '/^Version:/s/^Version: //p')" = "2.0.5-1" ]; then
			skip_distupgrade "#837387: uninstallable in wheezy with icedove (>= 1:45)"
		fi
		;;
	xul-ext-compactheader@jessie)
		if [ "$(apt-cache show xul-ext-compactheader | sed -n '/^Version:/s/^Version: //p')" = "2.0.8-1" ]; then
			skip_distupgrade "#837387: uninstallable in jessie with icedove (>= 1:45)"
		fi
		;;
esac
