#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"

setupenvironment
unset APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE
configarchitecture 'native'
configdpkgnoopchroot

buildingpkg() {
	local PKG="$1"
	shift
	setupsimplenativepackage "$PKG" 'native' '1' 'unstable' "$@"
	BUILDDIR="incoming/${PKG}-1"
	echo '#!/bin/sh
apt-key list >/dev/null' > "${BUILDDIR}/debian/postinst"
	buildpackage "$BUILDDIR" 'unstable' 'main' 'native'
	rm -rf "$BUILDDIR"
}
buildingpkg 'aptkeyuser-nodepends' 'Depends: unrelated'
buildingpkg 'aptkeyuser-depends' 'Depends: gnupg'

insertinstalledpackage 'unrelated' 'native' '1'
insertinstalledpackage 'gnupg' 'native' '1'
testdpkgnotinstalled 'aptkeyuser-depends' 'aptkeyuser-nodepends'

testsuccess apt install ./incoming/aptkeyuser-depends_*.changes -y
cp rootdir/tmp/testsuccess.output apt.output
testdpkginstalled 'aptkeyuser-depends'
testfailure grep '^Warning: This will BREAK' apt.output
testsuccess grep '^Warning: apt-key' apt.output

testsuccess apt install --with-source ./incoming/aptkeyuser-nodepends_*.changes aptkeyuser-nodepends -y
cp rootdir/tmp/testsuccess.output apt.output
testdpkginstalled 'aptkeyuser-nodepends'
testsuccess grep '^Warning: This will BREAK' apt.output
testsuccess grep '^Warning: apt-key' apt.output

testsuccess aptkey list
cp rootdir/tmp/testsuccess.output aptkey.list
testsuccess grep '^Warning: apt-key' aptkey.list