#!/bin/sh
# Called by udev (99-opengd77-hotspot.rules) when the OpenGD77 radio is plugged in or removed.
# Restarts MMDVMHost so that it releases the old serial device and opens the new one. --no-block:
# udev must not wait for the restart. During boot nothing is done, Pi-Star starts MMDVMHost itself
# (mmdvmhost.timer, 30 s after boot).
case "$(/bin/systemctl is-system-running 2>/dev/null)" in
	running|degraded) ;;
	*) exit 0 ;;
esac
# After losing the modem MMDVMHost (20250925_PS4) spins at 100 % CPU and ignores SIGTERM; systemd would
# wait 90 s (TimeoutStopSec) before SIGKILL. On remove kill it right away.
if [ "$1" = remove ]; then
	/usr/bin/pkill -KILL -x MMDVMHost
fi
/usr/bin/logger -t opengd77-hotplug "$1: restarting mmdvmhost.service"
/bin/systemctl --no-block restart mmdvmhost.service
exit 0
