#!/bin/sh
#set -x

export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

##Commands##

ETHTOOL=`type ethtool | awk '/is/{print $3}'`
LSPCI=`type lspci | awk '/is/{print $3}'`
INSMOD=`type insmod | awk '/is/{print $3}'`
RMMOD=`type rmmod | awk '/is/{print $3}'`
MODPROBE=`type modprobe | awk '/is/{print $3}'`
STAT="/usr/bin/stat"

## end ##

##Global Modules
NX_NIC=`awk '/nx_nic/ {print $1}' /proc/modules`
NX_GPL=`awk '/netxen_nic/ {print $1}' /proc/modules`
UNM_NIC=`awk '/unm_nic/ {print $1}' /proc/modules`
NX_XPORT=`awk '/nx_xport/ {print $1}' /proc/modules`
NX_LSA=`awk  '{print $1}' /proc/modules | grep nx_lsa`

NX_NIC_PATH=""
NX_GPL_PATH=""
UNM_NIC_PATH=""
NX_LSA_PATH=""

NX_NIC_CHECK=0
NX_GPL_CHECK=0
UNM_NIC_CHECK=0
NX_LSA_CHECK=0
## end ##

MYPATH=`readlink -f $0`
MYPATH="${MYPATH%/*}"

##Ethtool Info globals

portoff=0
ports=0;
MAC_BASE=4097048
BOARD_TYPE_BASE=0x4008
FW_VERSION_BASE=0x3e8408
SERIAL_NUM_BASE=0x3E881c
BIOS_VERSION_BASE=0x3E883c
PXE_ADDR=0x3e8840

#CRBINIT_START=2700
CRBINIT_START=0
CRBINIT_LEN=16384
CRBINIT_DUMP="/tmp/crbinit_dump"
brd_string=""

## end ##


get_ports_from_ssid()
{
	case $1 in
		0x00014040)
			ports=1
			brd_string="NXB-10GXSR 10 Gigabit Ethernet PCIe Adapter with SR-XFP optical interface"
			;;
		0x00024040)
			ports=1
			brd_string="NXB-10GCX4 10 Gigabit Ethernet PCIe Adapter with CX4 copper interface"
			;;	
		0x7047103c)
			ports=1
			brd_string="HP NC510F 10G Ethernet Adapter"
			;;
		0x7048103c)
			ports=1
			brd_string="HP NC510C 10G Ethernet Adapter"	
			;;
		0x00034040)
			ports=1
			brd_string="NXB-4GCU Quad Gigabit Ethernet PCIe Adapter with 1000-BASE-T interface"
			;;
		0x170e103c)
			ports=2
			brd_string="BladeCenter-H 10 Gigabit Ethernet High Speed Daughter Card"
			;;
		0x00044040)
			ports=2
			brd_string="NetXen Dual Port 10GbE Multifunction Adapter for C-Class"	
			;;
		0x171b103c)
			ports=2
			brd_string="HP NC522m Dual Port 10GbE Multifunction BL-c Adapter"
			;;
		0x3251103c)
			ports=4
			portoff=128 #(16 * 8)
			brdtype="0x80"
			brd_string="HP NC375i 1G w/HP NC524SFP 10G Module"	
			;;
		0x705a103c)
			ports=4
			brd_string="HP NC375i Integrated Quad Port Multifunction Gigabit Server Adapter"	
			;;

		0x705b103c)
			ports=2
			brd_string="HP NC522SFP Dual Port 10GbE Server Adapter"
			;;

		0x01234040)
			ports=2
			brd_string="NetXen NX3031 Dual Port CX4"
			;;

		0x01244040)
			ports=4
			brd_string="NetXen NX3031 Quad Port Gigabit Server Adapter"
			;;

		0x01264040)
			ports=2
			brd_string="NetXen NX3031 Dual Port SFP+ 10GbE Server Adapter"
			;;
		0x01254040)
			ports=2
			brd_string="IBM Mezzanine Dual Port 10GbE Server Adapter"
			;;

		0x896b152d)
			ports=2
			brd_string="Quanta TG20 Dual Port 10GbE Server/Storage Adapter"
			;;

		0x8970152d)
			ports=2
			brd_string="Quanta XG2 Dual Port 10G-BASE-T  Server/Storage Adapter"	
			;;
		0x1740103c)
			ports=4
			brd_string="NC375T PCI Express Quad Port Gigabit Server Adapter"	
			;;

		*)
			;;
	esac	

}



print_mac()
{
	addr=`expr $1 + $portoff`
	maxports=$2
	port_number=$3
	length=6
	port=0
	while [ $port -lt $maxports ]
	do
	if [ $port -eq $port_number ]; then
		echo -e -n "MAC Addr $port		\t: ` $ETHTOOL -e $ethinterface offset $addr length $length | awk 'BEGIN {OFS=":"} /^0x3/ {print $7,$6,$5,$4,$3,$2}'`\n"
	fi
		addr=`expr $addr + 8`
		port=`expr $port + 1`
	done
}

print_pxe()
{
	port=0
	maxports=$1
	port_number=$2
	pxe_status="0x`$ETHTOOL -e $ethinterface offset $PXE_ADDR length 1 | awk ' /^0x3/ {printf "%x", strtonum("0x"$2) }'`"

	while [ $port -lt $maxports ]
	do
		bit=$(( 1 << $port ))
		value=$(( $pxe_status & $bit ))
		if [ $value -eq $bit ]; then
			if [ $port -eq $port_number ]; then
				echo -e -n "PXE Status $port		\t: Enabled\n"
			fi
		else
			if [ $port -eq $port_number ]; then
				echo -e -n "PXE Status $port		\t: Disabled\n"
			fi
		fi
		port=`expr $port + 1`
	done
}

