#!/bin/sh

[ -z "$ROOT" ] && ROOT=/www
CONFIG=$ROOT/config
SCRIPT=$ROOT/script

source $CONFIG/ap.conf
source $CONFIG/ng.conf

source $SCRIPT/utils/network.sh

setupVlan() {
  local vlan="$1"
  local key="network.vlan${vlan}"
  if [ -z "`uci get $key 2>/dev/null`" ] ; then
    uci set $key=interface
    uci set $key.type=bridge
    uci set $key.proto=dhcp
    uci set $key.defaultroute=0

    eval local proto="\$VLAN${vlan}_PROTO"
    eval local vlanIp="\$VLAN${vlan}_IP"
    eval local vlanGateway="\$VLAN${vlan}_GATEWAY"
    if [ "$proto" == static ] ; then
      uci set $key.proto=static
      eval uci set $key.netmask="\$VLAN${vlan}_NETMASK"
      uci set $key.ipaddr="$vlanIp"
      uci set $key.gateway="$vlanGateway"
    fi

    if [ "$WAN_VLAN" == "$vlan" ] ; then
      uci set $key.defaultroute=1
    fi

    local inf="`getVlanInf $vlan`"
    uci add_list "$key.ifname=$inf"

    local uciKey="dhcp.vlan${vlan}"
    uci -q del $uciKey
    eval local dhcp="\$VLAN${vlan}_DHCP"
    if [ "$proto" == static -a "$dhcp" == 1 ] ; then
      uci set $uciKey=dhcp
      uci set $uciKey.instance=main
      uci set $uciKey.interface="vlan$vlan"
      uci set $uciKey.force=1

      eval local dhcpStart="\$VLAN${vlan}_DHCP_START"
      eval local dhcpLimit="\$VLAN${vlan}_DHCP_LIMIT"
      eval local dhcpLeaseTime="\$VLAN${vlan}_DHCP_LEASETIME"
      eval local dhcpLeaseTimeUnit="\$VLAN${vlan}_DHCP_LEASETIME_UNIT"

      [ -n "$dhcpStart" ] && uci set $uciKey.start="$dhcpStart" || uci set $uciKey.start=50
      [ -n "$dhcpLimit" ] && uci set $uciKey.limit="$dhcpLimit" || uci set $uciKey.limit=100
      [ -n "$dhcpLeaseTime" -a -n "$dhcpLeaseTimeUnit" ] && uci set $uciKey.leasetime="${dhcpLeaseTime}${dhcpLeaseTimeUnit}"

      eval local dhcpDns1="\$VLAN${vlan}_DHCP_DNS1"
      eval local dhcpDns2="\$VLAN${vlan}_DHCP_DNS2"
      eval local dhcpGateway="\$VLAN${vlan}_DHCP_GATEWAY"
      local dnsIps=""
      [ -z "$dhcpDns1" ] || dnsIps="${dnsIps},$dhcpDns1"
      [ -z "$dhcpDns2" ] || dnsIps="${dnsIps},$dhcpDns2"
      [ -z "$dnsIps" -a ! -z "$vlanIp" ] && dnsIps=",${vlanIp}"
      [ -z "$dhcpGateway" ] && dhcpGateway="$vlanGateway"

      [ -z "$dhcpGateway" ] || uci add_list $uciKey.dhcp_option="3,${dhcpGateway}"
      [ -z "$dnsIps" ] || uci add_list $uciKey.dhcp_option="6${dnsIps}"
    fi
  fi

  local uciKey="network.sv${vlan}"
  uci -q del $uciKey
  if [ "$HAS_SWITCH" == true ] ; then
    uci set $uciKey=switch_vlan
    uci set $uciKey.device=switch0
    uci set $uciKey.ports="${CPU_SWITCH_PORT:-0}t ${WAN_SWITCH_PORT}t"
    uci set $uciKey.vlan=$vlan
  fi
}

echo > /etc/config/network

uci set network.loopback=interface
uci set network.loopback.ifname='lo'
uci set network.loopback.proto='static'
uci set network.loopback.ipaddr='127.0.0.1'
uci set network.loopback.netmask='255.0.0.0'

