include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-led-scheduler
PKG_VERSION:=0.3.0
PKG_RELEASE:=1
PKG_LICENSE:=MIT

LUCI_TITLE:=LuCI support for LED Scheduler
LUCI_DESCRIPTION:=Schedule LEDs configured by OpenWrt system LED settings.
LUCI_DEPENDS:=+luci-base +rpcd-mod-file
LUCI_PKGARCH:=all

define Package/$(PKG_NAME)/postinst
#!/bin/sh
# Support both old opkg/IPK staging and new apk staging variables.
[ -n "$${IPKG_INSTROOT}$${PKG_INSTROOT}" ] && exit 0

rm -f /tmp/luci-indexcache.*
rm -rf /tmp/luci-modulecache/

chmod 0755 \
	/etc/init.d/led-scheduler \
	/usr/bin/led-control \
	/etc/hotplug.d/ntp/90-led-scheduler \
	2>/dev/null || true

# OpenWrt 23.05 uses /etc/config/ucitrack, while 24.10+ uses
# /usr/share/ucitrack/*.json. Install the legacy trigger dynamically only
# when the old ucitrack configuration exists.
if [ -f /etc/config/ucitrack ]; then
	uci -q delete ucitrack.led_scheduler >/dev/null 2>&1 || true
	uci -q set ucitrack.led_scheduler='led-scheduler'
	uci -q set ucitrack.led_scheduler.init='led-scheduler'
	uci -q commit ucitrack
fi

/etc/init.d/led-scheduler enable 2>/dev/null || true
/etc/init.d/ucitrack restart 2>/dev/null || true
/etc/init.d/rpcd reload 2>/dev/null || true
/etc/init.d/led-scheduler restart 2>/dev/null || true

exit 0
endef

define Package/$(PKG_NAME)/prerm
#!/bin/sh
[ -n "$${IPKG_INSTROOT}$${PKG_INSTROOT}" ] && exit 0

chmod 0755 \
	/etc/init.d/led-scheduler \
	/usr/bin/led-control \
	/etc/hotplug.d/ntp/90-led-scheduler \
	2>/dev/null || true

/usr/bin/led-control clear-cron >/dev/null 2>&1 || true
/usr/bin/led-control on >/dev/null 2>&1 || true
/etc/init.d/led-scheduler disable >/dev/null 2>&1 || true

# Remove the legacy OpenWrt 23.05 ucitrack entry on uninstall.
if [ -f /etc/config/ucitrack ]; then
	uci -q delete ucitrack.led_scheduler >/dev/null 2>&1 || true
	uci -q commit ucitrack
	/etc/init.d/ucitrack restart >/dev/null 2>&1 || true
fi

exit 0
endef

include $(TOPDIR)/feeds/luci/luci.mk

# call BuildPackage - OpenWrt buildroot signature
