#
# Copyright (C) 2022-2024 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk

PKG_NAME:=dnsmngr
PKG_VERSION:=1.0.19

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/dnsmngr.git
PKG_SOURCE_VERSION:=f22f14c1f1b8b17db99fd87eadbc9fe254b55c7f
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk
include ../bbfdm/bbfdm.mk

MAKE_PATH:=src

define Package/dnsmngr
  SECTION:=net
  CATEGORY:=Network
  DEPENDS:=+libuci +libubox +libubus +libblobmsg-json +libjson-c +odhcpd
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
  DEPENDS+=+DNSMNGR_DNS_SD:umdns
  DEPENDS+=+DNSMNGR_BACKEND_DNSMASQ:dnsmasq
  DEPENDS+=+DNSMNGR_BACKEND_UNBOUND:unbound-daemon +DNSMNGR_BACKEND_UNBOUND:unbound-control
  TITLE:=Package to configure DNS backend and TR-181 support
endef

define Package/dnsmngr/description
  Package to configure DNS backend and TR-181 support.
endef

define Package/$(PKG_NAME)/config
	source "$(SOURCE)/Config.in"
endef

ifeq ($(CONFIG_DNSMNGR_DNS_SD),y)
define Build/Compile
	$(call Build/Compile/Default,all)
endef
else
define Build/Compile
	$(call Build/Compile/Default,dns)
endef
endif

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
	$(CP) -rf ~/git/dnsmngr/* $(PKG_BUILD_DIR)/
endef
endif

ifeq ($(CONFIG_DNSMNGR_BACKEND_DNSMASQ),y)
        TARGET_CFLAGS += -DDNSMASQ_BACKEND
endif

define Package/dnsmngr/install
	$(INSTALL_DIR) $(1)/etc/umdns
	$(INSTALL_DIR) $(1)/etc/umdns/tmp
	$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
	$(INSTALL_DATA) ./files/lib/upgrade/keep.d/dnsmngr $(1)/lib/upgrade/keep.d/dnsmngr
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/libdnsmngr.so $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_SCRIPT) -d $(PKG_BUILD_DIR)/scripts/nslookup $(1)
ifeq ($(CONFIG_DNSMNGR_DNS_SD),y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/src/libdns_sd.so $(1) $(PKG_NAME) 10
endif
endef

$(eval $(call BuildPackage,dnsmngr))
