#!/bin/sh /etc/rc.common

START=82
STOP=01

USE_PROCD=1
PROG=/usr/sbin/swmodd

log()
{
        logger -t swmodd.init "$*"
}

validate_globals_section()
{
	uci_validate_section swmodd swmodd "globals" \
		'enabled:bool:1' \
		'debug:bool:false' \
		'log_level:uinteger:1' \
		'root:string' \
		'sock:string'
}

start_lxc_container() {
	local root_dir

	config_get name "${1}" name ""
	config_get type "${1}" type ""
	config_get autostart "${1}" autostart "0"
	config_get timeout "${1}" timeout "300"
	root_dir="${2}"

	if [ -z "${name}" ] || [ -z "${type}" ]; then
		return 0;
	fi

	# workaround to install lxc container with installdu and autostart them
	if [ -f "${root_dir}/$name/config" ]; then
		type=lxc
	fi

	if [ "${type}" != "lxc" ]; then
		return 0;
	fi

	state=$(lxc-info -n "$name" -s -H)
	if [ -z "${state}" ]; then
		return 0;
	fi

	if [ "${autostart}" == "0" ]; then
		if [ "${state}" == "RUNNING" ]; then
			# stop the container if running
			lxc-stop -k -n "${name}" &
			return 0;
		elif [ "${state}" == "FROZEN" ]; then
			# first unfreeze then stop
			lxc-unfreeze -n "${name}"
			lxc-stop -k -n "${name}" &
			return 0;
		fi
	else
		if [ "${state}" == "FROZEN" ]; then
			# unfreeze the container
			lxc-unfreeze -n "${name}"
		elif [ "${state}" == "STOPPED" ]; then
			# start the container
			lxc-start -n "${name}"
			return 0;
		fi
	fi
}

stop_lxc_containers() {
	for f in `lxc-ls`; do
		lxc-stop -k -n $f >/dev/null 2>&1;
	done
}

start_service() {
	local enabled debug log_level sock root

	config_load swmodd
	validate_globals_section || {
		return 1;
	}

	[ "${enabled}" -eq 0 ] && return 0

	# crun default runtime directory /run, if not present then create
	if [ ! -d "/run" ]; then
		if [ ! -L "/run" ]; then
			ln -fs /var/run /run
		fi
	fi

	if [ -L "${root}" ]; then
		if [ ! -d "${root}" ]; then
			mkdir -p $(realpath ${root})
		fi
	fi

	if [ ! -d "${root}" ]; then
		log "# Not starting, Base root [${root}] not accessible/defined"
		return 1
	fi

	# Currently only one execenv supported
	env_name="$(uci -q get swmodd.@execenv[0].name)"
	if [ -z "${env_name}" ]; then
		log "# Not starting, execenv name [${env_name}] not defined"
		return 1
	fi

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

	bundle_root="${bundle_root}${env_name}"
	if [ ! -d "${bundle_root}" ]; then
		if [ -n "${bundle_root}" ]; then
			mkdir -p "${bundle_root}"
		else
			log "# Not starting, execenv [${bundle_root}] not defined"
			return 1
		fi
	fi

	procd_open_instance swmodd
	procd_set_param command ${PROG}

	if [ "${debug}" -eq 1 ]; then
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_append_param command -l "${log_level}"
	fi

	if [ -f "${sock}" ]; then
		procd_append_param command -s "${sock}"
	fi

	procd_set_param respawn
	procd_close_instance

	# Auto-start the lxc containers
	if [ -f "${bundle_root}/ocicontainer" ]; then
		UCI_CONFIG_DIR="${bundle_root}"
		config_load ocicontainer
		config_foreach start_lxc_container du_eu_assoc ${bundle_root}
	fi
}

stop_service() {
	stop_lxc_containers
}

reload_service() {
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "swmodd"
}
