#!/bin/sh

LANG=C
timeout=180

if [ ! "$1" ]
then
	exit 0
fi

iface=$1

if [ "$2" ]
then
	timeout=$2
fi

start=`date +%s`
found=0

# loop until the interface is found or the timeout exceeds 
while [ "`date +%s`" -lt $(($start+$timeout)) -a $found -eq 0 ]
do
	# addr=`ip addr show $1 2> /dev/null | grep inet | head -n1`
	addr=`ifconfig $iface 2> /dev/null | grep "inet addr" | awk '{print $2;}' |  awk -F':' '{print $2;}'`
	if [ "$addr" ]
	then
		found=1
	fi
	sleep 0.5
done

if [ $found -eq 1 ]
then
	# the interface is up
	exit 0
else
	# timeout exceeded
	exit 1
fi
