#!/bin/sh
set -e

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

setupenvironment
configarchitecture 'amd64' 'i386'

# regression test for #754904
testfailureequal 'E: Unsupported file /dev/null given on commandline' aptget install -qq /dev/null

# only consider .deb files
cat > foo.rpm <<EOF
I'm not a deb, I'm a teapot.
EOF
testfailureequal 'E: Unsupported file ./foo.rpm given on commandline' aptget install -qq ./foo.rpm

# and ensure we fail for invalid debs
mv foo.rpm foo.deb
testfailuremsg "E: Sub-process Popen returned an error code (2)
E: Encountered a section with no Package: header
E: Problem with MergeList ${TMPWORKINGDIRECTORY}/foo.deb
E: The package lists or status file could not be parsed or opened." aptget install ./foo.deb

buildsimplenativepackage 'foo' 'i386,amd64' '1.0'

testfailureequal "Reading package lists...
Building dependency tree...
Note, selecting 'foo:i386' instead of './incoming/foo_1.0_i386.deb'
Note, selecting 'foo' instead of './incoming/foo_1.0_amd64.deb'
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 foo:i386 : Conflicts: foo but 1.0 is to be installed
 foo : Conflicts: foo:i386 but 1.0 is to be installed
E: Unable to correct problems, you have held broken packages." aptget install ./incoming/foo_1.0_i386.deb ./incoming/foo_1.0_amd64.deb -s

testdpkgnotinstalled 'foo' 'foo:i386'
testsuccess aptget install ./incoming/foo_1.0_i386.deb -o Debug::pkgCacheGen=1
testdpkginstalled 'foo:i386'
testfailure aptget install incoming/foo_1.0_i386.deb -o Debug::pkgCacheGen=1
cd downloaded
testsuccess aptget install "$(readlink -f ../incoming/foo_1.0_i386.deb)" -o Debug::pkgCacheGen=1 -y --reinstall
testfailure grep 'is already the newest version' ../rootdir/tmp/testsuccess.output
testsuccess aptget install ../incoming/foo_1.0_i386.deb -o Debug::pkgCacheGen=1 -y --reinstall
testfailure grep 'is already the newest version' ../rootdir/tmp/testsuccess.output
cd ..

testsuccessequal "Reading package lists...
Building dependency tree...
Reading state information...
Note, selecting 'foo' instead of './incoming/foo_1.0_amd64.deb'
The following packages will be REMOVED:
  foo:i386
The following NEW packages will be installed:
  foo
0 upgraded, 1 newly installed, 1 to remove and 0 not upgraded.
Remv foo:i386 [1.0]
Inst foo (1.0 local-deb [amd64])
Conf foo (1.0 local-deb [amd64])" aptget install ./incoming/foo_1.0_amd64.deb -s

testsuccessequal 'Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.' aptget install --with-source ./incoming/foo_1.0_amd64.deb -s

testsuccessequal 'Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be REMOVED:
  foo:i386
The following NEW packages will be installed:
  foo
0 upgraded, 1 newly installed, 1 to remove and 0 not upgraded.
Remv foo:i386 [1.0]
Inst foo (1.0 local-deb [amd64])
Conf foo (1.0 local-deb [amd64])' aptget install --with-source ./incoming/foo_1.0_amd64.deb foo -s

# Check that installing the local deb works if it is not the candidate
echo "Package: foo
Pin: version 1.0
Pin-Priority: -1" > rootdir/etc/apt/preferences

testsuccessequal "Reading package lists...
Building dependency tree...
Reading state information...
Note, selecting 'foo' instead of './incoming/foo_1.0_amd64.deb'
The following packages will be REMOVED:
  foo:i386
The following NEW packages will be installed:
  foo
0 upgraded, 1 newly installed, 1 to remove and 0 not upgraded.
Remv foo:i386 [1.0]
Inst foo (1.0 local-deb [amd64])
Conf foo (1.0 local-deb [amd64])" aptget install ./incoming/foo_1.0_amd64.deb -s

createpkg() {
	local PKG="pkg-$1"
	mkdir -p ./incoming/$PKG/DEBIAN
	if [ -n "$2" ]; then
		echo -n "$2" >> ./incoming/$PKG/DEBIAN/control
	fi
	echo "Package: $PKG
Version: 0
Priority: extra
Maintainer: No Body <no@example.org>
Architecture: all
Depends: foo:i386
Description: test package" >> ./incoming/$PKG/DEBIAN/control
	if [ -n "$3" ]; then
		echo -n "$3" >> ./incoming/$PKG/DEBIAN/control
	fi
	testsuccess dpkg-deb --build ./incoming/$PKG/ ./incoming
	#dpkg-deb -I ./incoming/${PKG}_0_all.deb control
}
createpkg 'as-it-should-be'
createpkg 'leading-newline' '

