#!/bin/sh
# Migration script for fchomo server
# Used to migrate LuCI application server option.

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

CONF=fchomo

config_load "$CONF"

# isDefined <section> <option>
isDefined() {
	local opt=CONFIG_${1}_${2};

	eval "[ -n \"\${$opt+x}\" ] && return 0 || return 1"
}

migrate() {
	# tuic_congestion_controller -> congestion_controller
	if isDefined "$1" tuic_congestion_controller; then
		local tuic_congestion_controller
		config_get tuic_congestion_controller "$1" tuic_congestion_controller ""
		uci_remove "$CONF" "$1" tuic_congestion_controller
		uci_set "$CONF" "$1" congestion_controller "$tuic_congestion_controller"
	fi

	# tuic_max_idle_time -> max_idle_time
	if isDefined "$1" tuic_max_idle_time; then
		local tuic_max_idle_time
		config_get tuic_max_idle_time "$1" tuic_max_idle_time ""
		uci_remove "$CONF" "$1" tuic_max_idle_time
		uci_set "$CONF" "$1" max_idle_time "$tuic_max_idle_time"
	fi

	# plugin -> plugin_type
	if isDefined "$1" plugin; then
		local plugin
		config_get plugin "$1" plugin ""
		uci_set "$CONF" "$1" plugin "1"
		uci_set "$CONF" "$1" plugin_type "$plugin"
	fi
}

config_foreach migrate server

uci_commit "$CONF"

exit 0
