#! /bin/sh
. $TS_GLOBAL

DEVICELIST=$WKDIR/devicelist
if [ -z "$SETNETWORKUP" ] ; then
	NETWORKUP=FALSE
fi

if [ -n "$DEBUG_NETWORK" ] ; then
	debug="-d"
fi

# USB and PCMCIA cards need some time to powerup, so w8 3 secs
sleep 3

# Find wireless devices if iwconfig is available
if [ -x /bin/iwconfig ]; then
	WIRELESS_DEV=`iwconfig 2>&1 | grep -v 'no wireless' | cut -c1-7 | grep -v '^ *$'`
fi

# All devices, not only the *0 devices
(ifconfig -a | cut -c1-7 | grep -v '^ *$' | grep -v lo ) |
while read device
do
  devicetype=`echo $device | cut -c1-2`
  if [ "$devicetype" = "tr" ] ; then
    client_mac=`ifconfig -a $device | grep HWaddr | cut -c 37-38,40-41,43-44,46-47,49-50,52-53,55-56,58-59,61-62,64-65,67-68,70-71,73-74,76-77,79-80,82-83`
  elif [ "$devicetype" = "et" ] || [ "$devicetype" = "at" ] || [ "$devicetype" = "wl" ] ; then
    client_mac=`ifconfig -a $device | grep HWaddr | cut -c 39-40,42-43,45-46,48-49,51-52,54-55`
  fi
  echo $device $client_mac >> $DEVICELIST
done


get_net_hostname()
{
  if [ -n "`echo $NET_HOSTNAME | sed -n '/\*/p'`" ]; then
    CLIENT_NAME=`echo $NET_HOSTNAME | sed "s/\*/$CLIENT_MAC/"`
  else
    CLIENT_NAME=$NET_HOSTNAME
  fi
}

# Manual network configuration

manual_config()
{

echo "CLIENT_IP=$NET_IP_ADDRESS" >> $TS_RUNTIME
echo "nameserver $NET_DNS1" >> /etc/resolv.conf
echo "nameserver $NET_DNS2" >> /etc/resolv.conf
echo "search $NET_DNS_SEARCH" >> /etc/resolv.conf

(cat $DEVICELIST) |
while read device client_mac
do
  if [ "$NETWORKUP" = "FALSE" ] ; then
    if ifconfig $device $NET_IP_ADDRESS netmask $NET_MASK ; then
	NETWORKUP=TRUE

	DEVICE=$device
	CLIENT_MAC=$client_mac
	    
	echo "DEVICE=$device" >> $TS_RUNTIME
	echo "CLIENT_MAC=$client_mac" >> $TS_RUNTIME
	# run wireless config - only works if it's a wireless device
        wireless_config
    fi
  fi
  # Check for celular connection already up
  if [ -n "$SETNETWORKUP" ] ; then
	CLIENT_MAC=$client_mac
  	if [ -n "$NET_HOSTNAME" ] ; then
		get_net_hostname
	else
		CLIENT_NAME=$CLIENT_MAC
  	fi

  	echo "CLIENT_NAME=$CLIENT_NAME" >> $TS_RUNTIME
  	echo "CLIENT_MAC=$CLIENT_MAC" >> $TS_RUNTIME
  
  	hostname $CLIENT_NAME
  fi
done

. $TS_GLOBAL

if [ -z "$DEVICE" ] ; then
  if [ -z "$SETNETWORKUP" ] ; then
    if [ `make_caps "$HALTONERROR"` = "FALSE" ] ; then
     echo_log "Error, Network not initialized. Halt Overrided, Continuing..."
    else
     echo_log "Error, Network not initialized. Halting..."
     halt
    fi
  fi
else

  if [ -n "$NET_HOSTNAME" ] ; then
	get_net_hostname
  else
	CLIENT_NAME=$CLIENT_MAC
  fi

  echo "CLIENT_NAME=$CLIENT_NAME" >> $TS_RUNTIME
  
  route add default gw $NET_GATEWAY
  hostname $CLIENT_NAME
fi
}