print_wol()
{
	bit_val=0
	maxports=$1
	port=0
	port_number=$2

	`$ETHTOOL -e $ethinterface offset $CRBINIT_START length $CRBINIT_LEN > $CRBINIT_DUMP`	
	bit_val=`grep "84 21 60 41" $CRBINIT_DUMP`
	if [ "$bit_val" != "" ]; then 
		bit_val=`grep "84 21 60 41" $CRBINIT_DUMP | tr '[:space:]' '\n' | grep -B4 84 | head -n 1| awk '{printf "%d" ,strtonum("0x"$0)}'`
	else
		bit_val=0
	fi

	while [ $port -lt $maxports ]
	do
	bit=$(( 1 << $port ))
        value=$(( $bit_val & $bit ))
	if [ $value -eq $bit ]; then

		if [ $port -eq $port_number ]; then
			echo -e -n "WOL Status $port            \t: Enabled\n"
		fi
	else

		if [ $port -eq $port_number ]; then
			echo -e -n "WOL Status $port            \t: Disabled\n"
		fi
	fi
	port=`expr $port + 1`
	done
	rm -rf $CRBINIT_DUMP
}

print_ncsi()
{
       bit_val=0
       `$ETHTOOL -e $ethinterface offset $CRBINIT_START length $CRBINIT_LEN > $CRBINIT_DUMP`
       bit_val=`grep "b4 20 60 41" $CRBINIT_DUMP`
       if [ "$bit_val" != "" ]; then
               bit_val=`grep "b4 20 60 41" $CRBINIT_DUMP | tr '[:space:]' '\n' | grep -B4 "b4" | head -n 2| awk '{printf "%d" ,strtonum("0x"$1)}'`
       else
               bit_val=0
       fi
       #check for NCSI signature
       if [ "$bit_val" == "81156" ]; then

               echo -e -n "NCSI Status             \t: Enabled\n"
       else

               echo -e -n "NCSI Status             \t: Disabled\n"
       fi
       rm -rf $CRBINIT_DUMP
}

ethtool_info()
{
	ethinterface=$1
	kalium_port=$2
	driver=` $ETHTOOL -i $ethinterface | awk '/^driver:/ {print $2}'`
	if [ "$driver" != "nx_nic" -a "$driver" != "netxen_nic" ]; then
		echo "$ethinterface:Not an Netxen interface"
		exit 0
	fi

	fw_version=` $ETHTOOL -e $ethinterface offset $FW_VERSION_BASE length 4 | awk '/^0x/ {printf "%d.%d.%d\n", $2,$3, strtonum("0x"$5$4)}'`
	brdtype=` $ETHTOOL -e $ethinterface offset $BOARD_TYPE_BASE length 4|awk '/^0x/ {print "0x"$2}'`
	bios_version=` $ETHTOOL -e $ethinterface offset 0x3E883c length 3|awk ' /^0x3/ {printf("%d.%d.%d\n",$2,$3,$4)}'`

	serial_num=` $ETHTOOL -e $ethinterface offset $SERIAL_NUM_BASE length 10|awk ' /^0x3/ {printf("%c%c%c%c%c%c%c%c%c%c",strtonum("0x"$2),strtonum("0x"$3),strtonum("0x"$4),strtonum("0x"$5),strtonum("0x"$6),strtonum("0x"$7),strtonum("0x"$8),strtonum("0x"$9),strtonum("0x"$10),strtonum("0x"$11))}'`

	bus_id=` $ETHTOOL -i $ethinterface |grep bus| cut -d : -f 3`
	pci_string=`$LSPCI -s $bus_id:`
	chip_rev=`echo ${pci_string##*(} | awk '{ printf("0x%x\n",strtonum("0x"$2))}'`
	sysid_1=` $LSPCI -n -s$bus_id: -v | grep Subsystem: | sed -n 1p | awk '/:/ {print $2}' | cut -d: -f2`
	sysid_2=` $LSPCI -n -s$bus_id: -v | grep Subsystem: | sed -n 1p | awk '/:/ {print $2}' | cut -d: -f1`


	ssid="0x$sysid_1$sysid_2"
	get_ports_from_ssid $ssid
	port_number=`$ETHTOOL $1 |grep PHYAD | cut -d : -f2`


	echo -e -n "Board Type			: $brdtype ($brd_string)\n"
	echo -e -n "Chip Rev			: $chip_rev\n"
	echo -e -n "Serial Number		\t: $serial_num\n"
	echo -e -n "BIOS Version		\t: $bios_version\n"
	echo -e -n "Firmware version(on Flash)	: $fw_version\n"
	echo -e -n "SubSystem ID		\t: 0x$sysid_1$sysid_2\n"
	echo -e -n "PCI Bus ID			: 0x$bus_id\n"
	
	if [ "$brdtype" == "0x22" ];
	then
		echo -e -n "MAC Addr              \t \t: "
		ifconfig $ethinterface | awk '/HWaddr/{ print $5 }'
		
		pxe_status="0x`$ETHTOOL -e $ethinterface offset $PXE_ADDR length 1 | awk ' /^0x3/ {printf "%x", strtonum("0x"$2) }'`"
		kalium_port=`expr $kalium_port % 2`
		if [ "$kalium_port" == "0" ]; then
			pport=0
		else
			pport=1
		fi

		bit=$(( 1 << $pport ))
		value=$(( $pxe_status & $bit ))

		if [ $value -eq $bit ]; then

                        echo -e -n "Base PXE Status                 : Enabled\n"
                else

                        echo -e -n "Base PXE Status                 : Disabled\n"
 		fi
	else
		print_mac $MAC_BASE $ports $port_number
		print_pxe $ports $port_number
	fi
	p3=`echo $chip_rev | awk '{ printf "%d", strtonum($0)}'`
	if [ $p3 -ge 65 ]; then
        	if [ "$ssid" != "0x171b103c" ]; then
		      print_wol $ports $port_number
        	fi
                if [ "$ssid" == "0x705b103c" ]; then
                      print_ncsi
                fi

	fi

}

if [ -x $MYPATH/$FLASHPGM ] ; then
	TOOLS_DIR=$MYPATH
else
	TOOLS_DIR=/opt/netxen
fi


if [ "$1" == "mfg" -o "$1" == "MFG" ] ; then
        FLASHPGM=$TOOLS_DIR/'nxflashmfg.bin'
        shift
else
        FLASHPGM=$TOOLS_DIR/'nxflash.bin'
fi



LOCK_FILE=/tmp/.netxen_lock_file

