#!/bin/sh
set -e

TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"
setupenvironment
configarchitecture 'amd64'

buildsimplenativepackage 'pkg1' 'amd64' '1' 'stable'
buildsimplenativepackage 'pkg2' 'amd64' '1' 'stable'


setupaptarchive

cathistory() {
    sed rootdir/var/log/apt/history.log -re 's/^(Commandline|Start-Date|End-Date):.*/\1: dummy/'
}


testsuccess aptget install pkg1

testequal "
Start-Date: dummy
Commandline: dummy
Install: pkg1:amd64 (1)
End-Date: dummy" cathistory

testsuccess aptget install pkg2 --comment="A test comment"

testequal "
Start-Date: dummy
Commandline: dummy
Install: pkg1:amd64 (1)
End-Date: dummy

Start-Date: dummy
Commandline: dummy
Comment: A test comment
Install: pkg2:amd64 (1)
End-Date: dummy" cathistory

normalizedinfo() {
    apt history-info 0 | sed -E \
    -e 's/^( *-?Start time *: ).*/\1dummy/' \
    -e 's/^( *-?End time *: ).*/\1dummy/' \
    -e 's|^( *-?Command line *: ).*/(.*)|\1\2|'
}

testsuccessequal "Transaction ID: 0
Start time: dummy
End time: dummy
Requested by: 
Command line: apt-get install pkg1
Packages changed:
    Install pkg1:amd64 (1)
" normalizedinfo
 
# replace aribtrary command line and date with "dummy".
normalizedlist() {
    apt history-list | sed -E 's/^([0-9]+)[[:space:]]+'\
'[^[:space:]]+[[:space:]]+'\
'[0-9]{4}-[0-9]{2}-[0-9]{2}[[:space:]]+'\
'[0-9]{2}:[0-9]{2}:[0-9]{2}/'\
'\1  dummy                    dummy               /'
}

testsuccessequal "ID Command line             Date and Time          Action    Changes                  

0  dummy                    dummy                  Install   1                        
1  dummy                    dummy                  Install   1                        " normalizedlist

msgmsg "Actionless group"
cat > rootdir/var/log/apt/history.log << EOF
Start-Date: dummy
Commandline: install nothing
End-Date: dummy
EOF

testsuccessequal "ID Command line             Date and Time          Action    Changes                  

0  install nothing          dummy                            0                        " normalizedlist

testsuccessequal "Transaction ID: 0
Start time: dummy
End time: dummy
Requested by: 
Command line: install nothing
Packages changed:" normalizedinfo
 
