#
# Copyright (C) 2020-2024 iopsys
#

include $(TOPDIR)/rules.mk

PKG_NAME:=bridgemngr
PKG_VERSION:=1.1.6

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

PKG_LICENSE:=GPL-2.0-only
PKG_LICENSE_FILES:=LICENSE

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

define Package/bridgemngr
 CATEGORY:=Utilities
 TITLE:=Bridge Manager
 DEPENDS:=+libuci +libubox +libubus +libblobmsg-json
 DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
endef

define Package/bridgemngr/description
	Package to add Device.Bridging. data model support.
endef

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

MAKE_PATH:=src

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

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

ifeq ($(CONFIG_BRIDGEMNGR_BRIDGE_VLAN),y)
	TARGET_CFLAGS += -DBRIDGE_VLAN_BACKEND
endif

ifeq ($(CONFIG_BRIDGEMNGR_COPY_PBITS),y)
	TARGET_CFLAGS+=-DBRIDGEMNGR_COPY_PBITS
endif

define Package/bridgemngr/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config

	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/libbridgemngr.so $(1) $(PKG_NAME)
ifeq ($(CONFIG_BRIDGEMNGR_BRIDGE_VENDOR_EXT), y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/src/libbridgeext.so $(1) $(PKG_NAME) 10
	$(BBFDM_INSTALL_MS_PLUGIN) -v ${VENDOR_PREFIX} ./files/VLAN_Filtering_Extension.json $(1) $(PKG_NAME) 11
endif

	$(INSTALL_BIN) ./files/etc/init.d/bridging $(1)/etc/init.d/
	$(INSTALL_DATA) ./files/etc/config/bridging $(1)/etc/config/
endef

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

$(eval $(call BuildPackage,bridgemngr))
