#!/bin/bash
set -eu -o pipefail
cd `dirname $0`

source conts-env.sh

service_names=$(jq -r '.[].name' ${rspec_file})
for service_name in $service_names; do
    if lxc-info -n ${service_name}; then
        echo "Stopping container: ${service_name}"
        lxc-stop -n ${service_name}
        echo "Destroying container: ${service_name}"
        lxc-destroy -n ${service_name}
        ip=$(jq -r --arg NAME ${service_name} '.[] | select(.name==$NAME) | .ip_address' ${rspec_file})
        sed -i "/dhcp-host=${service_name},/d" /etc/lxc/dnsmasq.conf
    fi
done