ret_flag=0
nodriver_flag=0
nodowngrade=1
pci_map_fbd=-1
device_string=""
initsetupdone=0
rqmts=0
ethstr=""
pci=""

cmdstring=""
has_ip=0
int_index=0

if [ "$NX_NIC" == "" -a "$NX_GPL" == "" -a "$UNM_NIC" == "" ]; then
	nodriver_flag=1
fi

load_drivers()
{
	restore_driver
	if [ "$NX_GPL" != "" ]; then
		grep -q netxen_nic /proc/modules
		if [ $? -eq 0 ] ; then
			$RMMOD $NX_GPL
		fi	
		$MODPROBE $NX_GPL
		if [ "$?" != "0" ]; then
			echo $NX_GPL "driver failed to load"
		fi
	fi	
	if [ "$NX_NIC" != "" ]; then
		grep -q nx_nic /proc/modules
		if [ $? -eq 0 ] ; then
			$RMMOD $NX_NIC
		fi
		$MODPROBE $NX_NIC
		if [ "$?" != "0" ]; then
			echo $NX_NIC "driver failed to load"
		fi
	fi
	if [ "$UNM_NIC" != "" ]; then
		grep -q nx_nic /proc/modules
		if [ $? -eq 0 ] ; then
			$RMMOD $NX_NIC
		fi	
		$MODPROBE $UNM_NIC
		if [ "$?" != "0" ]; then
			echo $UNM_NIC "driver failed to load"
		fi
	fi
	if [ "$NX_LSA" != "" ]; then
		grep -q nx_lsa /proc/modules
		if [ $? -eq 0 ] ; then
			$RMMOD $NX_LSA
		fi
		$MODPROBE $NX_LSA
		if [ "$?" != "0" ]; then
			echo $NX_LSA "driver failed to load"
		fi
	fi

#removed "has_ip" and "is_up" check as we need to issue ifconfig commands
#in all conditions
	i=0
	echo "Restoring IP addresses and interface status"
	while [ $i -lt $int_index ]
	do
		echo ${cmdstring[$i]}
		`${cmdstring[$i]}`
		i=`expr $i + 1`
	done	
}

restore_driver()
{
	if [[ $NX_LSA_CHECK -eq 1 ]]
	then
		for qlogic_path in $NX_LSA_PATH
		do
			DIRNAME=`dirname ${qlogic_path}`
			mv $DIRNAME/qlogic.nx_lsa.ko $DIRNAME/nx_lsa.ko
	 		NX_LSA_CHECK=0
		done
	fi	
	if [[ $NX_NIC_CHECK -eq 1 ]]
	then
		for qlogic_path in $NX_NIC_PATH
		do
			DIRNAME=`dirname ${qlogic_path}`
			mv $DIRNAME/qlogic.nx_nic.ko $DIRNAME/nx_nic.ko
		 	NX_NIC_CHECK=0
		done
	fi	
	if [[ $UNM_NIC_CHECK -eq 1 ]]
	then
		for qlogic_path in $UNM_NIC_PATH
		do
			DIRNAME=`dirname ${qlogic_path}`
			mv $DIRNAME/qlogic.unm_nic.ko $DIRNAME/unm_nic.ko
		 	UNM_NIC_CHECK=0
		done
	fi	
	if [[ $NX_GPL_CHECK -eq 1 ]]
	then
		for qlogic_path in $NX_GPL_PATH
		do
			DIRNAME=`dirname ${qlogic_path}`
			mv $DIRNAME/qlogic.netxen_nic.ko $DIRNAME/netxen_nic.ko
		 	NX_GPL_CHECK=0
		done
	fi	
	
}
save_driver ()
{
	type=$1
	kernel=$(echo /lib/modules/`uname -r` )
	if [[ $type == "nx_lsa" ]]
	then
		 NX_LSA_PATH=$( echo `find $kernel -name nx_lsa.ko` )
		 if [[ $NX_LSA_PATH != "" ]]
		 then
			for qlogic_path in  $NX_LSA_PATH
			do
				DIRNAME=`dirname ${qlogic_path}`
			 	mv $DIRNAME/nx_lsa.ko $DIRNAME/qlogic.nx_lsa.ko
				 NX_LSA_CHECK=1
			done
		fi 
	fi

	if [[ $type == "nx_nic" ]]
	then
		NX_NIC_PATH=$( echo `find $kernel -name nx_nic.ko` )
		if [[ $NX_NIC_PATH != "" ]]
		then
			for qlogic_path in  $NX_NIC_PATH
			do
				DIRNAME=`dirname ${qlogic_path}`
				mv $DIRNAME/nx_nic.ko $DIRNAME/qlogic.nx_nic.ko
				NX_NIC_CHECK=1
			done
		fi	
	fi

	if [[ $type == "unm_nic" ]]
	then
		UNM_NIC_PATH=$( echo `find $kernel -name unm_nic.ko` )
		if [[ $UNM_NIC_PATH != "" ]]
		then
			for qlogic_path in  $UNM_NIC_PATH
			do
				DIRNAME=`dirname ${qlogic_path}`
				mv $DIRNAME/unm_nic.ko $DIRNAME/qlogic.unm_nic.ko
				UNM_NIC_CHECK=1
			done
		fi	
	fi	

	if [[ $type == "gpl" ]]
	then
		NX_GPL_PATH=$( echo `find $kernel -name netxen_nic.ko` )
		if [[ $NX_GPL_PATH != "" ]]
		then
			for qlogic_path in  $NX_GPL_PATH
			do
				DIRNAME=`dirname ${qlogic_path}`
				mv $DIRNAME/netxen_nic.ko $DIRNAME/qlogic.netxen_nic.ko
				NX_GPL_CHECK=1
			done
		fi	
	fi	
	
}

