#
# Copyright (C) 2020-2025 IOPSYS Software Solutions AB
#
# This is free software, licensed under the BSD-3-Clause
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=icwmp
PKG_VERSION:=9.10.10

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/icwmp.git
PKG_SOURCE_VERSION:=63251b6c9789b1428604af0a5c1d23d32d14a8b8
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 $(INCLUDE_DIR)/cmake.mk
include ../bbfdm/bbfdm.mk

define Package/icwmp
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=TR069 CWMP client
  DEPENDS:=+libuci +libubox +libblobmsg-json +libubus +libjson-c +libcurl +mxml +libuuid +libopenssl
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service +sysmngr
  MENU:=1
endef

define Package/icwmp/description
 TR069 client implementation with bbfdm backend for TR181 support
endef

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

ifeq ($(CONFIG_ICWMP_MGMT_FROM_USP),y)
EXTRA_CFLAGS += -DCWMP_DUAL_SUPPORT=BBFDM_BOTH
endif

ifeq ($(CONFIG_ICWMP_BACKUP_EVENTS),y)
EXTRA_CFLAGS += -DPERSIST_BACKUP_SESSION_EVENTS
endif

ifeq ($(CONFIG_ICWMP_ENABLE_VENDOR_EXTN),y)
EXTRA_CFLAGS += -DICWMP_ENABLE_VENDOR_EXTN
endif

ifeq ($(CONFIG_ICWMP_ENABLE_SMM_SUPPORT),y)
EXTRA_CFLAGS += -DICWMP_ENABLE_SMM_SUPPORT
endif

ifeq ($(CONFIG_ICWMP_VENDOR_PREFIX),"")
CMAKE_OPTIONS += -DBBF_VENDOR_PREFIX:String="$(CONFIG_BBF_VENDOR_PREFIX)"
else
CMAKE_OPTIONS += -DBBF_VENDOR_PREFIX:String="$(CONFIG_ICWMP_VENDOR_PREFIX)"
endif


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

define Package/icwmp/install
	$(INSTALL_DIR) $(1)/etc/icwmpd
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
	$(INSTALL_DIR) $(1)/etc/udhcpc.user.d
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/icwmpd $(1)/usr/sbin/icwmpd
	$(INSTALL_DATA) ./files/etc/config/cwmp $(1)/etc/config/cwmp
	$(INSTALL_BIN) ./files/etc/init.d/icwmpd $(1)/etc/init.d/icwmpd
	$(INSTALL_BIN) ./files/etc/uci-defaults/50-cwmp-align-keep-config $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/85-cwmp-set-userid $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/90-cwmpfirewall $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/95-set-random-inform-time $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/85-migrate-gw-info $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/999-cwmp-conn-config $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/icwmpd/vendor_log.sh $(1)/etc/icwmpd/vendor_log.sh
	$(INSTALL_BIN) ./files/etc/icwmpd/firewall.cwmp $(1)/etc/icwmpd/firewall.cwmp
	$(INSTALL_DATA) ./files/lib/upgrade/keep.d/icwmp $(1)/lib/upgrade/keep.d/icwmp
	$(INSTALL_BIN) ./files/etc/udhcpc.user.d/udhcpc_icwmp_opt43.user $(1)/etc/udhcpc.user.d/udhcpc_icwmp_opt43.user
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/libcwmpdm.so $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_PLUGIN) ./files/etc/bbfdm/json/CWMPManagementServer.json $(1) $(PKG_NAME) 20
endef

$(eval $(call BuildPackage,icwmp))
