#
# shater-core — data-plane glue for the Shater transparent-proxy stack.
#
# Ships the "железно" (rock-solid) static layer that the Go control-plane
# (xrayctl) and the LuCI app sit on: the procd init that supervises the stock
# xray binary against xrayctl's generated /etc/xray/run.json, the hotplug hook
# that re-persists policy routing, the sysctl knobs TPROXY needs, a minimal
# inert UCI default, and one-time rt_tables seeding.
#
# Pure scripts + config => PKGARCH:=all. Nothing is compiled here.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=shater-core
PKG_VERSION:=0.1.0
PKG_RELEASE:=19

PKG_MAINTAINER:=Shater <maqrota@icloud.com>
PKG_LICENSE:=GPL-2.0-or-later

include $(INCLUDE_DIR)/package.mk

define Package/shater-core
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Shater transparent-proxy data-plane glue
  URL:=https://github.com/shater
  # xrayctl  : control-plane (gen/test/apply/reconcile) invoked by our init/hotplug
  # xray-core: the /usr/bin/xray engine our init supervises
  # dnsmasq-full: nftset= support for domain-based routing sets
  # kmod-nft-tproxy + kmod-nft-socket: kernel TPROXY (xrayctl emits the rules)
  # ip-full  : `ip rule`/`ip route`/rt_tables for policy routing
  DEPENDS:=+xrayctl +xray-core +dnsmasq-full +kmod-nft-tproxy +kmod-nft-socket +ip-full
  PKGARCH:=all
endef

define Package/shater-core/description
  Static data-plane glue for the Shater xray-based transparent proxy: procd
  init (supervises xray on xrayctl-generated /etc/xray/run.json and reconciles
  policy routing / our nft table `inet shater`), an ifup/ifdown hotplug hook
  that re-persists ip rules & routes, TPROXY sysctl settings, a minimal inert
  UCI default (globals disabled until configured), and idempotent first-boot
  setup. Designed to never break connectivity: fully inert until explicitly
  enabled and a generated runtime config exists.
endef

# /etc/config/shater is user-editable desired state -> preserve on upgrade.
define Package/shater-core/conffiles
/etc/config/shater
endef

# Nothing to fetch or build.
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
endef

define Build/Compile
endef

define Package/shater-core/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/shater $(1)/etc/init.d/shater
	$(INSTALL_BIN) ./files/etc/init.d/shater-cron $(1)/etc/init.d/shater-cron

	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
	$(INSTALL_BIN) ./files/etc/hotplug.d/iface/99-shater $(1)/etc/hotplug.d/iface/99-shater

	$(INSTALL_DIR) $(1)/etc/sysctl.d
	$(INSTALL_DATA) ./files/etc/sysctl.d/99-shater.conf $(1)/etc/sysctl.d/99-shater.conf

	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/etc/config/shater $(1)/etc/config/shater

	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/etc/uci-defaults/30_shater-core $(1)/etc/uci-defaults/30_shater-core

	$(INSTALL_DIR) $(1)/usr/share/shater
	$(INSTALL_DATA) ./files/usr/share/shater/README $(1)/usr/share/shater/README
endef

$(eval $(call BuildPackage,shater-core))