unload_drivers ()
{
	ALLDEV=$(awk '/:/ && !/\<lo\>/ { print substr($1,0,index($1,":")-1)}' \
							 /proc/net/dev)
	
	ret_flag=0
	echo "NIC driver will be temporarily unloaded"
	echo -n "Closing interfaces: "
	for dev in $ALLDEV
	do
		drv=$($ETHTOOL -i $dev 2>/dev/null | awk '/^driver/ {print $2}')
		case $drv in
		       "netxen-nic"|"netxen_nic")
				NX_GPL="netxen_nic"
				;;
			"nx_nic"|"hp_nic")
				NX_NIC="nx_nic"
				;;
			"unm_nic")
				UNM_NIC="unm_nic"
				;;
			*)
				continue
				;;
		esac
		echo -n "$dev "
		interface=$dev
		interface_up_txt=`ifconfig $interface | grep UP`
		ip_addr=`ifconfig $interface | grep "inet addr:" | awk '{ print $2 }'|cut -d : -f2`
		ip6_addr=`ifconfig $interface | grep "inet6 addr:" | grep "Global" |awk '{ print $3 }'`
		mask=`ifconfig $interface | grep "inet addr:" | awk '{ print $4 }'|cut -d : -f2`
		temp=`echo "ifconfig $interface $ip_addr netmask $mask "`
		temp6=`echo "/sbin/ip -6 addr add $ip6_addr  dev $interface"`
		#echo $temp
		if [ "$ip_addr" != "" ]; then
			has_ip=1;
			cmdstring[$int_index]=$temp
			int_index=`expr $int_index + 1`
		fi

		if [ "$ip6_addr" != "" ]; then
			has_ip=1;
			cmdstring[$int_index]=$temp6
			int_index=`expr $int_index + 1`
		fi

		if [ "$interface_up_txt" != "" ]; then
			local_temp=`echo "ifconfig $interface up"`
			cmdstring[$int_index]=$local_temp
			int_index=`expr $int_index + 1`
		else
			local_temp=`echo "ifconfig $interface down"`
			cmdstring[$int_index]=$local_temp
			int_index=`expr $int_index + 1`
		fi		

		ifconfig $dev down
		if [  "$?" != "0" ]; then
		       echo -n "[failed] "
		       ret_flag=1
		fi

	done
	echo ""

	if [ "$ret_flag" -eq "1" ]; then
		exit 1;
	fi

	if [ "$NX_LSA" != "" ]; then
	save_driver nx_lsa	
	$RMMOD $NX_LSA
		if [ "$?" != "0" ]; then
			echo $NX_LSA "driver failed to unload"
			echo "Please reboot the system"	
			exit 1;	
		fi
	fi

	if [ "$NX_NIC" != "" ]; then
	save_driver nx_nic	
	$RMMOD $NX_NIC
		if [ "$?" != "0" ]; then
		echo $NX_NIC "driver failed to unload"
		ret_flag=1;
	fi
	fi
	if [ "$UNM_NIC" != "" ]; then
	save_driver unm_nic	
		$RMMOD $UNM_NIC
		if [ "$?" != "0" ]; then
			echo $UNM_NIC "driver failed to unload"
			ret_flag=1;
		fi
	fi
	if [ "$NX_GPL" != "" ]; then
	save_driver gpl	
	$RMMOD $NX_GPL
		if [ "$?" != "0" ]; then
		echo $NX_GPL "driver failed to unload"
		ret_flag=1;
	fi
	fi
	
	if [ "$ret_flag" -eq "1" ]; then
		restore_driver
		exit 1;
	fi
}

fbd_check()
{
        bfd_colon=`echo $1|grep ":"| wc -l`
        bfd_dot=`echo $1|grep '\.'| wc -l`
        devices=`/sbin/lspci -d4040: |cut -d " " -f 1| wc -l`
	if [ $bfd_colon == 1 -o $bfd_dot == 1 ]; then
	   #echo $devices
	   i=1
	   j=0
	   valid_device=0
	   while [ $i -le $devices ]
	   do
	   str[$i]=`/sbin/lspci -d4040: |cut -d " " -f 1| sed -n "$i"p`
	   i=`expr $i + 1`
	   done
	   while [ $j -le $devices ]
	   do
	   if [ "${str[$j]}" == $1 ];then
           tmp=`expr $j - 1`
           pci_map_fbd=$tmp
	   valid_device=1
	   fi
	   j=`expr $j + 1`
	   done
	   if [ "$valid_device" == 0 ];then
	   echo WRONG PCI Bus:Device.FuncId. Correct values are
	   Display_Ports
           exit 1
	   fi
        else
           range=`expr $devices - 1`
	   if [ "$3" == -info ];then
           return
           else
                        if [ "$1" -lt 0 ]; then
			echo -e "\nNot a valid device number\n"
			Display_Ports
			exit 0
                        fi
                        if [ "$1" -gt $range ]; then
			echo -e "\nNot a valid device number\n"
			Display_Ports
			exit 0
                        fi
           fi

        fi

}

InitSetup ()
{
	TOOLS_DEF_DIR=/opt/netxen
	if [ $initsetupdone -eq 1 ]; then
		return 0
	fi
	if [ "$nodriver_flag" == "0" ]; then
		unload_drivers
	fi
	if [ "$NX_XPORT" == "" ]; then

		kernel=$( echo /lib/modules/`uname -r` )
		if [ "$xport_file_location" != "" ]; then
			nx_xport_loc=$xport_file_location
			$INSMOD $nx_xport_loc > /dev/null 2>&1
			if [ "$?" != "0" ]; then
				echo "NetXen Xport Driver failed to load from $nx_xport_loc"
				echo "Please use --file option to provide path to NetXen Xport Driver."
				return 1
			fi
		else
			nx_xport_loc=$( echo `find $kernel -name nx_xport.ko` )
			$INSMOD $nx_xport_loc > /dev/null 2>&1
			if [ "$?" != "0" ]; then
				if [ -f $TOOLS_DIR/nx_xport.ko ]; then
					$INSMOD $TOOLS_DIR/nx_xport.ko
					if [ "$?" != "0" ]; then
						echo "NetXen Xport Driver failed to load from $TOOLS_DIR"
						return 1
					fi
				else
					if [ ! -f $TOOLS_DEF_DIR/nx_xport.ko ]; then
						echo "Could not find the NetXen Xport Driver."
						echo "Please install version 4.0.304 or newer version of NetXen NIC driver."
						return 1
					fi

					$INSMOD $TOOLS_DEF_DIR/nx_xport.ko
					if [ "$?" != "0" ]; then
						echo "NetXen Xport Driver failed to load"
						return 1
					fi
				fi
			fi
		fi

		major=`cat /proc/devices | grep nx_pci | cut -d" " -f1 -`

		if [ ! -c /dev/nx_pci ]; then
			mknod -m 666 /dev/nx_pci c $major 0
		fi
	fi
	initsetupdone=1
	return 0

}

