#! /bin/bash

# default libexecdir used to bootstrap scripts
libexecdir=${GLOBUS_LOCATION}/libexec

# load GRIS common code and initialization
. ${libexecdir}/grid-info-net-common

#########################################################################

process_ifconfig_line ()
{
    case "$1:$2:$3" in
	0:*:*)
	    if [ "$3" = "Link" ]; then
	    	eval "_net_${i}_name=\"$2\""
	    else
	    	eval "_net_${i}_name=\"$2:$3\""
	    fi
	    eval "_net_${i}_up="
	    ;;
	1:inet:addr)
	    while [ $# -gt 0 ]
	    do
		case "$1" in
		    addr)
			addr="$2"
			shift
			;;
		    Mask)
			mask="$2"
			shift
			;;
		esac
		shift
	    done
	    eval "_net_${i}_ipaddr=$addr"
	    IFS="${IFS_save}."
	    netaddr=`compute_netaddr ${addr} $mask`
	    IFS="${IFS_save}"
	    eval "_net_${i}_netaddr=\"${netaddr}\""
	    ;;
	2:*)
	    while [ $# -gt 0 ]
	    do
		case "$1" in
		    UP)
			eval "_net_${i}_up=up"
			;;
		    MTU)
			eval "_net_${i}_mtu=$2"
			shift
			;;
		esac
		shift
	    done
	    eval "up=\${_net_${i}_up}"
	    if [ "X$up" = "Xup" ]
	    then
		i=`$EXPR $i + 1`
	    fi
	    ;;
    esac
}

probe_resource_net ()
{
    ${LINUX_IFCONFIG} | \
    grep -E '(^$)|(Link encap:)|(inet addr)|(MTU)|(UP)|(RX packets)' | \
    {
	i=0
	lineno=0
	while read line
	do
	    if [ "X$line" = "X" ]
	    then
		lineno=0
	    else
		IFS="${IFS_save}:"
		process_ifconfig_line $lineno $line
		IFS="${IFS_save}"
		lineno=`$EXPR $lineno + 1`
	    fi
	done
	_net_count=$i

	# run this in initialized subshell environment
	emit_resource_net_descriptions
    }
}

#############
# do the work
probe_resource_net
