#
# Copyright (C) 2021-2025 IOPSYS
#

include $(TOPDIR)/rules.mk

PKG_NAME:=parental-control
PKG_VERSION:=1.4.6

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/parental-control.git
PKG_SOURCE_VERSION:=11777ff069888fc543c2501110313b654bbbfbc9
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 $(TOPDIR)/feeds/iopsys/bbfdm/bbfdm.mk

define Package/parental-control
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=URL filter
  DEPENDS:=+libuci +libnetfilter-queue +libnfnetlink +iptables-mod-nfqueue +libpthread
  DEPENDS+=+libubox +ubus +conntrack +libcurl +cmph +libjson-c
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +dm-service
endef

define Package/parental-control/description
	Enables filtering of packets on the basis of URL and source MAC address.
endef

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

TARGET_CFLAGS += -D_GNU_SOURCE

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

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

ifeq ($(CONFIG_PARENTAL_CONTROL_URLFILTERING),y)
	TARGET_CFLAGS+=-DPARENTAL_CONTROL_ULRFILTER
endif

TARGET_LDFLAGS += -lcmph

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

define Package/parental-control/install
	$(INSTALL_DIR) $(1)/etc/parentalcontrol
	$(INSTALL_DIR) $(1)/lib/parentalcontrol
	$(INSTALL_DATA) ./files/lib/parentalcontrol/parentalcontrol.sh $(1)/lib/parentalcontrol/
	$(INSTALL_BIN) ./files/lib/parentalcontrol/sync_bundles.sh $(1)/lib/parentalcontrol/

	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_DATA) ./files/etc/firewall.parentalcontrol $(1)/etc/

	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/urlfilter $(1)/usr/sbin

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/parentalcontrol $(1)/etc/init.d/

	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DATA) ./files/etc/config/parentalcontrol $(1)/etc/config/parentalcontrol

	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DATA) ./files/etc/uci-defaults/95-firewall_parentalcontrol.ucidefaults $(1)/etc/uci-defaults/
	$(INSTALL_DATA) ./files/etc/uci-defaults/35-migrate_urlfilter.ucidefaults $(1)/etc/uci-defaults/

	$(BBFDM_REGISTER_SERVICES) -v ${VENDOR_PREFIX} ./bbfdm_service.json $(1) parentalcontrol

	$(INSTALL_DATA) ./files/etc/uci-defaults/40-parental_control_update_bundle_path $(1)/etc/uci-defaults/
ifeq ($(CONFIG_PARENTAL_CONTROL_URLFILTERING),y)
	$(INSTALL_DATA) ./files/etc/parentalcontrol/url_bundles.json $(1)/etc/parentalcontrol/
	$(INSTALL_DATA) ./files/etc/parentalcontrol/urlbundle_override.json $(1)/etc/parentalcontrol/
else
	$(BBFDM_INSTALL_MS_PLUGIN) -v ${VENDOR_PREFIX} ./files/urlbundle_override.json $(1) parentalcontrol
endif
endef

$(eval $(call BuildPackage,parental-control))
