#!/bin/sh
#
# Copyright (C) 2026 iopsys Software Solutions AB
#
# See LICENSE file for license related information.
#
# Action "RestartInterface" — bounce the monitored interface.
#

. /usr/share/connmon/functions.sh

conn="$1"; iface="$2"; ref="$3"

restart_if() {
	iface="${1}"

	log_warn "${conn}: bouncing interface ${iface}"
	if command -v ifup >/dev/null 2>&1; then
		ifdown "$iface" 2>/dev/null
		sleep 1
		ifup "$iface" 2>/dev/null
	else
		ubus call network.interface."$iface" down 2>/dev/null
		sleep 1
		ubus call network.interface."$iface" up 2>/dev/null
	fi
}

#reference is not supported as of now

if [ -z "${iface}" ]; then
	log_err "${conn}: no interface to restart"
	exit 1
fi

DEV=$(uci -q get network.${iface}.device)
if [ -z "$DEV" ]; then
	restart_if "${iface}"
else
	for i in $(ubus call network.interface dump | \
           jsonfilter -e "@.interface[@.device='$DEV' || @.l3_device='$DEV'].interface" | \
           sort -u); do
		restart_if "${i}"
	done
fi
