#!/bin/sh
#
# Copyright (C) 2014-2020, Lantronix, Inc. All Rights Reserved.
#           7535 Irvine Center Drive, Suite 100
#           Irvine, CA 92618 USA

# Permission to use, copy, modify this software for any
# purpose is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#


# This file is used when backup file from other devices is used and do not
# want to change device specific data/configurations.
# Add parameters as and when it requires in this file.
# This script will run after reboot after applying backup file or the image
# is created using SDK.

CFG=/etc/board.json

. /usr/share/libubox/jshn.sh
. /lib/functions/system.sh
. /lib/functions.sh

[ -s $CFG ] || /bin/board_detect || exit 1

update_original_device_mac() {
    local ifname macaddr protocol type ipaddr netmask metric

    echo "$1"
    
    json_select network
        json_select "$1"
            json_get_vars ifname macaddr protocol ipaddr netmask metric
        json_select ..
    json_select ..
    
    echo "$macaddr"
    
    if [ -n "$macaddr" ]; then     
        uci set network.$1_${ifname/./_}_dev.macaddr="$macaddr" 2>/dev/null
    fi
}

json_init
json_load "$(cat ${CFG})"

json_get_keys keys network
for key in $keys; do update_original_device_mac $key; done

uci commit network

exit 0
