#!/bin/sh

set -eux

echo "Check which lxd-installer version we have installed"
dpkg -l | grep -wF lxd-installer

echo "Make sure LXD's snap is not seeded in the current image"
if snap list lxd; then
    echo "Removing seeded LXD snap"
    snap remove --purge lxd
fi

echo "Report list of installed snaps:"
snap list

echo "Remove ubuntu from lxd group"
deluser ubuntu lxd

echo "Trigger the on-demand installation without being a member of the lxd group should fail"
! sudo -Hu ubuntu lxc list || false

echo "Check the on-demand installation failed with the proper message:"
out="$(! sudo -Hu ubuntu lxc list 2>&1 || false)"
echo "${out}" | grep -F "Unable to trigger the installation of the LXD snap."

echo "Check that LXD was not installed"
! snap list lxd || false

echo "Add ubuntu to the lxd group"
adduser ubuntu lxd

echo "Check the on-demand installation worked"
sudo -Hu ubuntu lxc list

echo "Check that LXD was installed"
snap list lxd