# NETWORK - SWITCH
if [ "$HAS_SWITCH" == true ] ; then
  uciKey=network.switch
  uci set $uciKey=switch
  uci set $uciKey.name=switch0
  uci set $uciKey.reset=1
  uci set $uciKey.enable_vlan=1

  uciKey=network.svwan
  uci set $uciKey=switch_vlan
  uci set $uciKey.device=switch0
  uci set $uciKey.ports="${CPU_SWITCH_PORT:-0}t ${WAN_SWITCH_PORT}"
  wanEthVlan=`echo $WAN_ETH | cut -d "." -f 2`
  uci set $uciKey.vlan=$wanEthVlan

  uciKey=network.svlan
  uci set $uciKey=switch_vlan
  uci set $uciKey.device=switch0
  uci set $uciKey.ports="${CPU_SWITCH_PORT:-0}t ${LAN_SWITCH_PORT}"
  lanEthVlan=`echo $LAN_ETH | cut -d "." -f 2`
  uci set $uciKey.vlan=$lanEthVlan
fi

# NETWORK - VLAN


# NETWORK - WAN
uciKey=network.wan
uci -q del $uciKey
if [ ! -z "$WAN_ETH" ] ; then
  uci set $uciKey=interface
  uci set $uciKey.type=bridge
  uci set $uciKey.proto=dhcp
  uci set $uciKey.defaultroute=1

  if [ "$WAN_PROTO" == static ] ; then
    uci set $uciKey.proto=static
    uci set $uciKey.ipaddr="$WAN_IP"
    uci set $uciKey.netmask="$WAN_NETMASK"
    uci set $uciKey.gateway="$WAN_GATEWAY"
  elif [ "$WAN_PROTO" == pppoe ] ; then
    uci set $uciKey.proto=pppoe
    uci set $uciKey.username="$WAN_PPP_USERNAME"
    uci set $uciKey.password="$WAN_PPP_PASSWORD"
  fi
  uci add_list $uciKey.ifname="$WAN_ETH"

  [ -z "$WAN_VLAN" ] || uci set $uciKey.defaultroute=0
fi

# NETWORK - LAN 0
uciKey=network.lan
uci -q del $uciKey
if [ "$LAN2_BRIDGE" == 1 ] && [ ! -z "$LAN_ETH" ] ; then
  if [ ! -z "$LAN2_VLAN" ] ; then
    setupVlan $LAN2_VLAN
    uci add_list network.vlan$LAN2_VLAN.ifname="$LAN_ETH"
  else
    uci add_list network.wan.ifname="$LAN_ETH"
  fi
else
  uci set $uciKey=interface
  uci set $uciKey.ifname="$LAN_ETH"
  uci set $uciKey.proto=static
  if [ -n "$LAN2_IP" ] ; then
    uci set $uciKey.ipaddr="$LAN2_IP"
    uci set $uciKey.netmask="$LAN2_NETMASK"
  else
    uci set $uciKey.ipaddr="192.168.102.1"
    uci set $uciKey.netmask="255.255.255.0"
  fi
fi

# NETWORK - DEBUG
uciKey=network.debug
uci -q del $uciKey
uci set $uciKey=interface
uci set $uciKey.type=bridge
uci set $uciKey.ifname=debug0
uci set $uciKey.proto=static
uci set $uciKey.ipaddr=10.69.69.1
uci set $uciKey.netmask=255.255.255.0


# NETWORK - STATIC ROUTES
echo "$STATIC_ROUTES" | tr ";" "\n" | awk -F':' '{
  if ($1 == "") return;
  key = "network.route"NR;
  system("uci set "key"=route");
  system("uci set "key".target="$1);
  system("uci set "key".netmask="$2);
  system("uci set "key".gateway="$3);
  system("uci set "key".interface="$4);
}'

ssids=`seq 1 "$NO_SSID"`
for ssid in $ssids; do
  sh $SCRIPT/setup/setup-network-ssid.sh $ssid

  eval vlan="\$SSID${ssid}_VLAN"
  [ ! -z "$vlan" -a ! -z "$WAN_ETH" ] && setupVlan $vlan
done