#!/bin/sh

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

SERVICESFILE="-1"

find_service_config() {
	local cfg="$1"

	config_get library "$cfg" library
	if [ "$library" != "olsrd_nameservice" ]; then
		return 1
	fi
	config_get services_file "$cfg" services_file
	SERVICESFILE=$services_file
}

load_services() {
	local olsrd="$1"
	config_load $olsrd
	config_foreach find_service_config LoadPlugin
	local services_configured=0
	if [ "$SERVICESFILE" != "-1" ]; then
		services_configured=1
	fi
	local services=$(cat $SERVICESFILE|grep -ve "^###"|grep -ve "^$")
	json_init
	json_add_boolean configured $services_configured
	json_add_string source "$olsrd"
	json_add_string services "$services"
	json_dump
}

case "$1" in
	list)
		# List method must return the list of methods and parameters that the daemon will accept. Only methods listed here will available to call.
		echo '{ "services4": { }, "services6": { } }'
	;;
	call)
		case "$2" in
			services4)
				load_services "olsrd"
			;;
			services6)
				load_services "olsrd6"
			;;
		esac
	;;
esac
