#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2010 OpenWrt.org

START=46

stop() {
	rmmod nbptx
}

nbptc_add_lan_if() {
	local cfg="$1"
	local ifname
	[ "$cfg" = "vwan" ] && {
			return 0
	}
	config_get ifname "$cfg" ifname
	nbptc up $ifname "lan"
}

start() {
	local lan_mac=$(uci get network.lan.macaddr)
	local l2tp=$(uci get l2tp-global.@global[0].netbios_passthrough)
	local pptp=$(uci get pptp-global.@global[0].netbios_passthrough)
	local ovpn="on"
	local val=0
	[ "$pptp" == "on" ] && val=$(($val | 1))
	[ "$l2tp" == "on" ] && val=$(($val | 2))
	[ "$ovpn" == "on" ] && val=$(($val | 4))
	if [ "$val" == "0" ] ; then
		stop
	else
		insmod nbptx
		nbptc mac $lan_mac
		nbptc enable $val

		#maybe vpn interface aready up
		all_files=`ls /tmp/ppp/pid`
		for file in $all_files
		do
			ifname=`cat /tmp/ppp/pid/$file | grep "ifname" | sed 's/^\ *option\ ifname\ //'`
			proto=`cat /tmp/ppp/pid/$file | grep 'configpath' | sed 's/^\ *option\ configpath\ \/tmp\///' | awk -F '/' '{print $3}'`
			if [ "$proto" == "pptp" ] ; then
				nbptc up $ifname "pptp"
			elif [ "$proto" == "l2tp" ] ; then
				nbptc up $ifname "l2tp"
			fi
		done
		config_load vnetwork
		config_foreach nbptc_add_lan_if vinterface
	fi
}


restart() {
	stop
	start
}
