#!/bin/sh
set -e

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

setupenvironment
configarchitecture 'amd64' 'i386'

buildsimplenativepackage 'testing' 'amd64' '0.1' 'stable'
buildsimplenativepackage 'testing' 'amd64' '0.8.15' 'stable'
buildsimplenativepackage 'testing2' 'amd64,i386' '0.8.15' 'stable'
setupaptarchive

# install native
exec 3> apt-progress.log
testsuccess aptget install testing=0.1 -y -o APT::Status-Fd=3
testfileequal './apt-progress.log' 'dlstatus:1:0.0000:Retrieving file 1 of 1
dlstatus:1:100.0000:Retrieving file 1 of 1
pmstatus:dpkg-exec:0.0000:Running dpkg
pmstatus:testing:0.0000:Preparing testing (amd64)
pmstatus:testing:20.0000:Unpacking testing (amd64)
pmstatus:testing:40.0000:Installing testing (amd64)
pmstatus:dpkg-exec:40.0000:Running dpkg
pmstatus:testing:40.0000:Preparing to configure testing (amd64)
pmstatus:testing:60.0000:Configuring testing (amd64)
pmstatus:testing:80.0000:Installed testing (amd64)'

# upgrade
exec 3> apt-progress.log
testsuccess aptget install testing=0.8.15 -y -o APT::Status-Fd=3
testfileequal './apt-progress.log' 'dlstatus:1:0.0000:Retrieving file 1 of 1
dlstatus:1:100.0000:Retrieving file 1 of 1
pmstatus:dpkg-exec:0.0000:Running dpkg
pmstatus:testing:0.0000:Preparing testing (amd64)
pmstatus:testing:20.0000:Unpacking testing (amd64)
pmstatus:testing:40.0000:Installing testing (amd64)
pmstatus:dpkg-exec:40.0000:Running dpkg
pmstatus:testing:40.0000:Preparing to configure testing (amd64)
pmstatus:testing:60.0000:Configuring testing (amd64)
pmstatus:testing:80.0000:Installed testing (amd64)'

# reinstall
exec 3> apt-progress.log
testsuccess aptget install testing=0.8.15 --reinstall -y -o APT::Status-Fd=3
testfileequal './apt-progress.log' 'dlstatus:1:0.0000:Retrieving file 1 of 1
dlstatus:1:100.0000:Retrieving file 1 of 1
pmstatus:dpkg-exec:0.0000:Running dpkg
pmstatus:testing:0.0000:Preparing testing (amd64)
pmstatus:testing:20.0000:Unpacking testing (amd64)
pmstatus:testing:40.0000:Installing testing (amd64)
pmstatus:dpkg-exec:40.0000:Running dpkg
pmstatus:testing:40.0000:Preparing to configure testing (amd64)
pmstatus:testing:60.0000:Configuring testing (amd64)
pmstatus:testing:80.0000:Installed testing (amd64)'

# and remove
exec 3> apt-progress.log
testsuccess aptget remove testing -y -o APT::Status-Fd=3
testfileequal './apt-progress.log' 'pmstatus:dpkg-exec:0.0000:Running dpkg
pmstatus:testing:0.0000:Preparing for removal of testing (amd64)
pmstatus:testing:33.3333:Removing testing (amd64)
pmstatus:testing:66.6667:Removing testing (amd64)
pmstatus:dpkg-exec:66.6667:Running dpkg'

# install non-native and ensure we get proper progress info
exec 3> apt-progress.log
testsuccess aptget install testing2:i386 -y -o APT::Status-Fd=3
testfileequal './apt-progress.log' 'dlstatus:1:0.0000:Retrieving file 1 of 1
dlstatus:1:100.0000:Retrieving file 1 of 1
pmstatus:dpkg-exec:0.0000:Running dpkg
pmstatus:testing2:0.0000:Preparing testing2 (i386)
pmstatus:testing2:20.0000:Unpacking testing2 (i386)
pmstatus:testing2:40.0000:Installing testing2 (i386)
pmstatus:dpkg-exec:40.0000:Running dpkg
pmstatus:testing2:40.0000:Preparing to configure testing2 (i386)
pmstatus:testing2:60.0000:Configuring testing2 (i386)
pmstatus:testing2:80.0000:Installed testing2 (i386)'

rm -f apt-progress*.log