#!/bin/sh

. /lib/functions.sh

set_execenv_ref() {
	config_get ee_name "${1}" ee_name ""

	if [ "${ee_name}" != "${2}" ]; then
		uci_set ocicontainer "${1}" ee_name "${2}"
	fi
}

migrate_execenv_ref() {
	config_load swmodd

	env_name=$(uci_get swmodd.@execenv[0].name)
	bundle_root=$(uci_get swmodd.globals.root)

	if [ -z "${bundle_root}" ]; then
		return 0
	fi

	if [ -z "${env_name}" ]; then
		return 0
	fi

	if [ "${bundle_root: -1}" != '/' ]; then
		bundle_root="${bundle_root}/"
	fi

	lxcpath="${bundle_root}${env_name}"

	if [ -f "${lxcpath}/ocicontainer" ]; then
		UCI_CONFIG_DIR="${lxcpath}"
		config_load ocicontainer
		config_foreach set_execenv_ref du_eu_assoc ${env_name}
		uci -q -c "${lxcpath}" commit ocicontainer
	fi
}

migrate_execenv_ref