'
createpkg 'trailing-newline' '' '
'

echo 'Package: /pkg-/
Pin: release a=experimental
Pin-Priority: 501' > rootdir/etc/apt/preferences.d/pinit

testsuccess aptget install ./incoming/pkg-as-it-should-be_0_all.deb
testsuccess aptget install "$(readlink -f ./incoming/pkg-leading-newline_0_all.deb)"
testsuccess aptget install ./incoming/pkg-trailing-newline_0_all.deb

testempty apt clean
if [ "$(id -u)" = '0' ]; then
	# see if permission dropping is checked before usage
	chmod 711 ./incoming
	testsuccess aptget install -y ./incoming/pkg-as-it-should-be_0_all.deb --reinstall
	testfailure grep 'is already the newest version' rootdir/tmp/testsuccess.output
	chmod 710 ./incoming
	testsuccesswithnotice aptget install -y ./incoming/pkg-as-it-should-be_0_all.deb --reinstall
	testfailure grep 'is already the newest version' rootdir/tmp/testsuccesswithnotice.output
	chmod 700 ./incoming
	testsuccesswithnotice aptget install -y ./incoming/pkg-as-it-should-be_0_all.deb --reinstall
	testfailure grep 'is already the newest version' rootdir/tmp/testsuccesswithnotice.output
	chmod 711 ./incoming
else
	testsuccess aptget install -y ./incoming/pkg-as-it-should-be_0_all.deb --reinstall
	testfailure grep 'is already the newest version' rootdir/tmp/testsuccess.output
fi

sed -i -e '/^Depends: foo/ d' rootdir/var/lib/dpkg/status
testsuccess aptget install -y ./incoming/pkg-as-it-should-be_0_all.deb
testfailure grep 'is already the newest version' rootdir/tmp/testsuccess.output
testsuccess apt purge -y pkg-as-it-should-be

echo "Package: pkg-as-it-should-be
Architecture: all
Version: 0
Installed-Size: 2903
Filename: incoming/pkg-as-it-should-be_0_all.deb
Size: $(stat -c %s incoming/pkg-as-it-should-be_0_all.deb)
SHA256: $(sha256sum incoming/pkg-as-it-should-be_0_all.deb | cut -d' ' -f 1)
" > Packages
testdpkgnotinstalled 'pkg-as-it-should-be'
testnopackage pkg-as-it-should-be
testsuccess apt install --with-source ./Packages pkg-as-it-should-be -s
testsuccess apt install --with-source ./Packages pkg-as-it-should-be --print-uris
testsuccess apt show --with-source ./Packages pkg-as-it-should-be
testequal 'Package: pkg-as-it-should-be' head -n1 rootdir/tmp/testsuccess.output
testsuccess apt install -y --with-source ./Packages pkg-as-it-should-be
testdpkginstalled 'pkg-as-it-should-be'
rm -f ./Packages

echo 'dpkg::install::recursive "true";
dpkg::install::recursive::force "true";
dpkg::install::recursive::minimum "0";' > rootdir/etc/apt/apt.conf.d/lowerminimum.conf
mv ./incoming/pkg-as-it-should-be_0_all.deb ./incoming/pkg-as-it-should-be_0_all.ddeb
testsuccess aptget install -y ./incoming/pkg-as-it-should-be_0_all.ddeb --reinstall
testfailure grep 'is already the newest version' rootdir/tmp/testsuccess.output
testsuccess apt purge -y pkg-as-it-should-be
testdpkgnotinstalled 'pkg-as-it-should-be'

mv ./incoming/pkg-as-it-should-be_0_all.ddeb ./incoming/pkg-as-it-should-be_0_all.foobar
echo "Package: pkg-as-it-should-be
Architecture: all
Version: 0
Installed-Size: 2903
Filename: incoming/pkg-as-it-should-be_0_all.foobar
Size: $(stat -c %s incoming/pkg-as-it-should-be_0_all.foobar)
SHA256: $(sha256sum incoming/pkg-as-it-should-be_0_all.foobar | cut -d' ' -f 1)
" | gzip > Packages.gz
testsuccess apt install --with-source ./Packages.gz pkg-as-it-should-be -s
testsuccess apt install --with-source ./Packages.gz pkg-as-it-should-be --print-uris
testsuccess apt show --with-source ./Packages.gz pkg-as-it-should-be
testequal 'Package: pkg-as-it-should-be' head -n1 rootdir/tmp/testsuccess.output
testsuccess apt install -y --with-source ./Packages.gz pkg-as-it-should-be
testdpkginstalled 'pkg-as-it-should-be'