#!/bin/bash

set -e -u

if [ ! "${RELEASE+x}" ]; then
   . ./config.sh
fi

sudo cp ./test_node.sh /var/lib/lxc/$RELEASE/rootfs/home/user

sudo mkdir -p /var/lib/lxc/$RELEASE/rootfs/home/user/wp/src

sudo rm -rf /var/lib/lxc/$RELEASE/rootfs/home/user/wp/src/liblepto

a=""
a=$(sudo ls /var/lib/lxc/$RELEASE/rootfs/home/user/wp/src/liblepto)  || echo -n

# sudo if [ ! -e /var/lib/lxc/debian_stable/rootfs/home/user/wp/src/liblepto ]; then
if [ -z "$a" ]; then
   echo "# Copy files to /var/lib/lxc/${RELEASE}/rootfs/home/user/wp/src/liblepto"
   sudo cp -R ${HOME}/wp/src/campo/liblepto.github /var/lib/lxc/$RELEASE/rootfs/home/user/wp/src/liblepto
fi

sudo lxc-stop -n ${RELEASE} &> /dev/null || echo -n

NID=$(sudo lxc-execute -n ${RELEASE} -- id user -g)
sudo chown $NID:$NID -R /var/lib/lxc/${RELEASE}/rootfs/home/user/wp

# sudo lxc-execute -n debian_stable -u 1000 -g 1000 /home/user/test_node.sh

echo "### Starting node"

sudo lxc-start -n ${RELEASE}
sleep 1
sudo lxc-attach -n ${RELEASE} -- su user /home/user/test_node.sh
# -u 1000 -g 1000 -- /home/user/test_node.sh
sudo lxc-stop -n ${RELEASE}


echo "[$0] fin."
