# OpenWrt feed package — luci-app-srunc.
#
# **Arch-specific** (not noarch): bundles the prebuilt static-musl `srunc`
# binary alongside the LuCI assets, so `opkg install luci-app-srunc_*.ipk`
# installs everything in one shot. The binary is fetched from the project's
# GitHub Release per-arch tarball and placed at `./files/srunc` before this
# Makefile runs — see `.github/workflows/release.yml` for the CI plumbing.
#
# Locally (rare; CI normally drives this):
#
#   # 1. Drop the prebuilt static-musl binary for your target into ./files/
#   curl -L https://github.com/WangYihang/srunc/releases/latest/download/srunc-x86_64-unknown-linux-musl.tar.gz \
#     | tar -xz -C /tmp/ && cp /tmp/srunc-x86_64-unknown-linux-musl/srunc ./files/srunc
#   # 2. Build with the OpenWrt SDK
#   echo "src-link srunc $(realpath packaging/openwrt)" >> feeds.conf
#   ./scripts/feeds update srunc && ./scripts/feeds install -p srunc luci-app-srunc
#   make package/luci-app-srunc/compile V=s

include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-srunc
PKG_VERSION:=0.6.2
PKG_RELEASE:=1
PKG_LICENSE:=MIT
PKG_MAINTAINER:=Yihang Wang <wangyihanger@gmail.com>

LUCI_TITLE:=srunc (srun campus-portal auth) + LuCI web UI
LUCI_DESCRIPTION:=\
  Generic srun (深澜) captive-portal auth client with a modern LuCI web UI \
  under Services › srunc. Auto-discovers the portal host and ac_id from \
  the captive redirect — most users only need to set username + password. \
  Multi-instance via UCI. Bundles the static-musl `srunc` binary; no \
  separate download needed.
LUCI_DEPENDS:=
# luci.mk defaults LUCI_PKGARCH to "all" unless `src/Makefile` exists.
# Setting it to empty here keeps it from being defaulted, leaving PKGARCH
# unset → luci.mk falls through and BuildPackage picks the SDK's target
# arch, so we produce one .ipk per architecture (each bundling its own
# musl binary) instead of a single noarch package.
LUCI_PKGARCH:=

# Absolute path to luci.mk so the package builds from any feed location
# (the standard `include ../../luci.mk` only resolves when located inside
# the luci feed at applications/<name>/Makefile).
include $(TOPDIR)/feeds/luci/luci.mk

# /etc/config/srunc is NOT shipped — the file is generated by postinst
# only when missing, so reinstalls / upgrades can't clobber a user's
# credentials. The conffiles directive stays declared so opkg won't sweep
# the file out from under the user on `opkg remove`.
define Package/luci-app-srunc/conffiles
/etc/config/srunc
endef

# luci.mk unconditionally redefines Package/.../install (user-defined
# version silently overwritten). Default copies root/* → / and htdocs/* →
# /www/, which covers everything we ship — including the per-arch `srunc`
# binary CI stages at root/usr/sbin/srunc.

define Package/luci-app-srunc/postinst
#!/bin/sh
[ -n "$${IPKG_INSTROOT}" ] && exit 0
mkdir -p /usr/share/luci-app-srunc
printf '$(PKG_VERSION)' > /usr/share/luci-app-srunc/VERSION
# Only write a default config if none exists — preserves user creds across
# reinstall / upgrade regardless of opkg conffile quirks.
if [ ! -f /etc/config/srunc ]; then
	cat > /etc/config/srunc <<'EOF'
# srunc — srun (深澜) captive-portal auth. Most users only need username
# + password; host and ac_id are auto-discovered from the captive redirect.
# Add more `config srunc '<name>'` sections to run multiple instances.

config globals 'globals'
	option enabled '1'

config srunc 'default'
	option enabled '1'
	option username ''
	option password ''
	option profile ''
	option log_level 'info'
	option keepalive '1'
	option campus_only '0'
	option interval '3'
	option retry '2'
EOF
	chmod 0600 /etc/config/srunc
fi
/etc/init.d/srunc enable >/dev/null 2>&1 || true
# reload (SIGHUP) — NOT restart. restart kills rpcd, which evicts every
# LuCI session token and logs the user out. reload makes rpcd re-read its
# ACL / RPC scripts in place.
/etc/init.d/rpcd reload >/dev/null 2>&1 || true
exit 0
endef

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

define Package/luci-app-srunc/postrm
#!/bin/sh
[ -n "$${IPKG_INSTROOT}" ] && exit 0
# /etc/config/srunc is a conffile — left alone on remove so reinstall
# restores the user's credentials. Drop our runtime / cache state, plus
# the rc.d / procd leftovers that `disable` + procd GC sometimes miss on
# OverlayFS (same defensive pattern goauthing uses).
rm -f  /usr/share/luci-app-srunc/VERSION
rm -rf /usr/share/luci-app-srunc
rm -rf /var/run/srunc
rm -f  /etc/rc.d/S95srunc /etc/rc.d/K01srunc
rm -f  /overlay/upper/etc/rc.d/S95srunc /overlay/upper/etc/rc.d/K01srunc
rm -rf /sys/fs/cgroup/services/srunc
rm -f  /tmp/lock/procd_srunc.lock
/etc/init.d/rpcd reload >/dev/null 2>&1 || true
exit 0
endef

# call BuildPackage - OpenWrt buildroot signature
