#!/bin/sh
json="/usr/sbin/jsoner"
file="/work/config.json"

subnet_mask_to_cidr() {
    local subnet_mask="$1"
    local cidr=0

    # 1
    for octet in $(echo "$subnet_mask" | tr '.' ' '); do
        binary=$(printf "%08d" $(echo "obase=2;$octet" | bc))
        cidr=$((cidr + $(echo -n "$binary" | tr -d '0' | wc -c)))
    done

    echo "$cidr"
}
#==================================
#LAN1-ip
#==================================

ip=`$json -g $file "eth0.ip"`
mask=`$json -g $file "eth0.mask"`

cidr=$(subnet_mask_to_cidr "$mask")
crudini --set   /etc/systemd/network/eth1.network Network Address $ip/$cidr

gate=`$json -g $file "eth0.gate"`
echo "$gate"
crudini --set   /etc/systemd/network/eth1.network Route Gateway $gate
route add default gw $gate

#=================================
#LAN2-ip
#=================================

ip=`$json -g $file "eth1.ip"`
mask=`$json -g $file "eth1.mask"`

cidr=$(subnet_mask_to_cidr "$mask")
crudini --set   /etc/systemd/network/eth2.network Network Address $ip/$cidr

gate=`$json -g $file "eth1.gate"`
echo "$gate"
crudini --set   /etc/systemd/network/eth2.network Route Gateway $gate

#4g
#if [ ${VALID_4G} = "true" ]
#then
#	$start4G &
#fi

