#!/bin/sh
# Copyright (c) 2015-2025 Dirk Brenken (dev@brenken.org)
# This is free software, licensed under the GNU General Public License v3.

# (s)hellcheck exceptions
# shellcheck disable=all

export LC_ALL=C
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"

config="banip"
old_options="ban_loginput ban_logforwardwan ban_logforwardlan ban_blockinput ban_blockforwardwan ban_blockforwardlan ban_blocktype ban_blockpolicy"

for option in ${old_options}; do
	old_values="$(uci -q get "${config}.global.${option}" 2>/dev/null)"
	for value in ${old_values}; do
		case "${option}" in
			"ban_loginput" | "ban_logforwardwan")
				uci -q set "${config}".global.ban_loginbound="${value}"
			;;
			"ban_logforwardlan")
				uci -q set "${config}".global.ban_logoutbound="${value}"
			;;
			"ban_blockpolicy")
				if printf "%s" "${old_values}" | grep -qw "input\|forwardwan\|forwardlan"; then
					break
				else
					continue 2
				fi
			;;
		esac
	done
	uci -q delete "${config}.global.${option}"
done
[ -n "$(uci -q changes "${config}")" ] && uci -q commit "${config}"
exit 0
