#!/bin/sh
#
# Copyright (C) 2026 iopsys Software Solutions AB
#
# See LICENSE file for license related information.
#
# Action "Reboot" — reboot the device.
#
# Post-boot hold-off: to avoid a reboot loop when connectivity never recovers
# (and to give a WAN that is merely slow to come up after a reboot time to
# settle), the reboot is skipped while the system uptime is below the hold-off.
# The hold-off (seconds) comes from connmon.globals.reboot_holdoff (default 300;
# 0 disables). Uptime == time since the last reboot.
#
# Positional contract (see RestartInterface):
#   $1 connection  $2 iface  $3 reference (ignored for Reboot)

. /usr/share/connmon/functions.sh

conn="$1"

holdoff="$(uci -q get connmon.globals.reboot_holdoff)"
[ -n "$holdoff" ] || holdoff=300

uptime="$(cut -d. -f1 /proc/uptime 2>/dev/null)"
[ -n "$uptime" ] || uptime=0

if [ "$holdoff" -gt 0 ] 2>/dev/null && [ "$uptime" -lt "$holdoff" ] 2>/dev/null; then
	log_warn "${conn}: suppressed — uptime ${uptime}s < hold-off ${holdoff}s"
	exit 0
fi

log_warn "${conn}: connectivity failure threshold reached, rebooting device"
# Give the log a moment to flush, then reboot.
sync
reboot
exit 0
