#
# Copyright (C) 2020-2023 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk

PKG_NAME:=hostmngr
PKG_VERSION:=1.4.3

LOCAL_DEV=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=667866b8149d3df83a05536319eac02aee0b6d75
PKG_SOURCE_URL:=https://dev.iopsys.eu/iopsys/hostmngr.git
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)_$(PKG_SOURCE_VERSION).tar.xz
PKG_MIRROR_HASH:=skip
endif
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

PKG_MAINTAINER:=Anjan Chanda <anjan.chanda@iopsys.eu>
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE

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


define Package/hostmngr
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Show and manage hosts in the network
  DEPENDS= +libubox +libuci +libubus +ubus +libeasy +libnl-genl \
	   +libjson-c +libblobmsg-json +libnfnetlink +libmnl \
	   +libnetfilter-conntrack \
	   +HOSTMNGR_PLATFORM_HAS_WIFI:libwifi +libwifiutils
  DEPENDS+= +libbbfdm-api +libbbfdm-ubus +dm-service
endef

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

define Package/hostmngr/description
 This package provides hostmngr that can detect and show host devices
 present in the network.
endef

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

ifeq ($(CONFIG_HOSTMNGR_PLATFORM_HAS_WIFI),y)
TARGET_CFLAGS += -DHAS_WIFI
endif

ifeq ($(CONFIG_HOSTMNGR_DATAMODEL_EXT),y)
TARGET_CFLAGS += -DHAS_VENDOR_EXT
ifeq ($(CONFIG_HOSTMNGR_VENDOR_PREFIX),"")
TARGET_CFLAGS += -DCUSTOM_PREFIX=\\\"$(CONFIG_BBF_VENDOR_PREFIX)\\\"
else
TARGET_CFLAGS += -DCUSTOM_PREFIX=\\\"$(CONFIG_HOSTMNGR_VENDOR_PREFIX)\\\"
endif
endif

MAKE_PATH:=src

define Package/hostmngr/install
	$(CP) ./files/etc $(1)/
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/hostmngr $(1)/usr/sbin/
	$(INSTALL_DIR) $(1)/usr/share/hostmngr
	$(INSTALL_DATA) ./files/scripts/hosts_acl.sh $(1)/usr/share/hostmngr/

	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/bbf_plugin/libhostmngr.so $(1) $(PKG_NAME)

ifeq ($(CONFIG_HOSTMNGR_DATAMODEL_EXT),y)
	$(BBFDM_INSTALL_MS_PLUGIN) $(PKG_BUILD_DIR)/src/bbf_plugin/libhostext.so $(1) $(PKG_NAME) 10
endif

endef

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
        rsync -r --exclude=.* ~/git/hostmngr/ $(PKG_BUILD_DIR)/
endef
endif

$(eval $(call BuildPackage,hostmngr))