ClearSetup()
{
	if [ "$1" != "0" ]; then
		echo -e  "\nReceived a signal........"
	fi

	grep -q nx_xport /proc/modules
	if [ $? -eq 0 ]; then
		$RMMOD nx_xport
	fi

	if [ "$nodriver_flag" == "0" ]; then
		echo "Please wait while NetXen drivers are restored..."
		load_drivers
	fi
	initsetupdone=0
	if [ "$1" != "0" ]; then
                exit 1
        fi
}

Display_Ports ()
{
	devices=`$LSPCI -d 4040:|wc -l`
	k=0
	iterator=1;
	pslot_orig="255";

	if [ $devices -eq 0 ] ; then
		echo "No NetXen Devices found on the Machine"
		exit 0
	fi

	echo ""
	echo "Valid Device Numbers are:"
	echo ""
	while [ $k -lt $devices ]
	do
		pslot=`$LSPCI -d4040: | awk ' /:/ {print $1 }'|sed -n "$iterator"p | cut -d: -f1`
		function_id=`$LSPCI -d4040: | awk ' /:/ {print $1 }'|sed -n "$iterator"p | cut -d: -f2|cut -d . -f2`	
	        fbd_local=`/sbin/lspci -d4040: |cut -d " " -f 1 |sed -n "$iterator"p`
		subsystem=""
		string=""
		if [ "$pslot" != "$pslot_orig" ]
		then
			pslot_orig=$pslot;
			i=0;
		fi
		check_mgmt=`$LSPCI -d4040: | grep "Mgmt" |sed -n "$iterator"p|wc -l`
		if [ $check_mgmt -eq 0 ]; then
			
		subsystem=`$LSPCI -d 4040: -v | grep Subsystem | cut -d : -f 2 |sed -n "$iterator"p |cut -d ' ' -f 2`
		if [ $subsystem = "Hewlett-Packard" ] ; then
			subsystem="HP"
		else
			if [ $subsystem = "NetXen" ] ; then
				subsystem=""
			fi
		fi
		string=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`
		echo  "[$k] " $subsystem $string  "[PCI slot $pslot function $function_id]" "[Corresponding BDF $fbd_local]"
		

		echo ""
		i=$(($i+1));
		k=`expr $k + 1`
		else
			devices=`expr $devices - 1`	
		fi
		iterator=`expr $iterator + 1`
	done
}

Get_Netxen_Board ()
{
	devices=`$LSPCI -d 4040: | grep "\.0 "|wc -l`
	PCI_NUM=`$LSPCI -d4040: | cut -d "." -f2 | cut -d " " -f1`

	j=0
	k=0
	c=0
	iterator=1
	str=""

	if [ "$devices" == "0" ]; then

		echo "No NetXen Devices found on the Machine"
		return -1
	fi

	echo "NetXen Devices on the Machine"
	echo ""
	for k in $PCI_NUM
	do
		if [ $k -eq 0 ]; then
		pslot=`$LSPCI -d4040: | awk ' /:/ {print $1 }'|sed -n "$iterator"p | cut -d: -f1`
		card_string=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`
		card_string=`echo $card_string |grep "170e"|wc -l`
		subsystem=""
		string=""
			if [ $card_string -ne 0 ]; then
				iterator=`expr $iterator + 1`
			fi	
			subsystem=`$LSPCI -d 4040: -v | grep Subsystem | cut -d : -f 2 |sed -n "$iterator"p |cut -d ' ' -f 2`
			if [ $subsystem = "Hewlett-Packard" ] ; then
				subsystem="HP"
			else
				if [ $subsystem = "NetXen" ] ; then
					subsystem=""
				fi
			fi
			string=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`
			echo "[$c]" $subsystem $string "[PCI Bus $pslot]"
			str[$c]=$j
			device_string[`expr $iterator - 1`]=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`" [PCI Bus $pslot]"
			echo ""
			c=`expr $c + 1`
		fi
		j=`expr $j + 1`
		iterator=`expr $iterator + 1`
	done
	temp=`expr $devices - 1`
	while :
	do
	if [ $devices -gt 1 ]; then
		echo -e -n "Enter Choice [0-`expr $devices - 1`] Or Enter [q] to exit: "
	else
		echo -e -n "Enter Choice [0] Or Enter [q] to exit: "
	fi
	read i
	case $i in
			[qQ])
			if [ $initsetupdone -eq 1 ]; then
				ClearSetup 0
				exit 1
			fi
				exit 1
				;;
			[0-$temp])
				break
				;;
			*)
				echo "Wrong Input"
				;;
		esac
	done

	num1=${str[$i]}
	return $num1;
}

Flash_NetXen_Card()
{
	onecard=`$LSPCI -d 4040: | grep "\.0 "|wc -l`
	if [ "$onecard" == "1" -a "$nodowngrade" == "0" ]; then
		num1=0;
	else
		Get_Netxen_Board
		num1=$?
		if [ $num1 -eq 255 ]; then
			return -1;
		fi		
	fi
	cd $TOOLS_DIR
	echo -e -n "Enter the path for romimage [romimage from $TOOLS_DIR ] :"
	read ROMIMAGE
	
	if [ ! -f $ROMIMAGE ]; then
		echo "`readlink -f $ROMIMAGE` : Invalid Romimage path"
		`cd - > /dev/null 2>/dev/null`
		return -1
	fi
	
	echo -e "Updating firmware on board\n"

	if [ "$nodowngrade" -eq "0" ]; then
		echo "y"|$FLASHPGM -i $num1 -a "$ROMIMAGE"
	fi

	`cd - > /dev/null 2>/dev/null`

}

