#!/bin/sh

. /lib/functions.sh

migrate_service() {
    local section="$1" proto
    config_get proto "$section" "proto"
    config_get service "$section" "service"
    [ "${proto#gre}" != "$proto" ] || return 0
    [ -n "$service" ] || return 0
    uci_rename "network" "$section" "service" "services"
}

add_service() {
    local section="$1" bind_to service
    config_get bind_to "$section" "bind_to"
    [ -n "$bind_to" ] || return 0
    uci_get network "$bind_to" services | grep -qw "ipsec" || uci_add_list "network" "$bind_to" "services" "ipsec"
}

config_load network
config_foreach "migrate_service" "interface"
config_load ipsec
config_foreach "add_service" "connection"
uci commit network
exit 0
