#!/bin/sh
[ "${PKG_UPGRADE}" = 1 ] && exit 0
[ -z "${IPKG_INSTROOT}" ] || exit 0

echo "Removing firewall rules..."
count=$(( $(uci show firewall | grep -c "=rule") - 1 ))
for i in $(seq "$count" -1 0); do
   name=$(uci get firewall.@rule["$i"].name)
   if [ "$name" = "Net 44 ICMP Echo Request" ] || [ "$name" = "Net 44 Router ICMP" ] || [ "$name" = "ipip" ]; then
      uci del firewall.@rule["$i"]
   fi
done
uci commit firewall

echo "Removing network rules..."
count=$(( $(uci show network | grep -c "=rule") - 1 ))
for i in $(seq "$count" -1 0); do
   lookup=$(uci get network.@rule["$i"].lookup)
   if [ "$lookup" = "44" ]; then
      uci del network.@rule["$i"]
   fi
done
uci commit network

echo "Removing firewall zone forwarding rules..."
count=$(( $(uci show firewall | grep -c "=forwarding") -1 ))
for i in $(seq "$count" -1 0); do
   name=$(uci get firewall.@forwarding["$i"].src)
   if [ "$name" = "amprlan" ] || [ "$name" = "amprwan" ]; then
      uci del firewall.@forwarding["$i"]
   fi
done
uci commit firewall

echo "Removing firewall zones..."
count=$(( $(uci show firewall | grep -c "=zone") -1 ))
for i in $(seq "$count" -1 0); do
   name=$(uci get firewall.@zone["$i"].name)
   if [ "$name" = "amprlan" ] || [ "$name" = "amprwan" ]; then
      uci del firewall.@zone["$i"]
   fi
done
uci commit firewall

echo "Removing network interfaces..."
uci del network.amprwan
uci del network.amprlan
uci commit network

