#!/bin/bash
set -e
set -u

export DEBIAN_FRONTEND=noninteractive

exit=0

eval "$(apt-config shell ARCHITECTURE APT::Architecture)"

testsuccess() {
    echo "Testing for success of $* ..."
    if ! "$@" &>out.log </dev/null; then
        echo "E: Failed $*"
        exit=1
    fi
}

testfailurematches() {
    message="$1"
    shift
    echo "Testing for failure of $* ..."
    if "$@" &>out.log </dev/null; then
        echo "E: Succeeded: $*"
        exit=1
    fi
    echo "Testing whether output contains '$message' ..."
    if ! grep -qE "$message" out.log; then
        echo "E: Does not match $message"
        cat out.log
        exit=1
    fi
}

testsuccess apt-get install -y --allow-remove-essential boot-managed-by-snapd shim-signed-

testfailurematches "boot-managed-by-snapd : Conflicts: .*" apt-get install -y shim-signed
testfailurematches "^Cannot install [^ ]* on system as boot is managed by snapd.$" apt-get install -y shim-signed

testfailurematches "boot-managed-by-snapd : Conflicts: .*" apt-get install -y shim
testfailurematches "^Cannot install [^ ]* on system as boot is managed by snapd.$" apt-get install -y shim

testfailurematches "boot-managed-by-snapd : Conflicts: nullboot" apt-get install -y nullboot
testfailurematches "^Cannot install nullboot on system as boot is managed by snapd.$" apt-get install -y nullboot

testfailurematches "boot-managed-by-snapd : Conflicts: linux-firmware" apt-get install -y linux-firmware
testfailurematches "^Cannot install linux-firmware on system as boot is managed by snapd.$" apt-get install -y linux-firmware

testfailurematches "boot-managed-by-snapd : Conflicts: linux-image" apt-get install -y linux-image-generic
#FIXME: Because linux-image is a meta package we don't get this info:
#testfailurematches "^Cannot install [^ ]* on system as boot is managed by snapd.$" apt-get install -y linux-image-generic

testfailurematches "boot-managed-by-snapd : Conflicts: secureboot-db" apt-get install -y secureboot-db
testfailurematches "^Cannot install secureboot-db on system as boot is managed by snapd.$" apt-get install -y secureboot-db

case "$ARCHITECTURE" in
    amd64)
        for pkg in grub-pc grub-efi-amd64 grub-efi-amd64-signed; do
            testfailurematches "boot-managed-by-snapd : Conflicts: $pkg" apt-get install -y $pkg
            testfailurematches "^Cannot install $pkg on system as boot is managed by snapd.$" apt-get install -y $pkg
        done
        ;;
    arm64)
        for pkg in grub-efi-arm64 grub-efi-arm64-signed; do
            testfailurematches "boot-managed-by-snapd : Conflicts: grub-efi-arm64" apt-get install -y $pkg
            testfailurematches "^Cannot install grub-efi-arm64.* on system as boot is managed by snapd.$" apt-get install -y $pkg
        done
        ;;
    *)
        echo "E: Unknown test architecture $ARCHITECTURE"
        exit=1
esac


testsuccess apt-get install --allow-remove-essential -y shim-signed boot-managed-by-snapd-
testfailurematches "boot-managed-by-snapd : Conflicts: .* but .* is to be installed" apt-get install -y boot-managed-by-snapd


exit "$exit"
