#!/bin/bash

# This script detect the WAN interface and writes /var/run/interface
# Author: Stephan Linke, Robert Allmeroth

. /etc/sysconfig/rc.conf
. /etc/sysconfig/network.conf
##. /proc/fiad/hw-info

`echo export $(envtool | grep ethaddr)` > /dev/null
export DEFAULT_MAC=$ethaddr
`echo export $(envtool | grep eth1addr)` > /dev/null
export DEFAULT_EXTERNAL_MAC=$eth1addr


. /etc/image_features

write_interfaces_info ()
{
        echo "PHYS_LAN=lan0"						> /var/run/interfaces
	echo "PHYS_WAN=$WAN_IF"						>> /var/run/interfaces
        echo "VIRT_LAN=lan0"						>> /var/run/interfaces
	echo "VIRT_WAN=$VIRT_WAN"					>> /var/run/interfaces
	echo "WAN_TYPE=$WAN_TYPE"					>> /var/run/interfaces
	echo "WAN_DEV=$WAN_DEV"						>> /var/run/interfaces
	echo "WAN_PROTOCOL=$WAN_PROTOCOL"				>> /var/run/interfaces
	echo "IPPHONE_PNP=$IPPHONE_PNP"					>> /var/run/interfaces
	echo "IPPHONE_WORKING=$IPPHONE_WORKING"				>> /var/run/interfaces

	# store original MAC addresses
	# just create file for now (before we can understand where to get the original MAC address from)
	touch /tmp/ori_mac_addrs
	echo "lan0=$DEFAULT_MAC"						 > /tmp/ori_mac_addrs
	echo "external=$VIRT_DEFAULT_EXTERNAL_MAC"				 >> /tmp/ori_mac_addrs
	if [ "x$WAN_IF" != "x" ] ; then
		echo "$WAN_IF=$VIRT_DEFAULT_EXTERNAL_MAC"			 >> /tmp/ori_mac_addrs
	fi
}

# eth - ethernet is used
# These values are set only for format compatibility 
# with other products
WAN_TYPE="eth"
WAN_DEV="eth"
WAN_PROTOCOL="eth"

if [ "$START_PPP" = "true" ] ; then
	WAN_PROTOCOL="pppoe"

	if [ "$PPP_TYPE" = "pppoa" ] ; then
		WAN_PROTOCOL="pppoa"
	fi
fi

if [ "$START_PPTP_UPLINK" = "true" ] ; then
	WAN_PROTOCOL="pptp"
fi

VIRT_DEFAULT_EXTERNAL_MAC=$DEFAULT_EXTERNAL_MAC

case "$WAN_PROTOCOL" in
	eth)
                WAN_IF="wan0"
                VIRT_WAN="wan0"

		if [ "$START_PBX_ONLY_MODE" = "true" ] ; then
			IPPHONE_PNP="wan0"
			IPPHONE_WORKING="wan0"
		else
                        IPPHONE_PNP="lan0"
                        IPPHONE_WORKING="lan0"
		fi

		if [ "$FEATURE_VLAN" = "1" ] ; then 
			if [ "$IPLINES_VLAN_ID" != "" ] ; then
				IPPHONE_WORKING="$IPPHONE_WORKING.$IPLINES_VLAN_ID"
			fi
		fi
		;;

	pppoe)
		WAN_IF="wan0"
		VIRT_WAN="ppp0"
		if [ "$START_PBX_ONLY_MODE" = "true" ] ; then
			IPPHONE_PNP="wan0"
			IPPHONE_WORKING="wan0"
		else
				IPPHONE_PNP="lan0"
				IPPHONE_WORKING="lan0"
		fi
		if [ "$FEATURE_VLAN" = "1" ] ; then 
			if [ "$IPLINES_VLAN_ID" != "" ] ; then
				IPPHONE_WORKING="$IPPHONE_WORKING.$IPLINES_VLAN_ID"
			fi
		fi
		;;
	pppoa)
		WAN_IF=""
		VIRT_WAN="ppp0"
		VIRT_DEFAULT_EXTERNAL_MAC=""
		;;
	pptp)
		WAN_IF="wan0"
		VIRT_WAN="ppp0"
		if [ "$START_PBX_ONLY_MODE" = "true" ] ; then
			IPPHONE_PNP="wan0"
			IPPHONE_WORKING="wan0"
		else
				IPPHONE_PNP="lan0"
				IPPHONE_WORKING="lan0"
		fi
		if [ "$FEATURE_VLAN" = "1" ] ; then 
			if [ "$IPLINES_VLAN_ID" != "" ] ; then
				IPPHONE_WORKING="$IPPHONE_WORKING.$IPLINES_VLAN_ID"
			fi
		fi
		;;
esac


case "$1" in
if)
	echo $WAN_IF;
	exit 0;
	;;

type)
	echo $WAN_TYPE;
	exit 0;
	;;

dev|device)
	echo $WAN_DEV;
	exit 0;
	;;

h|help)
	echo "Usage: $0 {if|type|device}"
	exit 0
	;;

esac

write_interfaces_info
