#!/bin/sh

# Copyright: 2012-2024 gregor herrmann <gregoa@debian.org>
#
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

APTVERSION=$(dpkg-query -f '${Version}\n' -W apt)
pkgavail() {
	for p in $(apt-get indextargets --format '$(FILENAME)' "Created-By: Packages") ; do
		/usr/lib/apt/apt-helper cat-file $p | grep -q $1 && return 0
	done
	return 1
}

EAT=
if pkgavail eatmydata ; then
	EAT=eatmydata
fi

FCNTLLOCK=
if pkgavail libfile-fcntllock-perl ; then
	FCNTLLOCK=libfile-fcntllock-perl
fi

UBUNTU=
if dpkg-vendor --derives-from Ubuntu ; then
	UBUNTU="ubuntu-keyring"
fi

RASPBIAN=
if dpkg-vendor --derives-from Raspbian ; then
	RASPBIAN="raspbian-archive-keyring"
fi

apt-get -y install debfoster
debfoster -o MaxPriority=required -o UseRecommends=no \
	-o InstallCmd="apt-get install -y" -o RemoveCmd="apt-get --purge remove -y --allow-remove-essential" \
	-f -n -v \
	apt aptitude debfoster cowdancer build-essential ccache $EAT $UBUNTU $RASPBIAN $FCNTLLOCK
rm -rf /var/lib/debfoster/keepers
apt-get -y purge debfoster     
apt-get -y autoremove

dpkg --list | grep '^rc\b' | awk '{ print $2 }' | xargs apt-get -y purge
