#!/bin/sh
#
# Copyright (C) 2026 iopsys Software Solutions AB
#
# See LICENSE file for license related information.
#

. /usr/share/connmon/functions.sh

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

#reference is not supported as of now
if [ -z "${iface}" ]; then
	log_err "${conn}: no interface to renew"
	exit 1
fi

DEV=$(uci -q get network.${iface}.device)
if [ -z "${DEV}" ]; then
	PROTO=$(ubus -S call network.interface."${iface}" status | jsonfilter -e '@.proto')
	if [ "${PROTO}" = "dhcp" ] || [ "${PROTO}" = "dhcpv6" ]; then
		log_warn "${conn}: no device configured, best-effort renew on ${iface}"
		ubus -S call network.interface."${iface}" renew 2>/dev/null
	fi
else
	for i in $(ubus -S call network.interface dump | \
           jsonfilter -e "@.interface[@.device='${DEV}' || @.l3_device='${DEV}'].interface" | \
           sort -u); do
		PROTO=$(ubus -S call network.interface."$i" status | jsonfilter -e '@.proto')
		if [ "${PROTO}" = "dhcp" ] || [ "${PROTO}" = "dhcpv6" ]; then
			log_warn "${conn}: best-effort renew on ${i}"
			ubus -S call network.interface."$i" renew 2>/dev/null
		fi
	done
fi
