#!/bin/sh
# bring iw connection up. input is ifcfg file. -dm

CONFIG="${1}"

if [ -f "$CONFIG" ]; then
	. "$CONFIG"
	if [ "$ESSID" != "" ] ; then
		iwconfig eth0 essid "$ESSID"
	fi
	if [ "$KEY" != "" ] ; then
		iwconfig eth0 key $KEY
	fi
	pid=`ps -ef | grep udhcpc | grep -v grep | sed 's: *\([0-9][0-9]*\).*:\1:g'`
	if [ "$pid" != "" ]; then
		kill $pid
	fi
	if [ "$IPADDR" != "" ] ; then
		ifconfig="ifconfig eth0 $IPADDR"
		if [ "$NETMASK" != "" ] ; then
			ifconfig="$ifconfig netmask $NETMASK"
		fi
		if [ "$BROADCAST" != "" ] ; then
			ifconfig="$ifconfig broadcast $BROADCAST"
		fi
		$ifconfig
		if [ "$GATEWAY" != "" ] ; then
			route del default gw 
			route add default gw $GATEWAY
		fi
	else
		udhcpc
	fi
else
	echo "network configuration not found: $CONFIG"
fi
