#
# Copyright (C) 2020-2024 Iopsys
#

include $(TOPDIR)/rules.mk

PKG_NAME:=ethmngr
PKG_VERSION:=3.0.9

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/hal/ethmngr.git
PKG_SOURCE_VERSION:=0bcfd98d64b5bd8d3162944e2abd5740f9bf4b92
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)_$(PKG_SOURCE_VERSION).tar.xz
PKG_MIRROR_HASH:=skip
endif

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

include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/feeds/iopsys/bbfdm/bbfdm.mk

define Package/ethmngr
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Ethernet status and configration utility
  DEPENDS:=+libethernet +libuci +libubox +ubus +libpthread +libnl-genl +libeasy
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service +libblobmsg-json +libjson-c +libc +libubus
endef

define Package/ethmngr/description
 This package can be used to configure and provide status about
 the ethernet interfaces and ports through UBUS.
 It uses APIs from the libethernet.so library.
endef

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

TARGET_CFLAGS += \
	-I$(STAGING_DIR)/usr/include \
	-I$(STAGING_DIR)/usr/include/libnl3 \
	-D_GNU_SOURCE

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

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

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


ifeq ($(CONFIG_ETHMNGR_MACVLAN_EXTENSION),y)
TARGET_CFLAGS += -DETHMNGR_MACVLAN_EXTENSION
endif

ifeq ($(CONFIG_ETHMNGR_EXPOSE_ETHERNET_OBJECT),y)
TARGET_CFLAGS += -DETHMNGR_EXPOSE_ETHERNET_OBJECT
endif

define Package/ethmngr/install
	$(CP) ./files/* $(1)/
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ethmngr $(1)/usr/sbin/
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
endef

$(eval $(call BuildPackage,ethmngr))
