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

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=ipt-trigger
PKG_VERSION:=1.0.3
PKG_LICENSE:=GPL-2.0

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=ac1beae4794f99533b28db7d0e6e80f4c268a3e8
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/ipt-trigger.git
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

include $(INCLUDE_DIR)/package.mk

define KernelPackage/ipt-trigger
  SUBMENU:=Other modules
  TITLE:=Kernel module for iptables port trigger
  FILES:=$(PKG_BUILD_DIR)/src/ipv4/ipt_TRIGGER.ko
  DEPENDS+=+kmod-nf-nat +kmod-ipt-core
  AUTOLOAD:=$(call AutoLoad,30,ipt_TRIGGER,1)
  KCONFIG:=
endef

define KernelPackage/ip6t-trigger
  SUBMENU:=Other modules
  TITLE:=Kernel module for ip6tables port trigger
  DEPENDS+=+kmod-nf-nat +kmod-ipt-core
  FILES:=$(PKG_BUILD_DIR)/src/ipv6/ip6t_TRIGGER.ko
  AUTOLOAD:=$(call AutoLoad,30,ip6t_TRIGGER,1)
  KCONFIG:=
endef

define KernelPackage/ipt-trigger/description
	Kernel module to enable port trigger for iptables
endef

define KernelPackage/ip6t-trigger/description
	Kernel module to enable port trigger for ip6tables
endef

-include ../../broadcom/bcmkernel/bcm-kernel-toolchain.mk

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
	$(CP) -rf ./ipt-trigger/* $(PKG_BUILD_DIR)/
	$(CP) ./ipt-trigger/include/ipt_TRIGGER.h $(LINUX_DIR)/include/linux/netfilter_ipv4/
endef
else
define Build/Prepare
	$(Build/Prepare/Default)
	$(CP) $(PKG_BUILD_DIR)/include/ipt_TRIGGER.h $(LINUX_DIR)/include/linux/netfilter_ipv4/
endef
endif

define Build/InstallDev
	$(INSTALL_DIR) $(1)/include/linux/netfilter_ipv4
	$(CP) $(PKG_BUILD_DIR)/include/ipt_TRIGGER.h $(1)/include/linux/netfilter_ipv4/
endef

define Build/Compile
	$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)/src/ipv4/" modules
	$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)/src/ipv6/" modules
endef

$(eval $(call KernelPackage,ipt-trigger))
$(eval $(call KernelPackage,ip6t-trigger))