usage ()
{
	echo "
./nxflash --file <path to nx_xport.ko>	Nxflash interactive mode to do 
					operations like Display Info,
					Flash cards, Configure PXE/WOL,
					Generate XML report. User may pass
					path to nx_xport.ko file using --file
					option.

./nxflash --file <path to nx_xport.ko> --unattended
					Flash NetXen card in unattended mode.
					User may pass path to nx_xport.ko
					module using --file option.

./nxflash -l, --list          	Show all the NetXen devices installed.

./nxflash --file <path to nx_xport.ko> -i, --interface <device number> <options>
    <device number> or <bus:device.function>   Device index [See --list option]
    User may pass path to nx_xport.ko module using --file option.
    Options:
	-info,--information	       Show board information
	-d,-D,/d,/D name.xml           Creates name.xml file having NIC/firmware
                                       information.By default it Creates nicdiscovery.xml file
                                       in the current directory. No -i option is required.
	-u,-U,/u,/U name.xml           Upgrades the firmware based on name.xml file.
                                       By default it use nicdiscovery.xml file from the current directory.
                                       No -i is required. Must be used after -d option.
        -a, --all                      Burn flash using romimage.
        -a, --all <image file>         Specify flash image file [optional].
        -r, --restore <image file>     Restore flash using backup image.
        --pxe-on                       Enable PXE boot.
        --pxe-off                      Disable PXE boot [default].
        --wol-on                       Enable Wake on Lan.
        --wol-off                      Disable Wake on Lan.

./nxflash -h, --help          	Display this message.
./nxflash -v, --version      	Display the version information.

Note: NetXen network interfaces will be offline during flash update.
"
}

map_ethstring ()
{
iterator=0
ALLDEV=$(awk '/:/ && !/\<lo\>/ { print substr($1,0,index($1,":")-1)}' \
                /proc/net/dev)

count=`$LSPCI -d 4040:|grep Ethernet |wc -l`

for dev in $ALLDEV
do
        drv=$($ETHTOOL -i $dev 2>/dev/null | awk '/^driver/ {print $2}')
        if [ "$drv" == "nx_nic" -o "$drv" == "netxen_nic" ]; then
                while [ $iterator -lt $count ]
                do
                        ss=${pci[$iterator]}
                        tt=`$ETHTOOL -i $dev 2>/dev/null |grep bus |awk '{ print $2 }'`
                        match_condtion=`echo $tt|grep $ss |wc -l`
                        if [ $match_condtion -ne 0  ]; then
                                ethstr[$iterator]=$dev
                                break
                        fi
                        iterator=`expr $iterator + 1`
                done
                iterator=0
        fi
done
}

fill_pcistring()
{
iterator=0
ALLPCI=`$LSPCI -d 4040: | grep "Ethernet" | awk '{ print $1}'`

for pslot in $ALLPCI
do
pci[$iterator]=$pslot
iterator=`expr $iterator + 1`
done
}



check_reqmts ()
{
	if [ $rqmts -eq 1 ]; then
		return 0
	fi
	echo -e "\nChecking for required components..."

        if [ ! -d "$TOOLS_DIR" ]; then
                echo "NetXen Tools directory not present"
                exit -1
        fi

        cd $TOOLS_DIR

        if [ ! -f $FLASHPGM ]; then
                echo "NetXen Flash utilty binary not present"
		`cd - > /dev/null 2>/dev/null`
                exit -1
        fi

        echo -e ""
        echo -e "Using" `$FLASHPGM -v` | sed s/Report/\\nReport/
        echo -e ""
        version_check=`$FLASHPGM -v | grep "nxflash version"|cut -c 28-30`
	if [ $version_check -lt 304 ]; then

		echo "nxflash utilty present is not compatible with the driver."
		`cd - > /dev/null 2>/dev/null`
		exit -1;
	fi

	rqmts=1

}



FlashUpdateMenu ()
{
	while :
	do
	clear

	echo "NetXen Flash Update Utility"
	echo ""

	
	onecard=`$LSPCI -d 4040: | grep "\.0 "|wc -l`
	if [ "$onecard" == "1" -a "$nodowngrade" == "0" ]; then
			pslot=`$LSPCI -d4040: | awk ' /:/ {print $1 }'|sed -n "1"p | cut -d: -f1`
			echo -e "\n\nDetected NetXen card : "`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "1"p` "[PCI Bus $pslot]"
	fi
		break	
	done

	check_reqmts
	
	Flash_NetXen_Card
}

