#!/bin/sh
#
# Copyright (C) 2011-2012 OpenWrt.org
#

[ -e /etc/config/network ] && exit 0

set_atm_wan() {
	local vpi=$1
	local vci=$2
	local encaps=$3
	local payload=$4
	uci batch <<EOF
set network.atm='atm-bridge'
set network.atm.vpi='$vpi'
set network.atm.vci='$vci'
set network.atm.encaps='$encaps'
set network.atm.payload='$payload'
set network.wan='interface'
set network.wan.ifname='nas0'
set network.wan.proto='pppoe'
set network.wan.username='foo'
set network.wan.password='bar'
set network.wan.ipv6='1'
set network.wan6='interface'
set network.wan6.ifname='@wan'
set network.wan6.proto='dhcpv6'
EOF
}

set_adsl_modem() {
	local annex=$1
	local firmware=$2
	uci batch <<EOF
set network.dsl='adsl'
set network.dsl.annex='$annex'
set network.dsl.firmware='$firmware'
EOF
}

set_vdsl_modem() {
	local annex=$1
	local firmware=$2
	local tone=$3
	local xfer_mode=$4
	uci batch <<EOF
set network.dsl='vdsl'
set network.dsl.annex='$annex'
set network.dsl.firmware='$firmware'
set network.dsl.tone='$tone'
set network.dsl.xfer_mode='$xfer_mode'
EOF
}

. /lib/functions/uci-defaults.sh
. /lib/functions/lantiq.sh

touch /etc/config/network

ucidef_set_interface_loopback
ucidef_set_interface_lan 'eth0'

vpi=1
vci=32
annex="a"
encaps="llc"
payload="bridged"
lan_mac=""
wan_mac=""

board=$(lantiq_board_name)

case "$board" in
# adm6996
ARV4510PW)
	lan_mac=$(mtd_get_mac_ascii uboot_env ethaddr)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "3 2 1 0 5t"
	;;

ARV4520PW)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "3 2 1 0 5t"
	;;

ACMP252|GIGASX76X)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "4 3 2 1 5t"
	;;

# ar8316
ARV4519PW|ARV7510PW22|ARV7518PW|ARV752DPW22)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0t 2 3 4 5"
	;;

# rtl8306g
ARV7519PW)
	lan_mac=$(mtd_get_mac_binary board_config 22)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0 1 2 3 4t"
	;;

WBMR)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0t 2 3 4 5"
	;;

VG3503J)
	ucidef_set_interface_lan 'eth0'
	uci set network.lan.type='none'
	;;

DGN3500*)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "3 2 1 0 5t"
	;;

GR7000)
	ucidef_set_interface_wan "eth0"
	uci set network.lan.ifname=''
	;;

H201L)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0 1 2 3 4t"
	;;

#rtl8306g
P2601HNFX)
	ucidef_set_interface_lan "eth0.1"
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0 1 2 3 5t"
	;;

P2812HNUF*)
	lan_mac=$(mtd_get_mac_ascii uboot-env ethaddr)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	ucidef_set_interface_lan 'eth0.1'
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0 1 2 4 5 6t"
	;;

TDW8970)
	lan_mac=$(mtd_get_mac_binary boardconfig 61696)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	ucidef_set_interface_lan 'eth0.1'
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "0 2 4 5 6t"
	;;

ARV7519RW22)
	lan_mac=$(mtd_get_mac_binary boardconfig 22)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	;;

BTHOMEHUBV2B|BTHOMEHUBV3A)
	lan_mac=$(mtd_get_mac_ascii uboot_env ethaddr)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	;;

VGV7519)
	lan_mac=$(mtd_get_mac_binary board_config 22)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	;;

VGV7510KW22)
	lan_mac=$(mtd_get_mac_binary board_config 22)
	wan_mac=$(macaddr_add "$lan_mac" 1)
	ucidef_set_interface_lan 'eth0.1'
	ucidef_add_switch "switch0" "1" "1"
	ucidef_add_switch_vlan "switch0" "1" "2 3 4 5 6t"
	;;

esac

[ -z "$(ls /lib/modules/`uname -r`/ltq_atm*)" ] || set_atm_wan "$vpi" "$vci" "$encaps" "$payload"

if [ -n "$(grep "system type.*: VR9" /proc/cpuinfo)" ]; then
	set_vdsl_modem "$annex" "/lib/firmware/vdsl.bin" "av" "ptm"
else
	set_adsl_modem "$annex" "/lib/firmware/adsl.bin"
fi

[ -n "$lan_mac" ] && ucidef_set_interface_macaddr lan $lan_mac
[ -n "$wan_mac" ] && ucidef_set_interface_macaddr wan $wan_mac

uci commit network

exit 0
