#!/bin/sh

. /lib/functions.sh

cl_id=1
srv_id=1

function get_ddns_config_option() {
	local val

	val="$(uci -q get ddns.${1}.${2})"

	if [ -z "${val}" ] && [ -n "${3}" ]; then
		val="${3}"
	fi

	echo "${val}"
}

function config_supported_service() {
	if ! uci -q get ddnsmngr.global >/dev/null 2>&1; then
		uci -q set ddnsmngr.global="ddnsmngr"
	fi

	servers=""

	for i in $(find /etc/ddnsmngr/servers/ -name '*.json' | cut -d'/' -f 5 | sed "s/.json//")
	do
		if [ -z "${servers}" ]; then
			servers="${i}"
		else
			servers="${servers},${i}"
		fi
	done

	uci -q set ddnsmngr.global.supported_services="${servers}"
}

function migrate_service_section() {
	client_sec=""
	enabled="$(get_ddns_config_option ${1} enabled "0")"
	service_name="$(get_ddns_config_option ${1} service_name)"
	interface="$(get_ddns_config_option ${1} interface)"
	ip_network="$(get_ddns_config_option ${1} ip_network)"
	username="$(get_ddns_config_option ${1} username)"
	password="$(get_ddns_config_option ${1} password)"
	lookup_host="$(get_ddns_config_option ${1} lookup_host)"
	use_ipv6="$(get_ddns_config_option ${1} use_ipv6 "0")"
	force_ipversion="$(get_ddns_config_option ${1} force_ipversion "0")"
	use_https="$(get_ddns_config_option ${1} use_https "0")"
	force_dnstcp="$(get_ddns_config_option ${1} force_dnstcp "0")"

	if [ -z "${service_name}" ]; then
		uci -q delete ddns."${1}"
		return 0
	fi

	# check server file is present in device
	if [ ! -f "/etc/ddnsmngr/servers/${service_name}.json" ]; then
		uci -q delete ddns."${1}"
		return 0
	fi

	# Check if client section is already added for this service
	clients=$(uci -q show ddnsmngr | grep "=client")
	client_count=$(echo "${clients}" | wc -l)

	tmp=0
	while [ $tmp -lt $client_count ]
	do
		mngr_serv="$(uci -q get ddnsmngr.@client[$tmp].service_name)"
		mngr_intf="$(uci -q get ddnsmngr.@client[$tmp].interface)"
		mngr_netw="$(uci -q get ddnsmngr.@client[$tmp].ip_network)"
		mngr_user="$(uci -q get ddnsmngr.@client[$tmp].username)"
		mngr_ipv6="$(uci -q get ddnsmngr.@client[$tmp].use_ipv6)"
		mngr_forceip="$(uci -q get ddnsmngr.@client[$tmp].force_ipversion)"
		mngr_https="$(uci -q get ddnsmngr.@client[$tmp].use_https)"
		mngr_dnstcp="$(uci -q get ddnsmngr.@client[$tmp].force_dnstcp)"

		[ -z "${mngr_ipv6}" ] && mngr_ipv6="0"
		[ -z "${mngr_forceip}" ] && mngr_forceip="0"
		[ -z "${mngr_https}" ] && mngr_https="0"
		[ -z "${mngr_dnstcp}" ] && mngr_dnstcp="0"

		if [ "${mngr_serv}" == "${service_name}" ] && [ "${mngr_intf}" == "${interface}" ] && \
		   [ "${mngr_netw}" == "${ip_network}" ] && [ "${mngr_user}" == "${username}" ] && \
		   [ "${mngr_ipv6}" == "${use_ipv6}" ] && [ "${mngr_forceip}" == "${force_ipversion}" ] && \
		   [ "${mngr_https}" == "${use_https}" ] && [ "${mngr_dnstcp}" == "${force_dnstcp}" ]; then
			break
		fi

		tmp=$(( tmp + 1 ))
	done

	if [ $tmp -ne $client_count ]; then
		i=0
		for client in $clients; do
			if [ $i -eq $tmp ]; then
				client_sec="$(echo $client | cut -d'=' -f 1 | cut -d'.' -f 2)"
				break
			fi
			i=$(( i + 1 ))
		done

		if [ $enabled -eq 1 ]; then
			uci -q set ddnsmngr."${client_sec}".enabled="1"
		fi
	else
		client_sec=ddns_mig_client_"${cl_id}"

		uci -q set ddnsmngr."${client_sec}"="client"
		uci -q set ddnsmngr."${client_sec}".enabled="${enabled}"
		uci -q set ddnsmngr."${client_sec}".service_name="${service_name}"
		uci -q set ddnsmngr."${client_sec}".interface="${interface}"
		uci -q set ddnsmngr."${client_sec}".ip_network="${ip_network}"
		uci -q set ddnsmngr."${client_sec}".username="${username}"
		uci -q set ddnsmngr."${client_sec}".password="${password}"
		uci -q set ddnsmngr."${client_sec}".use_ipv6="${use_ipv6}"
		uci -q set ddnsmngr."${client_sec}".force_ipversion="${force_ipversion}"
		uci -q set ddnsmngr."${client_sec}".use_https="${use_https}"
		uci -q set ddnsmngr."${client_sec}".force_dnstcp="${force_dnstcp}"

		cl_id=$(( cl_id + 1 ))

		# add server section if not added
		servers=$(uci -q show ddnsmngr | grep "service=\'${service_name}\'")
		if [ -z "${servers}" ]; then
			server_sec=ddns_mig_server_"${srv_id}"
			uci -q set ddnsmngr."${server_sec}"="server"
			uci -q set ddnsmngr."${server_sec}".enabled="1"
			uci -q set ddnsmngr."${server_sec}".service="${service_name}"
			uci -q set ddnsmngr."${server_sec}".name="${service_name}"

			srv_id=$(( srv_id + 1 ))
		fi
	fi


	# if lookup_host is set then add host section
	if [ -n "${lookup_host}" ] && [ -n "${client_sec}" ]; then
		# check number of hosts present for this client
		host_count=$(uci -q show ddnsmngr | grep "dm_parent=\'${client_sec}\'" | wc -l)
		host_ix=$(( host_count + 1 ))
		host_sec="${client_sec}"_host_"${host_ix}"

		uci -q set ddnsmngr."${host_sec}"="host"
		uci -q set ddnsmngr."${host_sec}".enabled="${enabled}"
		uci -q set ddnsmngr."${host_sec}".lookup_host="${lookup_host}"
		uci -q set ddnsmngr."${host_sec}".dm_parent="${client_sec}"
	fi

	uci -q delete ddns."${1}"
}

function migrate_ddns_config() {
	if [ ! -f "/etc/config/ddnsmngr" ]; then
		# Create ddnsmngr config file
		touch /etc/config/ddnsmngr
	fi

	config_supported_service

	config_load ddns
	config_foreach migrate_service_section service

	uci -q commit ddns
	uci -q commit ddnsmngr
}

migrate_ddns_config
