#!/bin/ksh -p -x
# $Id: //depot/stable/explorer/tools/ndd#2 $
#
# Explorer Version:
EXP_VERSION=4.0
#

# Source tools functions
. ${EXP_LIB}/exp_tools

# Run if specified to do so
script=${0##*/}
which_gate_exit network $script default all

# Collect info on IP, TCP, UDP and ICMP
SERVICE="ip tcp udp icmp"
for mod in $SERVICE
do
    echo_file netinfo/ndd/$mod.out "Configuration service: $mod"
    echo_file netinfo/ndd/$mod.out "-------------------------"
    get_cmd "/usr/sbin/ndd /dev/$mod \?" netinfo/ndd/$mod.list

    /usr/sbin/ndd /dev/$mod \? | while read line
    do
	parm=`echo "$line" | nawk -F'(' '{print $1}' | sed 's/ //g'`
	rest=`echo "$line" | nawk -F'(' '{print $2}'`
	[ "$parm" = '?' ] && continue

	# Only get info on vars that include "read"
	echo "$rest" | egrep -s -e "read"
	if [ "$?" != 0 ]; then
	    echo_file netinfo/ndd/$mod.out "$line"
	    echo_file netinfo/ndd/$mod.out ""
	    continue
	fi

	# Catch vars that have no read or write access
	echo "$line" | egrep -s -e "\(no "
	if [ "$?" = 0 ]; then
	    echo_file netinfo/ndd/$mod.out "$line"
	    echo_file netinfo/ndd/$mod.out ""
	    continue
	fi

	echo_file netinfo/ndd/$mod.out "$line"
	get_cmd "/usr/sbin/ndd /dev/$mod $parm" netinfo/ndd/$mod/$parm
	cat ${EXP_TARGET}/netinfo/ndd/$mod/$parm.out | echo_file netinfo/ndd/$mod.out
    done
done

# Collect interface data
INTERFACES=`ifconfig -au | nawk -F: '/flags=/ { print $1 }' | sort -u`
CARDS=`echo "${INTERFACES}" | sed -e 's/[0-9].*//g' | sort -u`
for mod in $CARDS
do
    /usr/sbin/ndd -set /dev/$mod instance 0 2>&1 | egrep -s -e "couldn't push"
    [ "$?" = 0 ] && continue

    INSTANCES=`echo "${INTERFACES}" | egrep -e "${mod}" | sed -e "s/${mod}//g"`
    for inst in $INSTANCES
    do
	/usr/sbin/ndd -set /dev/$mod instance $inst 2>&1 | egrep -s -e "operation failed"
	[ "$?" = 0 ] && continue
	echo_file netinfo/ndd/$mod.out "Card Configuration: $mod instance: $inst"
	echo_file netinfo/ndd/$mod.out "-----------------------------------------"
	get_cmd "/usr/sbin/ndd /dev/$mod \?" netinfo/ndd/$mod.$inst/list

	/usr/sbin/ndd /dev/$mod \? | while read line
	do
	    parm=`echo "$line" | nawk -F'(' '{print $1}' | sed 's/ //g'`
	    rest=`echo "$line" | nawk -F'(' '{print $2}'`
	    [ "$parm" = '?' ] && continue

	    # Only get info on vars that include "read"
	    echo "$rest" | egrep -s -e "read"
	    if [ "$?" != 0 ]; then
		echo_file netinfo/ndd/$mod.out "$line"
		echo_file netinfo/ndd/$mod.out ""
		continue
	    fi

	    # Catch vars that have no read or write access
	    echo "$line" | egrep -s -e "\(no "
	    if [ "$?" = 0 ]; then
		echo_file netinfo/ndd/$mod.out "$line"
		echo_file netinfo/ndd/$mod.out ""
		continue
	    fi

	    echo_file netinfo/ndd/$mod.out "$line"
	    get_cmd "/usr/sbin/ndd /dev/$mod $parm" netinfo/ndd/$mod.$inst/$parm
	    cat ${EXP_TARGET}/netinfo/ndd/$mod.$inst/$parm.out | echo_file netinfo/ndd/$mod.out
	done
    done
done
