#!/bin/sh

. /lib/functions.sh

configure_ee_path() {
	local lxc_bundle oci_bundle name root

	config_load swmodd
	config_get lxc_bundle globals lxc_bundle_root ""
	config_get oci_bundle globals oci_bundle_root ""

	mkdir -p /etc/lxc
	if [ -n "${lxc_bundle}" ]; then
		# if lxc_bundle_root define in swmodd, then remove it
		name=$(echo ${lxc_bundle##/*/})
		root=$(echo ${lxc_bundle%/$name})
		echo "lxc.lxcpath = ${lxc_bundle}" > /etc/lxc/lxc.conf
		uci_set swmodd globals lxc_bundle_root ""
	fi

	if [ -n "${oci_bundle}" ]; then
		# if oci_bundle_root define in swmodd, then remove it
		name=$(echo ${oci_bundle##/*/})
		root=$(echo ${oci_bundle%/$name})
		echo "lxc.lxcpath = ${oci_bundle}" > /etc/lxc/lxc.conf
		uci_set swmodd globals oci_bundle_root ""
	fi

	# configure root in globals section
	if [ -n "${root}" ]; then
		if ! uci_get swmodd globals root >/dev/null; then
			uci_set swmodd globals root ${root}
		fi
	fi

	if [ -z "${name}" ]; then
		name="oci"
	fi

	# configure execenv in swmodd
	if ! uci_get swmodd.@execenv[0].name >/dev/null; then
		uci_add swmodd execenv execenv_1
		uci_set swmodd execenv_1 name ${name}
	fi

}

configure_ee_path
