#!/bin/sh

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

LATLONFILE="-1"

find_service_config() {
  local cfg="$1"

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

load_services() {
  config_load "olsrd"
  config_foreach find_service_config LoadPlugin
  local services_configured=0
  if [ "$LATLONFILE" != "-1" ]; then
      json_init
      json_add_string "data" "$(cat $LATLONFILE)"
      json_dump
      return
  fi
  return 1
}

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 '{ "getdata": { } }'
  ;;
call)
  case "$2" in
  getdata)
    load_services
    ;;
  esac
  ;;
esac