wireless_config()
{

IS_WIRELESS="`echo $WIRELESS_DEV | grep $device`"
if [ "$IS_WIRELESS" != "" ]; then
	if [ -n "$WIRELESS_WPAKEY" ] && [ -x /bin/wpa_supplicant ] ; then
	    wpa_passphrase "$WIRELESS_ESSID" "$WIRELESS_WPAKEY" >> /etc/wpa_supplicant.conf.tmp
	    awk '{print $0;if($0=="network={"){print "\tscan_ssid=1"}}' < /etc/wpa_supplicant.conf.tmp > /etc/wpa_supplicant.conf
	    rm -f /etc/wpa_supplicant.conf.tmp
	    wpa_supplicant -Bw -D`make_lower $WIRELESS_DRIVER` -i$device -c/etc/wpa_supplicant.conf
	    sleep 1
	fi
    	# This appears to be a Wireless device (USB/PCI/PCMCIA). Set specific 
    	# options. (Code ripped from pcmcia-cs wireless script)
    	# Mode need to be first : some settings apply only in a specific mode !
    	if [ -n "$WIRELESS_MODE" ] ; then
    		iwconfig $device mode $WIRELESS_MODE
    	fi
    	# This is a bit hackish, but should do the job right...
    	if [ ! -n "$WIRELESS_NICKNAME" ] ; then
    		WIRELESS_NICKNAME=`hostname`
    	fi
    	if [ -n "$WIRELESS_ESSID" -o -n "$WIRELESS_MODE" ] ; then
    		iwconfig $device nick "$WIRELESS_NICKNAME" >/dev/null 2>&1
    	fi
    	# Regular stuff...
    	if [ -n "$WIRELESS_NWID" ] ; then
    		iwconfig $device nwid $WIRELESS_NWID
    	fi
    	if [ -n "$WIRELESS_FREQ" ] ; then
    		iwconfig $device freq $WIRELESS_FREQ
    	elif [ -n "$WIRELESS_CHANNEL" ] ; then
    		iwconfig $device channel $WIRELESS_CHANNEL
    	fi
    	if [ -n "$WIRELESS_SENS" ] ; then
    		iwconfig $device sens $WIRELESS_SENS
    	fi
    	if [ -n "$WIRELESS_RATE" ] ; then
    		iwconfig $device rate $WIRELESS_RATE
    	fi
    	if [ -n "$WIRELESS_KEY" ] && [ ! -n "$WIRELESS_WPAKEY" ] ; then
    		iwconfig $device key $WIRELESS_KEY
    	fi
    	if [ -n "$WIRELESS_RTS" ] ; then
    		iwconfig $device rts $WIRELESS_RTS
    	fi
    	if [ -n "$WIRELESS_FRAG" ] ; then
    		iwconfig $device frag $WIRELESS_FRAG
    	fi
    	# More specific parameters
    	if [ -n "$WIRELESS_IWCONFIG" ] ; then
    		iwconfig $device $WIRELESS_IWCONFIG
    	fi
    	if [ -n "$WIRELESS_IWSPY" ] ; then
    		iwspy $device $WIRELESS_IWSPY
    	fi
    	if [ -n "$WIRELESS_IWPRIV" ] ; then
    		iwpriv $device $WIRELESS_IWPRIV
    	fi
   	# ESSID need to be last : most device re-perform the scanning/discovery
    	# when this is set, and things like encryption keys are better be
    	# defined if we want to discover the right set of APs/nodes.
    	if [ -n "$WIRELESS_ESSID" ] && [ ! -n "$WIRELESS_WPAKEY" ] ; then
    		iwconfig $device essid "$WIRELESS_ESSID"
    	fi
fi
}

echo_log "\n\nConfiguring and Starting Network..." $debug

#Localhost
ifconfig lo up 
ifconfig lo 127.0.0.1
route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
echo -n "lo "

# Checks for booting without dhcp/tftp server

NET_USE_DHCP=`make_caps $NET_USE_DHCP`

#Bring up Network

if [ "$NET_USE_DHCP" = "OFF" ] ; then
    echo_log "Booting with manually configured network..." $debug
    manual_config
else
  (cat $DEVICELIST) |
  while read device client_mac
  do
    if [ "$NETWORKUP" = "FALSE" ] ; then
	IS_WIRELESS="`echo $WIRELESS_DEV | grep $device`"
	# Check if the cable is  connected (not always supported, but will continue in doubt)
	if [ "`ethtool $device | grep 'Link detected: no'`" = "" -o "$IS_WIRELESS" != "" ] ; then

	    ifconfig $device up

	    sleep $NET_DHCP_DELAY
	    wireless_config
	        
	    #Included -n switch. Try only 2 times to get an IP adress.
	    if udhcpc -n -i $device -C -s /etc/init.d/network_script || udhcpc -n -i $device -s /etc/init.d/network_script  ; then
	    NETWORKUP=TRUE

	    DEVICE=$device
	    CLIENT_MAC=$client_mac

	    echo "DEVICE=$device" >> $TS_RUNTIME
	    echo "CLIENT_MAC=$client_mac" >> $TS_RUNTIME
    	fi
      else
	    echo_log "No cable for $device."
      fi
    fi
  done

  . $TS_GLOBAL

  if [ -z "$DEVICE" ] ; then
    if [ "$NET_USE_DHCP" = "BOTH" ] ; then
	echo_log "Booting with manually configured network..." $debug
	manual_config
	wireless_config
    else
	echo_log "DHCP server not found! Network not initialized."
	echo_log "This error has probably occured because you haven't got the corrrect"
	echo_log "module loaded for you network card."
	if pkg_initialized debug ; then
	  echo_log "Debug enabled...continuing boot."
	else
	  echo_log "Debug not enabled, boot halted."
	  halt
	fi
    fi
  fi
fi

# Add MAC Address to .conf file
echo "CLIENT_MAC=$CLIENT_MAC" >> $TS_RUNTIME

# Kernel Network setting
echo 0 > /proc/sys/net/ipv4/tcp_retrans_collapse

rm $DEVICELIST
