# Exclude MBIM and QMI network interfaces which a DHCP client wouldn't work on.
grep -E "(mbim|qmi)" /sys"${DEVPATH%/*/*}/uevent" && exit
-# Exit if a network with the network interface already exists.
-uci_network=$(uci show network)
-echo "$uci_network" | grep -q ".device='$DEVICENAME'" && exit
+# If a network with the same name already exists, exit.
+uci get network.wan_"$DEVICENAME" && exit
# Decide on the metric value. Start from 1 and work up to 8.
+uci_network=$(uci show network)
metric=1
while [ $metric -le 8 ]; do
# Break if this metric isn't in use.
# If there are no available metrics, exit with code 1.
[ "$metric" -gt 8 ] && exit 1
-# Decide on the network name. Start from wan1.
-index=1
-while echo "$uci_network" | grep -q "wan$index"; do
- index=$((index + 1))
-done
-
-uci set network.wan$index=interface
-uci set network.wan$index.device="$DEVICENAME"
-uci set network.wan$index.proto='dhcp'
-uci set network.wan$index.peerdns='0'
-uci set network.wan$index.metric="$metric"
+uci set network.wan_"$DEVICENAME"=interface
+uci set network.wan_"$DEVICENAME".device="$DEVICENAME"
+uci set network.wan_"$DEVICENAME".proto='dhcp'
+uci set network.wan_"$DEVICENAME".peerdns='0'
+uci set network.wan_"$DEVICENAME".metric="$metric"
uci commit network
-ifup wan$index
+ifup wan_"$DEVICENAME"