#!/bin/sh
# SPDX-License-Identifier: MIT

config_file_path="/etc/atmark/els31.conf"

if [ ! -e "$config_file_path" ]; then
        exit 0
fi

. "$config_file_path"

els31_firewall_change() {
	case "$1" in
	usb0|ttyCommModem)
		case "$2" in
		dhcp4-change|vpn-up)
			nmcli device | grep ttyCommModem -q
			if [ $? -eq 0 ]; then
				if [ -z "$FIREWALL" ]; then
					exit 0;
				elif [ "$FIREWALL" = "disable" ]; then
					els31-firewall-disable
				elif [ "$FIREWALL" = "enable" ]; then
					els31-firewall-enable
				fi
			fi
			;;
		esac
		;;
	esac
}

overlays="$(awk -F= '$1 == "fdt_overlays" { print $2 }' /boot/overlays.txt 2>/dev/null)"
case "$overlays" in
*"armadillo-iotg-a6e-els31.dtbo"*)
	els31_firewall_change "$1" "$2"
;;
esac
