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

include $(TOPDIR)/rules.mk

PKG_NAME:=dhcpmngr
PKG_VERSION:=1.1.6

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/dhcpmngr.git
PKG_SOURCE_VERSION:=74d96cd70119e4ea08767d68b45b4922162d0328
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.zst
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/dhcpmngr
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Package to add Device.DHCPv4 and v6 data model support.
  DEPENDS:=+libuci +libubox +libubus +libblobmsg-json +libjson-c
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
  DEPENDS+=+DNSMNGR_DNS_SD:umdns
  DEPENDS+=+DNSMNGR_BACKEND_DNSMASQ:dnsmasq
  DEPENDS+=+DNSMNGR_BACKEND_UNBOUND:odhcpd
endef

define Package/dhcpmngr/description
  Package to add Device.DHCPv4. and Device.DHCPv6. data model support.
endef

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

ifeq ($(CONFIG_DHCPMNGR_ENABLE_VENDOR_EXT),y)
MAKE_FLAGS += DHCPMNGR_ENABLE_VENDOR_EXT=y
endif

ifeq ($(CONFIG_DHCPMNGR_VENDOR_PREFIX),"")
VENDOR_PREFIX = $(CONFIG_BBF_VENDOR_PREFIX)
else
VENDOR_PREFIX = $(CONFIG_DHCPMNGR_VENDOR_PREFIX)
endif

TARGET_CFLAGS += -DBBF_VENDOR_PREFIX=\\\"$(VENDOR_PREFIX)\\\"

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

define Package/dhcpmngr/install
	$(INSTALL_DIR) $(1)/etc/udhcpc.user.d
	$(INSTALL_BIN) ./files/etc/udhcpc.user.d/udhcpc_lease_start_time.user $(1)/etc/udhcpc.user.d/udhcpc_lease_start_time.user
ifeq ($(CONFIG_DNSMNGR_BACKEND_UNBOUND),y)
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DATA) ./files/etc/uci-defaults/unbound.odhcpd.uci_default $(1)/etc/uci-defaults/16-set-unbound-as-odhcpd-leasetrigger
endif
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/libdhcpmngr.so $(1) $(PKG_NAME)
endef

$(eval $(call BuildPackage,dhcpmngr))