main()
{

	devices=`$LSPCI -d 4040:|wc -l`
	onecard=`$LSPCI -d 4040: | grep "\.0 "|wc -l`

	clear

	check_reqmts

	if [ $onecard -gt 1 ]; then
		Get_Netxen_Board
		netxen_device=$?
		if [ $netxen_device -eq 255 ]; then
			
			if [ $initsetupdone -eq 1 ]; then
				ClearSetup 0
			fi
			exit -1
		fi		
	else
		iterator=1
		pslot=`$LSPCI -d4040: | awk ' /:/ {print $1 }'|sed -n "$iterator"p | cut -d: -f1`
		card_string=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`
		card_string=`echo $card_string |grep "170e"|wc -l`
			if [ $card_string -ne 0 ]; then
				iterator=`expr $iterator + 1`
			fi	

		device_string[0]=`$LSPCI -d 4040: -v | grep Subsystem | cut -d " " -f4-12 |sed -n "$iterator"p`" [PCI Bus $pslot]"
		netxen_device=0	
	fi	
	
	while [ 1 ]
	do
	echo ""	
	echo "## ${device_string[$netxen_device]} ##"
	echo ""	
	echo "[1] Display BoardInfo"
	echo "[2] Flash Update"
	echo "[3] Flash Restore"
	echo "[4] Configure PXE"
	echo "[5] Configure WOL"
	echo "[6] Flash Test"

	if [ $onecard -gt 1 ]; then
		echo "[q] Return to main menu"		
	else
		echo "[q] Exit"
	fi
	echo -e -n "Enter choice: "
	read choice

	case $choice in
	1)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		$FLASHPGM -i $netxen_device --info

		echo "Press return to continue"
		read	
		;;
	2)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		cd $TOOLS_DIR
		echo -e -n "Enter the path for romimage [romimage from $TOOLS_DIR ] : "
		read ROMIMAGE
		if [ ! -f $ROMIMAGE ]; then
			echo "`readlink -f $ROMIMAGE` : Invalid Romimage path"
			continue
		fi
		echo -e "Updating firmware on board\n"
		$FLASHPGM -i $netxen_device -a "$ROMIMAGE"
		`cd - > /dev/null 2>/dev/null`

		;;
	3)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi
		
		while [ 1 ]
		do
		echo -e -n "Enter the path for backup romimage [Press return to go back] : "
		read ROMIMAGE
		if [ "$ROMIMAGE" == "" ]; then
			local_abort=1
			break
		fi
		if [ ! -f $ROMIMAGE ]; then
			echo "`readlink -f $ROMIMAGE` : Invalid backup Romimage path"
			continue
		else
			break	
		fi
		done	
		if [ "$local_abort" != "1" ]; then
			echo -e "Restoring firmware on board\n"
			$FLASHPGM -i $netxen_device -r "$ROMIMAGE"
		fi
		local_abort=0
		;;

	4)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi
		echo "1)Turn PXE On"
		echo "2)Turn PXE Off"
		echo -e -n "Enter choice: "
		read choice
			case $choice in
			1)
				$FLASHPGM -i $netxen_device --pxe-on
				;;
			2)
				$FLASHPGM -i $netxen_device --pxe-off
				;;
			*)
				echo "Invalid choice"
				;;
			esac
		sleep 1
		;;

	5)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		echo "1)Turn WOL On"
		echo "2)Turn WOL Off"
	
		echo -e -n "Enter choice: "
		read choice
		
		case $choice in
		1)
			$FLASHPGM -i $netxen_device --wol-on
			;;
		2)
			$FLASHPGM -i $netxen_device --wol-off
			;;
		*)
			echo "Invalid choice"
			;;
		esac
		sleep 1
		;;

	6)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		$FLASHPGM -i $netxen_device -F
		sleep 1
		;;
	[qQ])
		if [ $onecard -gt 1 ]; then
			main	
		else
			if [ $initsetupdone -eq 1 ]; then
				ClearSetup 0
			fi
		fi
		echo "Exiting....."
		break
		;;
	7)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		$FLASHPGM -i $netxen_device -p
		echo "Press return to continue"
		read	
		;;

	8)
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			continue
		fi

		$FLASHPGM -i $netxen_device -s
		sleep 1
		;;

	*)
		echo "Invalid choice....."
		;;
	esac
	done
}

nxflash_cmd()
{
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			exit 1
		fi
		cd $TOOLS_DIR
		args=$*
		args1=`echo $args | cut -d " " -f 1`
		if [ "$args1" == "--file" ]; then
			wo_args1=`echo $args | cut -d " " -f 1 --complement`
			reqd=`echo $wo_args1 | cut -d " " -f 1 --complement`
		else
			reqd=$args
		fi

		$FLASHPGM $reqd
		ret_code=$?

		case $ret_code in
			19)
				Display_Ports
				;;
			2)
				Display_Ports
				;;
			5)
				break
				;;
			16)
				break
				;;
			30)
				break
				;;
			38)
				break
				;;
			0)
				break
				;;
			*)
				usage
				;;
		esac
		`cd - > /dev/null 2>/dev/null`
		ClearSetup $ret_code 

}

info_display()
{
	if [ "$1" == "--file" ]; then
		tdev=$4
		option=$5
		xport_file_location=$2
	else
		tdev=$2
		option=$3
	fi
        fbd_check $tdev $option

	if [ "$NX_NIC" != "" -o "$NX_GPL" != "" ]; then
		fill_pcistring
		map_ethstring
		count=`$LSPCI -d 4040:|grep Ethernet |wc -l`
        	if [ $pci_map_fbd != -1 ];then
		valid_input=`echo $pci_map_fbd|awk ' /[[:alpha:]]/ { print $0 } '| wc -l`
                else
		valid_input=`echo $tdev|awk ' /[[:alpha:]]/ { print $0 } '| wc -l`
                fi
		if [ $valid_input -ne 0 ]; then
			echo -e "\nNot a valid device number\n"
			Display_Ports
			exit 0
		fi	
        	if [ $pci_map_fbd != -1 ];then
		if [ $pci_map_fbd -lt 0 -o $pci_map_fbd -ge $count ]; then
			echo -e "\nNot a valid device number\n"
			Display_Ports
			exit 0
		fi	
                else

		if [ $tdev -lt 0 -o $tdev -ge $count ]; then
			echo -e "\nNot a valid device number\n"
			Display_Ports
			exit 0
		fi	
                fi
		
        	if [ $pci_map_fbd != -1 ];then
		device_info=${ethstr[$pci_map_fbd]}
                else
		device_info=${ethstr[$tdev]}
                fi
		if [ "$device_info" == "" ]; then
			echo -e "Network device for device number $tdev does not exist\n"
			exit 0
		fi
		echo -e "\nNetwork Device: $device_info\n"	
		if [ $pci_map_fbd != -1 ];then
			ethtool_info $device_info $pci_map_fbd
		else
			ethtool_info $device_info $tdev
		fi

	else
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			exit 1
		fi
		$FLASHPGM -i $tdev --info
		ClearSetup 0
	fi
}

trap ClearSetup SIGINT SIGTERM

IS_ROOT=`whoami`

if [ "$IS_ROOT" != "root" ]; then
		echo "nxflash:Requires root privileges."
		exit 1;
fi

if [ -f "$LOCK_FILE" ]; then
	if [[ "$1" == "-U" || "$1" == "-u" || "$1" == "/u" || "$1" == "/U" || 
	"$1" == "-D" || "$1" == "-d" || "$1" == "/d" || "$1" == "/D" ]]; then
		rm -rf $LOCK_FILE
        else
	echo "Warning: Either another NetXen tool is already running, or your last session did not close gracefully.\n"
	echo -e -n "Do you want to proceed anyway ? (Y/N)"
	read choice
	case $choice in
		[Yy])
			rm -rf $LOCK_FILE
			break
			;;
		[nN])
			echo "Exiting....."
			exit 1
			;;
		*)
			echo "Invalid choice....."
			exit 1
			;;
	esac
     fi
fi

if [ "$*" == "" ]; then
	main
elif [ "$*" == "--list" -o "$*" == "-l" ]; then
	Display_Ports
elif [[ "$3" == "-D" || "$3" == "-d" || "$3" == "/D" || "$3" == "/d" || "$1" == "-D" || "$1" == "-d" || "$1" == "/D" || "$1" == "/d" ]]; then
	NXXPORT_DRVR=""
	NXNIC_DRVR=""
	NXNIC_DRVR=`lsmod |grep "nx_nic"|cut -d " " -f 1`
	NETXENNIC_DRVR=""
	NETXENNIC_DRVR=`lsmod |grep "netxen_nic"|cut -d " " -f 1`
	NXNIC_INTERFACE=""
	nx_interface=""
	inf_name=""
	local=""
	i=1
	flag=0
	max_inf=`ifconfig -a |grep HWaddr| wc -l`

	if [ "$1" == "--file" ]; then
		xport_file_location=$2
		tdev=$4
	else
		tdev=$2
	fi

	if [ "$NXNIC_DRVR" == "nx_nic" ]; then
		for ((i=1; i<=$max_inf; ++i )) ;
		do
			inf_name=`ifconfig -a |grep HWaddr| sed -n "$i"p|cut -d " " -f 1`
			local=`ethtool -i $inf_name |grep nx_nic| cut -d: -f 2`
			if [ "$local" == " nx_nic" ]; then
				nx_interface=$inf_name
				flag=1
			break
			fi
		done
	fi
	if [ "$NETXENNIC_DRVR" == "netxen_nic" ]; then
		for ((i=1; i<=$max_inf; ++i )) ;
		do
			inf_name=`ifconfig -a |grep HWaddr| sed -n "$i"p|cut -d " " -f 1`
			local=`ethtool -i $inf_name |grep netxen_nic| cut -d: -f 2`
			if [ "$local" == " netxen_nic" ]; then
				nx_interface=$inf_name
				flag=1
			break
			fi
		done
	fi
	if [ "$flag" == "0" ]; then
		InitSetup
		if [ "$?" != "0" ]; then
			echo "Init Setup Failed"
			ClearSetup 0
			exit 1
		fi
		NXXPORT_DRVR=`lsmod |grep "nx_xport"|cut -d " " -f 1`
	fi

	if [ "$NXXPORT_DRVR" == "nx_xport" ]; then

		if [ "$tdev" == "" ]; then
		$FLASHPGM -i 0 -d "nicdiscovery.xml"
        	else
		$FLASHPGM -i 0 -d $tdev
        	fi
	elif [ "$NXNIC_DRVR" == "nx_nic" ] || [ "$NETXENNIC_DRVR" == "netxen_nic" ];  then
		if [ "$tdev" == "" ]; then
		$FLASHPGM -i $nx_interface -d "nicdiscovery.xml"
        	else
		$FLASHPGM -i $nx_interface -d $tdev
        	fi
	fi

	if [ "$NXXPORT_DRVR" == "nx_xport" ]; then
		ClearSetup 0
	fi

elif [[ "$3" == "-U" || "$3" == "-u" || "$3" == "/U" || "$3" == "/u" || "$1" == "-U" || "$1" == "-u" || "$1" == "/U" || "$1" == "/u" ]]; then
	if [ "$1" == "--file" ]; then
		xport_file_location=$2
		tdev=$4
	else
		tdev=$2
	fi
	InitSetup
	if [ "$?" != "0" ]; then
		echo "Init Setup Failed"
		ClearSetup 0
		exit 1
	fi
	if [ "$tdev" == "" ]; then
	$STAT ./nicdiscovery.xml > /dev/null 
	if [ "$?" != "0" ]; then
		ClearSetup 1
	fi
	$FLASHPGM -i 0 -u "nicdiscovery.xml"
        else
	$STAT $tdev > /dev/null 
	if [ "$?" != "0" ]; then
		ClearSetup 1
	fi
	$FLASHPGM -i 0 -u $tdev
        fi
	ClearSetup 0
elif [ "$*" == "--version" -o "$*" == "-v" ]; then
	$FLASHPGM -v
        exit 0
elif [ "$3" == "--unattended" -o "$1" == "--unattended" ]; then
	if [ "$1"  == "--file" ]; then
		xport_file_location=$2
	fi
	InitSetup
	if [ "$?" != "0" ]; then
		echo "Init Setup Failed"
		ClearSetup 0
		exit 1
	fi
	nodowngrade=0
	FlashUpdateMenu
	ClearSetup 0

elif [ "$3" == "-i" -o "$1" == "-i" ]; then
	
	if [ "$1" == "--file" ]; then
		xport_file_location=$2
		option1=$5
		option2=$6
		tdev=$4
		total_r=6
		total_a=6
		total_o=5
	else
		option1=$3
		option2=$4
		tdev=$2
		total_r=4
		total_a=4
		total_o=3
	fi

	case $option1 in
	
    	"--pxe-on" | "--pxe-off" | "--wol-off" | "--wol-on" | "-F" |  "--ncsi-on" | "--ncsi-off")

                fbd_check $tdev $option1
		if [ "$#" != "$total_o" ]; then
			echo "Invalid option"
			usage
			exit 0
		fi

		;;

	"-d" | "-r" | "--restore" | "--hwflags")
		if [ "$#" != "$total_r" ]; then
			echo "Invalid option"
			usage
			exit 0
		fi
		;;

	"-a" | "--all")
                fbd_check $tdev $option1
		if [ "$#" -gt "$total_a" ]; then
			echo "Invalid option"
			usage
			exit 0
		fi
		;;

	"-f" | "--force")
		if [ "$option2" != "-a" -a "$option2" != "--all" -a "$option2" != "-r" -a "$option2" != "--restore" ]; then
			echo "Invalid option"
			usage
			exit 0
		fi
		;;

	"-info")
		info_display $*
		exit 0
		;;
	"--information")
		info_display $*
		exit 0
		;;

		*)
			usage
			exit 0
	esac
	nxflash_cmd $*

elif [ "$1" == "--file" ]; then
	xport_file_location=$2
	main
else
	if [ $# -lt 3 ]; then
		usage
		exit 0
	fi
fi
