#!/bin/sh
#
# Starts the onboard PSD spectral-shape signal classifier. Numbered after
# S60maia-httpd since it depends on maia-httpd already
# serving /waterfall — see app/classifier/classifier.c for what it does.

NAME=classifier
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME.pid
TEMPLATES="/mnt/jffs2/classifier/templates.bin"
SEED="/etc/classifier-default-templates.bin"

[ -x "$DAEMON" ] || exit 0

start() {
	printf "Starting classifier: "
	# See S51wireguard's comment on why fw_printenv is bounded here — a
	# slow/unavailable U-Boot env backing device must degrade to
	# "disabled" rather than hang the whole boot sequence.
	if [ "$(timeout 3 fw_printenv -n classifier_enabled 2>/dev/null)" != "1" ]; then
		echo "disabled"
		return 0
	fi
	# Seed a small starter set on first boot (FM broadcast, DAB+, CW/
	# beacon — synthesized canonical shapes, see tools/classifier_templates/
	# synthesize_template.py) so the classifier reports something other
	# than "unknown" out of the box, same S59bandplan-style seed-if-
	# missing pattern. A later cmd/classifier/templates push overwrites
	# this permanently, same as S51wireguard/S59bandplan's persistence.
	if [ ! -f "$TEMPLATES" ] && [ -f "$SEED" ]; then
		install -D -m 644 "$SEED" "$TEMPLATES"
	fi
	start-stop-daemon -S -q -b -m -p "$PIDFILE" --exec "$DAEMON" -- \
		-f "$TEMPLATES" && echo "OK"
}

stop() {
	printf "Stopping classifier: "
	start-stop-daemon -K -q -p "$PIDFILE" && echo "OK" || echo "Failed"
	rm -f "$PIDFILE"
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start;;
	stop)
		stop;;
	restart)
		restart;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
esac

exit $?
