#!/bin/bash

set -u

PATH="${PATH}:$(dirname ${BASH_SOURCE[0]}):$(dirname ${BASH_SOURCE[0]})/../lib/lwr/netman"

for interface in $(netman list); do
    static_config="$(netman show ${interface})"

    dev=$(echo "${static_config}" | jq -r '.dev')
    if [[ "${dev}" == "null" ]]; then
        echo "Error: cannot get 'dev' from config for interface ${interface}"
        return 1
    fi

    command="netman-configure-interface --name ${interface} --dev ${dev}"

    vlan_id=$(echo "${static_config}" | jq -r '.vlanid')
    if [[ "${vlan_id}" != "null" ]]; then
        command+=" --vlanid ${vlan_id}"
    fi

    mac_source=$(echo "${static_config}" | jq -r '.macAddressSource')
    if [[ "${mac_source}" != "null" ]]; then
        command+=" --mac ${mac_source}"
    fi

    ${command}
done
