#
# Copyright (C) 2023 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=tr104
PKG_VERSION:=1.0.40

LOCAL_DEV:=0
ifeq ($(LOCAL_DEV),0)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/voice/tr104.git
PKG_SOURCE_VERSION:=f1dcd097b2e4531fabb6fa84b21225a91053ae11
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

PKG_CONFIG_DEPENDS:=CONFIG_TR104_RUNAS_BBFDM_MICROSERVICE CONFIG_TR104_VENDOR_EXTENSIONS
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE

PKG_USE_NINJA:=0

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

define Package/$(PKG_NAME)
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=TR104 Datamodel Daemon
  MENU:=1
  DEPENDS:=+libuci +libubox +libubus +libblobmsg-json +asterisk +libcurl +PACKAGE_libbbfdm-api:libbbfdm-api +PACKAGE_libbbfdm-ubus:libbbfdm-ubus +PACKAGE_dm-service:dm-service
endef

define Package/$(PKG_NAME)/description
	TR104 datamodel daemon
endef

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

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

CMAKE_OPTIONS += \
	-DBBF_VENDOR_PREFIX:String="$(VENDOR_PREFIX)" \
	-DDAEMON_JSON_INPUT:String="/etc/tr104/input.json" \
	-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
	-DDM_ENABLE_UBUS:BOOL=ON

ifeq ($(CONFIG_TR104_VENDOR_EXTENSIONS),y)
CMAKE_OPTIONS += \
	-DTR104_EXTENSION_DIR:String="iowrt"
endif

ifeq ($(CONFIG_TR104_RUNAS_BBFDM_MICROSERVICE),y)
MAKE_PATH:=libdm
EXTRA_CFLAGS+= -I$(STAGING_DIR)/usr/include/libbbfdm-api
CMAKE_OPTIONS += -DRUNAS_BBFDM_MICROSERVICE:BOOL=ON
else
EXTRA_CFLAGS+= -I$(STAGING_DIR)/usr/include/tr104
endif

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

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/uci-defaults/
ifeq ($(CONFIG_TR104_RUNAS_BBFDM_MICROSERVICE),y)
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/libdm/tr104/libtr104.so $(1) $(PKG_NAME)
ifeq ($(CONFIG_TR104_VENDOR_EXTENSIONS),y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/libdm/extensions/iowrt/libtr104ext.so $(1) $(PKG_NAME)
endif
else
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/tr104/plugins/
	$(INSTALL_BIN) ./files/etc/tr104/input.json $(1)/etc/tr104/input.json
	$(INSTALL_BIN) ./files/etc/init.d/tr104d $(1)/etc/init.d/tr104d
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/daemon/ubus/tr104d $(1)/usr/sbin/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/libdm/tr104/libtr104.so $(1)/etc/tr104
ifeq ($(CONFIG_TR104_VENDOR_EXTENSIONS),y)
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/libdm/extensions/iowrt/libtr104ext.so $(1)/etc/tr104/plugins
endif
endif
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/usr/lib
	$(INSTALL_DIR) $(1)/usr/include/tr104
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/libdmtree/*.h $(1)/usr/include/tr104/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/libdm/common/common.h $(1)/usr/include/tr104/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
