include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-open-box
PKG_VERSION:=0.1.385
PKG_RELEASE:=56

PKG_MAINTAINER:=liandu2024
PKG_LICENSE:=GPL-3.0-or-later

# 1. 自动判断目标编译架构，映射至 Open-Box Release 预编译资产名
ifeq ($(ARCH),x86_64)
  OPENBOX_ARCH:=x64
endif
ifeq ($(ARCH),aarch64)
  OPENBOX_ARCH:=arm64
endif

PKG_SOURCE:=open-box-linux-$(OPENBOX_ARCH).tar.gz
PKG_SOURCE_URL:=https://github.com/liandu2024/Open-Box/releases/download/v$(PKG_VERSION)/
PKG_HASH:=skip

include $(INCLUDE_DIR)/package.mk

define Package/luci-app-open-box
  SECTION:=luci
  CATEGORY:=LuCI
  SUBMENU:=3. Applications
  TITLE:=LuCI support for Open-Box Transparent Proxy (Full Core Included)
  # 架构约束：仅在目标平台为 x86_64 或 aarch64 时在 menuconfig 中展示，防止误选到不支持的 MIPS/ARMv7 平台
  DEPENDS:=@(x86_64||aarch64) +kmod-tun +kmod-nft-queue +kmod-nft-nat +kmod-veth +ip-full +ca-bundle +luci-base
endef

define Package/luci-app-open-box/description
  Open-Box all-in-one transparent proxy management suite for OpenWrt.
  Pre-packaged with full sing-box core, embedded Node.js runtime, GeoSite/GeoIP databases and web dashboard.
endef

define Package/luci-app-open-box/conffiles
/etc/config/openbox
/opt/open-box/data/
endef

# 2. 构建准备：解压下载的对应架构核心资产包至构建目录
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)/_core
	tar -xzf $(DL_DIR)/$(PKG_SOURCE) -C $(PKG_BUILD_DIR)/_core
endef

define Build/Configure
endef

define Build/Compile
endef

# 3. 安装与打包：同时铺装 LuCI 页面、系统脚本以及 /opt/open-box 核心
define Package/luci-app-open-box/install
	# 仅复制 _core 子目录内容，避免递归包含
	$(INSTALL_DIR) $(1)/opt/open-box $(1)/opt/open-box/data  $(1)/www
	$(CP) $(PKG_BUILD_DIR)/_core/* $(1)/opt/open-box/
	chmod -R 0755 $(1)/opt/open-box/bin $(1)/opt/open-box/node/bin $(1)/opt/open-box/openwrt/bin 2>/dev/null || true
	chmod +x $(1)/opt/open-box/update.sh $(1)/opt/open-box/uninstall.sh 2>/dev/null || true
	echo "3036" > $(1)/opt/open-box/data/panel-port

	$(CP) $(1)/opt/open-box/openwrt/luci/root/* $(1)/
	$(CP) $(1)/opt/open-box/openwrt/luci/htdocs/* $(1)/www/

	# 安装系统服务与默认配置
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) $(1)/opt/open-box/openwrt/initd/openbox $(1)/etc/init.d/openbox
	$(INSTALL_BIN) $(1)/opt/open-box/openwrt/initd/openbox-panel $(1)/etc/init.d/openbox-panel

	# 创建 CLI 管理命令软链接（直接指向 /opt/open-box/openwrt/bin/open-box 原版脚本）
	$(INSTALL_DIR) $(1)/usr/bin
	ln -sf /opt/open-box/openwrt/bin/open-box $(1)/usr/bin/open-box
endef

define Package/luci-app-open-box/extra_provides
    echo "libc.musl-x86_64.so.1";
endef

define Package/luci-app-open-box/prerm
#!/bin/sh
[ -n "$${IPKG_INSTROOT}" ] || {
	/etc/init.d/openbox stop >/dev/null 2>&1 || true
	/etc/init.d/openbox-panel stop >/dev/null 2>&1 || true
}
exit 0
endef

define Package/luci-app-open-box/postinst
#!/bin/sh
[ -n "$${IPKG_INSTROOT}" ] || {
	rm -rf /tmp/luci-*cache* 2>/dev/null || true
	/etc/init.d/rpcd reload >/dev/null 2>&1 || true
	/etc/init.d/openbox-panel restart >/dev/null 2>&1 || true
}
exit 0
endef

# 6. 包卸载后钩子：清理残留静态资源与防火墙规则
define Package/luci-app-open-box/postrm
#!/bin/sh
[ -n "$${IPKG_INSTROOT}" ] || {
	rm -rf /tmp/luci-*cache* 2>/dev/null || true
	uci -q delete firewall.openbox_panel || true
	uci -q delete firewall.openbox_dns || true
	uci -q delete firewall.openbox_tun_forward || true
	uci -q delete firewall.openbox_tun_input || true
	uci -q delete firewall.openbox_v6block || true
	for _ob_rule in $$(uci -q show firewall 2>/dev/null | sed -n 's/^firewall\.\(openbox_srv_[A-Za-z0-9_]*\)=rule$$/\1/p'); do
		uci -q delete "firewall.$$_ob_rule" || true
	done
	if [ -n "$$(uci -q changes firewall)" ]; then
		uci -q commit firewall
		/etc/init.d/firewall reload >/dev/null 2>&1 || true
	fi
}
exit 0
endef

$(eval $(call BuildPackage,luci-app-open-box))
