#!/bin/bash

set -e -u
# bullsyeye: 11 CMake 3.18.4; not supported
# bookworm:  12
# trixie     13 <- current stable
# forky      14 <- current testing
# duke       15

. ./config.sh

a=$(sudo ls /var/lib/lxc/$RELEASE/rootfs) || echo Ignoring

if [ -z "$a" ] || [ 1 == 1 ]; then
    sudo lxc-stop -n ${RELEASE} || echo Ignoring...
    sudo lxc-destroy -n ${RELEASE} || echo Ignoring...
    # sudo lxc-create -n debian_stable -t debian -- -r bullseye -a amd64
    echo "### Creating image"
    sudo lxc-create -n ${RELEASE} -t download -- -d $DISTRI -r $RELEASE -a amd64
    echo "lxc.apparmor.profile = unconfined" | sudo tee -a /var/lib/lxc/$RELEASE/config
    # sudo lxc-create -n ${RELEASE} -t debian -- -r $RELEASE -a amd64
fi

# sudo lxc-start -n debian_stable
# sudo lxc-stop -n debian_stable

# /usr/bin/lxc-post-create.sh debian_stable

sudo cp ./setup_node.sh /var/lib/lxc/${RELEASE}/rootfs

# sudo lxc-execute -n ${RELEASE} /setup_node.sh

echo "### Starting"

sudo lxc-start -n ${RELEASE}
sleep 2
sudo lxc-attach -n ${RELEASE} -- /setup_node.sh
sudo lxc-stop -n ${RELEASE}


echo "[$0] fin."
