#!/bin/sh

. /lib/functions.sh

add_bridge_option() {
    local section="$1"
    config_get dev "$section" "dev" ""
    config_get to_bridge "$section" "to_bridge" ""
    config_get type "$section" "type" ""
    if [ -z "${dev##tap*}" ] && [ -z "$to_bridge" ]; then
        uci_set "openvpn" "$section" "to_bridge" "br_lan"
        [ -n "$type" ] && {
            uci_set "openvpn" "$section" "dev" "tap_${type:0:1}_${section}"
            uci_add_list "network" "br_lan" "ports" "tap_${type:0:1}_${section}"
        }
    fi
}

remove_tap0() {
    uci_remove_list "network "$1" "ports" "tap0"
}

config_load "openvpn"
config_foreach add_bridge_option "openvpn"
uci_commit openvpn

config_load "network"
config_foreach remove_tap0 "device"
uci_commit network

exit 0