#
# Copyright (C) 2024 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk

PKG_NAME:=firewallmngr
PKG_VERSION:=1.0.9.5

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/firewallmngr.git
PKG_SOURCE_VERSION:=02dc90c48d996148b68d02632bac13a28d75cf25
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 ../bbfdm/bbfdm.mk

MAKE_PATH:=src

define Package/firewallmngr
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Package to add Device.Firewall and Device.NAT. data model support.
  DEPENDS:=+libuci +libubox +libubus +libblobmsg-json +libjson-c +firewall
  DEPENDS+=+FIREWALLMNGR_PORT_TRIGGER:iptables-mod-trigger
  DEPENDS+=+FIREWALLMNGR_PORT_TRIGGER:iptables-mod-nfqueue
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
endef

define Package/firewallmngr/description
  Package to add Device.Firewall. and Device.NAT. data model support.
endef

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

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

ifeq ($(CONFIG_FIREWALLMNGR_PORT_TRIGGER),y)
	TARGET_CFLAGS += -DINCLUDE_PORT_TRIGGER
endif

ifeq ($(CONFIG_FIREWALLMNGR_NAT_INTERFACE_SETTING),y)
	TARGET_CFLAGS += -DINCLUDE_NAT_IF_SETTING
endif

define Package/firewallmngr/install
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/uci-defaults
ifeq ($(CONFIG_FIREWALLMNGR_PORT_TRIGGER),y)
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/lib/port-trigger

	$(INSTALL_BIN) ./files/port-trigger/etc/init.d/port-trigger $(1)/etc/init.d/
	$(INSTALL_DATA) ./files/port-trigger/etc/config/port-trigger $(1)/etc/config/
	$(INSTALL_DATA) ./files/port-trigger/lib/port-trigger/port_trigger.sh $(1)/lib/port-trigger/
endif
	$(INSTALL_BIN) ./files/firewall.portmap $(1)/etc/
	$(INSTALL_DATA) ./files/etc/uci-defaults/95-portmap-firewall $(1)/etc/uci-defaults/

	$(INSTALL_BIN) ./files/firewall.service $(1)/etc/
	$(INSTALL_DATA) ./files/etc/uci-defaults/97-firewall-service $(1)/etc/uci-defaults/

	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
	$(BBFDM_INSTALL_MS_DM) $(PKG_BUILD_DIR)/src/libfirewallmngr.so $(1) $(PKG_NAME)
endef

$(eval $(call BuildPackage,firewallmngr))
