#!/bin/bash
set -eu

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

setupenvironment

buildpackage 'simple-package'
buildpackage 'conflicting-package-one'

aptgetinstallpackage 'simple-package'
aptgetinstallpackage 'conflicting-package-one'

testpkginstalled 'simple-package'
testpkginstalled 'conflicting-package-one'

state_with_gpgpubkey=()
manual_with_gpgpubkey=()
for (( i = 0; i < APT_TEST_GPGPUBKEY; ++i )); do
    id="$(tr [:upper:] [:lower:] <"$APT_TEST_INTERMEDIATES"/gpg-pubkey/"$i"/id)"
    id32bit="$(echo -n "$id" | tail -c 8)"
    time="$(<"$APT_TEST_INTERMEDIATES"/gpg-pubkey/"$i"/time)"
    printf -v time_hex '%x' "$time"
    time_hex="${time_hex#0x}"
    evrdt_re="$id32bit-$time_hex@$time"
    state_with_gpgpubkey=(
	"${state_with_gpgpubkey[@]}"
	"gpg-pubkey#$evrdt_re manual"
    )
    manual_with_gpgpubkey=(
	"${manual_with_gpgpubkey[@]}"
	"gpg-pubkey#$evrdt_re"
    )
done
readonly state_with_gpgpubkey manual_with_gpgpubkey

testequal 'Reading Package Lists...
Building Dependency Tree...
conflicting-package-one manual
simple-package manual' aptmark showstate simple-package conflicting-package-one

testpartsexhaust "$INTRO_re
conflicting-package-one manual" \
		 "${state_with_gpgpubkey[@]}" \
		 "simple-package manual" \
		 -- aptmark showstate

testequal 'Reading Package Lists...
Building Dependency Tree...' aptmark showauto

testpartsexhaust "$INTRO_re
conflicting-package-one" \
		 "${manual_with_gpgpubkey[@]}" \
		 "simple-package" \
		 -- aptmark showmanual

testsuccess aptmark auto simple-package

testequal 'Reading Package Lists...
Building Dependency Tree...
conflicting-package-one manual
simple-package auto' aptmark showstate simple-package conflicting-package-one

testequal 'Reading Package Lists...
Building Dependency Tree...
conflicting-package-one manual' aptmark showstate conflicting-package-one

testpartsexhaust "$INTRO_re
conflicting-package-one manual" \
		 "${state_with_gpgpubkey[@]}" \
		 "simple-package auto" \
		 -- aptmark showstate

testequal 'Reading Package Lists...
Building Dependency Tree...
simple-package' aptmark showauto

testpartsexhaust "$INTRO_re
conflicting-package-one" \
		 "${manual_with_gpgpubkey[@]}" \
		 -- aptmark showmanual
