#!/bin/sh
#
#    Copyright (c) 2004-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#        This pre-install script will be run before firmware
#        download or firmwarerestore starts.  It checks the
#	 combination of platform (21=meteor/cp128, 42=saturn/cp256),
#	 chassis option (1=single switch/CER, 2=dual switch, etc),
#	 and other switch features to see if the firmware download
#	 or firmwarerestore will be accepted.
#
#	 It can run some preprocessing code before the commands start,
#	 e.g. removing some files to free up disk space, etc.
#
#	 WARNING: This file is run before firmwarerestore also. If
#	 you add code that only applies to firmwaredownload, you will
#	 need to use the "TYPE" flag to exclude firmwarerestore. 
#
#        This version of the script is for starting firmware version 5.1.
#        It needs to be updated for every minor release.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
# Passed in version numbers
#
TO_MAJOR=$1
TO_MINOR=$2
TO_PATCH=$3
#
# When firmwaredownload is invoking this script,
# TYPE should be NULL; when firmwarerestore is
# invoking it, TYPE is "restore"
#
TYPE=$4

#
# Common variables
#
CHASSISCONFIG=/bin/getchassisconfig
NULL=/dev/null

#
# Used to detect whether the to-be-downloaded version
# is an NI version.
#
NI_VERSION_STR="5.2.1_ni"
RELEASE_FILE="/tmp/release.plist"

DEBUG=0
if [ $DEBUG = 1 ]; then
	ECHO='echo -e'
else
	ECHO=:
fi

OVERALL_STS=0
# This should match the file name in src/lib/upgrade/upgrade.c
STATUS_FILE=/tmp/fwdl_err.txt
# clear status file
echo > $STATUS_FILE
echo " " > $STATUS_FILE
echo "The following items need to be addressed before downloading the specified firmware:" >> $STATUS_FILE


#
# Error codes - these have to match the error codes
# in fwdl.h.
#
STS_OK=0
ERR_CONF_CHASSIS=42		# FWDL_ERR_CHASSISOPT 0x2a
ERR_UNSUPPORTED_VER=44 		# FWDL_ERR_VERSION 0x2c
ERR_CONF_ROUTING=45 		# FWDL_ERR_ROUTING 0x2d
ERR_CONF_BLADETYPE17=47		# FWDL_ERR_BLADETYPE 0x2f
ERR_CONF_BLADETYPE24=48		# FWDL_ERR_BLADETYPE 0x30
ERR_CONF_LSPORTTYPE=49		# FWDL_ERR_PORTTYPE 0x31
ERR_CONF_PORTCFG1=51		# FWDL_ERR_PORTCFG1 0x33
ERR_CONF_PORTCFG2=52		# FWDL_ERR_PORTCFG1 0x34
ERR_CONF_PORTMIRROR=53		# FWDL_ERR_PORTMIRROR 0x35
ERR_CONF_BLADETYPE31=54		# FWDL_ERR_BLADETYPE 0x36
ERR_CONF_BLADETYPE36=55		# FWDL_ERR_BLADETYPE 0x37
ERR_CONF_AD=56				# FWDL_ERR_BLADETYPE 0x38
ERR_CONF_CFGSIZE=57			# FWDL_ERR_BLADETYPE 0x39
ERR_CONF_FASTWRITE=58		# FWDL_ERR_FASTWRITE 0x3a
ERR_CONF_EXTRUNK=59			# FWDL_ERR_EXTRUNK 0x3b
ERR_CONF_INTEROP=60			# FWDL_ERR_INTEROP 0x3c
ERR_CONF_IPSEC=61			# FWDL_ERR_IPSEC 0x3d
ERR_CONF_TPIPE=62			# FWDL_ERR_TPIPE 0x3e
ERR_CONF_DHCP=63			# FWDL_ERR_DHCP 0x3f
ERR_CONF_FABWIDESTRICT=64	# FWDL_ERR_FABWIDESTRICT 0x40
ERR_CONF_RADIUSMODE=65	# FWDL_ERR_RADIUSMODE 0x41
ERR_CONF_SECMODE=66		# FWDL_ERR_SECMODE 0x42
ERR_CONF_AGMODE=67			# FWDL_ERR_AGMODE
ERR_NI_PLATFORM=68	
ERR_CONF_SFOS_ON=69
ERR_CONF_MCDATA_MSPLAT_ON=70
ERR_CONF_MCDATA_NOT_ACCEPTED=71
RET=${STS_OK}

MARATHONMSG="AP BLADE type 24 is inserted. Please use slotshow to find out which slot it is in and remove it."
IRONMANMSG="AP BLADE type 31 is inserted. Please use slotshow to find out which slot it is in and remove it."
POSITRONMSG="SW BLADE type 36 is inserted. Please use slotshow to find out which slot it is in and remove it."
ADMSG="AD feature is in use. Please clear it using the ad --clear command."
FASTWRITEMSG="Fastwrite is currently configured. Please use the portcfg fciptunnel command to remedy this."
CFGSIZEMSG="Zone configuration size is too large. Decrease the zone configuration size."
PORTMIRRORMSG="Port mirror connections detected. Please use portmirror --delete to remove these mirror connections."
LONGDISTANCEMSG="Ports in long-distance LS mode detected. Please use the portcfglongdistance command to remove the LS port settings."
ROUTINGMSG="Device Based Routing policy is not supported by 5.1. Please use aptPolicy to change the routing policy."
IPSECMSG="IPSEC is currently configured. Please use the portcfg fciptunnel command to remedy this."
TPIPEMSG="Tape pipelining is currently configured. Please use the portcfg fciptunnel command to remedy this."
DHCPMSG="DHCP is currently on. Please turn DHCP off using the ipaddrset command."
FABWIDESTRICTMSG="Strict fabric wide policy is configured. Please use the fddcfg --fabwideset command to remedy this."
RADIUSMODEMSG="The switch is currently configured with \"radiuslocal\" mode. Please use the aaaconfig command to remedy this."
SECMODEFLAGMSG="One or more DCC policies contain more than 256 ports, more than the maximum ports supported by the firmware you are trying to download. Please delete the DCC policy, or remove ports from the DCC policy to remedy this."
AGMODEMSG="Access Gateway mode is currently enabled. Please use \"ag --modedisable\" to remedy this."
NIPLATFORMMSG="The v5.2.1_ni firmware is not supported in this platform. It is only supported by SW5000."
MCDATA_MSPLAT_ON_MSG="MS Platform Service is currently enabled. Please use \"msplmgmtdeactivate\" to remedy this."
SFOS_ON_MSG="Secure FOS enabled. Please use \"secmodedisable\" to remedy this."
DOMAIN_RANGE_MSG="Domain ID needs to be set to 1-31. Please use \"switchdisable\" to disable the switch then use \"configure\" to change the domain ID."
PID_CORE_MSG="PID Format needs to be set to Core. Please use \"switchdisable\" to disable the switch then use \"configure\" to change the Switch PID Format."
AD_DB_MSG="AD Database needs to be cleared. Please use \"ad --clear\" to remedy this."
SECUR_STRICT_MSG="Security needs to be set to non-strict mode. Please use \"fddcfg\" to remedy this."

otherhost(){
        printf 10.0.0.$((5+$(othercp)))
}

HASHOW="$(/fabos/bin/hashow)"

othercp(){
        that=${HASHOW#*Remote CP \(Slot}
        that="${that%%,*}"
        printf $(($that - 5))
}

add_err_status(){
	echo "$1" >> $STATUS_FILE
}
		
lsport_active(){
	portcfgshow_cmd=$(ls /fabos/cliexec/portcfgshow)
	if [ "$portcfgshow_cmd" == "/fabos/cliexec/portcfgshow" ]; then
		LSPORT=`/fabos/cliexec/portcfgshow | grep "Long Dist" | grep "LS" | wc -l`
	else
		LSPORT=`/fabos/bin/portcfgshow | grep "Long Dist" | grep "LS" | wc -l`
	fi

	return $LSPORT
}

#
#	Make sure CPs are synchronized. This is to ensure
#	the other CP is up and running so rsh will not block
#	while trying to execute the command. If the CPs are
#	not synchronized, return 0 (no port set in LS mode).
#
lsport_standby(){
	sync=`/fabos/bin/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`
	if [ "$sync" != "State sync" ]; then
		return 0
	else
		portcfgshow_cmd=$(/usr/bin/rsh -n $(otherhost) ls /fabos/cliexec/portcfgshow 2> ${NULL})
		if [ "$portcfgshow_cmd" == "/fabos/cliexec/portcfgshow" ]; then
			LSPORT=`/usr/bin/rsh -n $(otherhost) /fabos/cliexec/portcfgshow | grep "Long Dist" | grep "LS" | wc -l`
		else
			LSPORT=`/usr/bin/rsh -n $(otherhost) /fabos/bin/portcfgshow | grep "Long Dist" | grep "LS" | wc -l`
		fi
	fi

	return $LSPORT
}

cfgcheck_active51(){
	error=$(/fabos/sbin/switchcfgvalid 0x51)
	echo "$error"
	add_err_status "$error"
	error=${error#*error}
	error=${error%%)*}
	return $error
}

cfgcheck_active50(){
	error=$(/fabos/sbin/switchcfgvalid 0x50)
	echo "$error"
	add_err_status "$error"
	error=${error#*error}
	error=${error%%)*}
	return $error
}

#
#	Make sure CPs are synchronized. This is to ensure
#	the other CP is up and running so rsh will not block
#	while trying to execute the command. If the CPs are
#	not synchronized, return 0 (no port set in LS mode).
#
cfgcheck_standby51(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		#determine the active cp version so we know which binary to run
		ACTV_MJR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Major: | sed -n -e 's/Major://gp')
		ACTV_MNR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Minor: | sed -n -e 's/Minor://gp')

		# only 52 has the 0x51 option
		if [ $ACTV_MJR == "5" -a $ACTV_MNR -ge 2 ]; then
			error=$(/usr/bin/rsh -n $(otherhost) /fabos/sbin/switchcfgvalid 0x51)
			echo "$error"
			add_err_status "$error"
			error=${error#*error}
			error=${error%%)*}
			return $error
		else
			return 0
		fi
	fi
}

#
#	Make sure CPs are synchronized. This is to ensure
#	the other CP is up and running so rsh will not block
#	while trying to execute the command. If the CPs are
#	not synchronized, return 0 (no port set in LS mode).
#
cfgcheck_standby50(){
	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		#determine the active cp version so we know which binary to run
		ACTV_MJR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Major: | sed -n -e 's/Major://gp')
		ACTV_MNR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Minor: | sed -n -e 's/Minor://gp')

		# 51 and 52 has the 0x50 option
		if [ $ACTV_MJR == "5" -a $ACTV_MNR -ge 1 ]; then
			error=$(/usr/bin/rsh -n $(otherhost) /fabos/sbin/switchcfgvalid 0x50)
			echo "$error"
			add_err_status "$error"
			error=${error#*error}
			error=${error%%)*}
			return $error
		else
			return 0	
		fi
	fi
}

#
# adcheck_active
#
# Check whether or not there are any admin domains configured
# on the active CP
#
adcheck_active(){
	ADCHECK=` ROLE_ID=root LOGIN_ID=root CURRENT_AD=255 /fabos/bin/switchshow | grep "AD Feature" | grep -v "Not Active" | wc -l`
	return $ADCHECK
}

#
# adcheck_standby
#
# Check whether or not there are any admin domains configured
# on the standby CP
# 5.2->5.1 yes
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
adcheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		ADCHECK=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/switchshow | grep "AD Feature" | grep -v "Not Active" | wc -l`
	fi
	return $ADCHECK
}

#
# interopcheck_active
#
# Check to see if interop mode is enabled
# 5.2->5.1 no	
# 5.2->5.0 no
# 5.0->5.2 yes
# 5.1->5.2 yes
#
interopcheck_active(){
		INTEROP=`/fabos/bin/configshow | grep switch.interopMode | grep 1 | wc -l`
		return $INTEROP
}

#
# interopcheck_standby
#
# Check to see if interop mode is enabled via the standby CP
# 5.2->5.1 no	
# 5.2->5.0 no
# 5.0->5.2 yes
# 5.1->5.2 yes
#
interopcheck_standby(){
	if [ -f /fabos/cliexec/hashow ];then
		hashow_cmd="/fabos/cliexec/hashow"
	else 
		hashow_cmd="/fabos/bin/hashow"
	fi

	sync=`$hashow_cmd | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else

		INTEROP=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep switch.interopMode | grep 1 | wc -l`
	fi

	return $INTEROP
}


#
# fastwritecheck_active
#
# Check to see if fastwrite is enabled activated on the active CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
fastwritecheck_active(){
		FWRITE=`/fabos/bin/configshow | grep FCIPTUNNEL | grep -E "TbWt=1|FWrt=1" | wc -l`
		return $FWRITE
}

#
# fastwritecheck_standby
#
# Check to see if fastwrite is enabled activated on the standby CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
fastwritecheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		FWCFG=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep FCIPTUNNEL | grep -E "TbWt=1|FWrt=1" | wc -l`
	fi

	return $FWCFG
}

#
# ipseccheck_active
#
# Check to see if ipsec is enabled activated on the active CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
ipseccheck_active(){
		IPSEC=`/fabos/bin/configshow | grep -E "IKE=" | grep -v "IKE=0" | wc -l`
		return $IPSEC
}

#
# ipseccheck_standby
#
# Check to see if ipsec is enabled activated on the standby CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
ipseccheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		IPSEC=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep -E "IKE=" | grep -v "IKE=0" | wc -l`
	fi

	return $IPSEC
}

#
# tpipecheck_active
#
# Check to see if tape pipelining is enabled activated on the active CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
tpipecheck_active(){
		TPIPECHECK=`/fabos/bin/configshow | grep -E "TapeAcc=1" | wc -l`
		return $TPIPECHECK
}

#
# tpipecheck_standby
#
# Check to see if tape pipelineing is enabled activated on the standby CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
tpipecheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		TPIPECHECK=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep -E "TapeAcc=1" | wc -l`
	fi

	return $TPIPECHECK
}

#
# dhcpcheck_active
#
# Check to see if dhcp is enabled
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
dhcpcheck_active(){
		DHCP=`/fabos/bin/ipaddrshow | grep -E "DHCP: On" | wc -l`
		return $DHCP
}

#
# dhcpcheck_standby
#
# Check to see if ipsec is enabled activated on the standby CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
dhcpcheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		DHCP=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/ipaddrshow | grep -E "DHCP: On" | wc -l`
	fi

	return $DHCP
}

#
# fabwidestrictcheck_active
#
# Check to see if fabwide strict is activated on the active CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
fabwidestrictcheck_active(){
		RADIUSCHECK=`/fabos/bin/configshow | grep -E "fdd.fabwidecfg" | grep -e "SCC:S" -e "DCC:S" | wc -l`
		return $RADIUSCHECK
}

#
# fabwidestrictcheck_standby
#
# Check to see if fabwide strict is activated on the standby CP
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
fabwidestrictcheck_standby(){

	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		RADIUSCHECK=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep -E "fdd.fabwidecfg" | grep -e "SCC:S" -e "DCC:S" | wc -l`
	fi

	return $RADIUSCHECK
}

#
# radiusmodecheck
#
# check to see if radius mode is set to --radiuslocal
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
radiusmodecheck(){
		RADIUSCHECK=`grep -E "radius.mode" /etc/fabos/fabos.chassis.conf | grep 5 | wc -l`
		return $RADIUSCHECK
}
		

#
# secmodeflagcheck_active
#
# Check to see whether DCC policy has more than 256 ports through the
# sec.mode flag value.
# 5.2->5.1 yes	
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
secmodeflagcheck_active(){
	SECMODECFG=`/fabos/bin/configshow | grep -E "sec.mode" |cut -d : -f 2`
	DEFPTCOUNT=$((SECMODECFG & 16))
	ACTPTCOUNT=$((SECMODECFG & 32))
	if [ $DEFPTCOUNT != "0" ] || [ $ACTPTCOUNT != "0" ]; then
		return 1
	else
		return 0
	fi
}

#
# secmodeflagcheck_standby
#
# Check to see whether DCC policy has more than 256 ports through the
# sec.mode flag value.
# 5.2->5.1 yes
# 5.2->5.0 yes
# 5.0->5.2 no
# 5.1->5.2 no
#
secmodeflagcheck_standby(){
	sync=`/fabos/cliexec/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`

	if [ "$sync" != "State sync" ]; then
		return 0
	else
		SECMODECFG=`/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/configshow | grep -E "sec.mode" | cut -d : -f 2`
		DEFPTCOUNT=$((SECMODECFG & 16))
		ACTPTCOUNT=$((SECMODECFG & 32))
	fi
	if [ $DEFPTCOUNT != "0" ] || [ $ACTPTCOUNT != "0" ]; then
		return 1
	else
		return 0
	fi
}

#
# swbd
#
# Retrieve the current system platform name, of the form "SWBDn", where n
# is cardinal number, assuming a sin/hinv input stream.
#
swbd() {
	sed -n -e 's/^.\+\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

#
# chassisopt
#
# Retrieve the current chassisconfig option.
#
chassisopt() {
	sed -n -e 's/^Cur.\+on: \([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# marathon_present
#
# Check whether there are any marathon blades enabled in the system.
#
marathon_present() {
	slotshow_cmd=$(ls /fabos/cliexec/slotshow)
	if [ "$slotshow_cmd" == "/fabos/cliexec/slotshow" ]; then
		MARATHON=$(/fabos/cliexec/slotshow | grep 24 | wc -l)
	else
		MARATHON=$(/fabos/bin/slotshow | grep 24 | wc -l)
	fi

	return $MARATHON
}

#
# ironman_present
#
# Check whether there are any ironman blades enabled in the system.
#
ironman_present() {

	slotshow_cmd=$(ls /fabos/cliexec/slotshow)
	if [ "$slotshow_cmd" == "/fabos/cliexec/slotshow" ]; then
		IRONMAN=$(/fabos/cliexec/slotshow | grep 31 | wc -l)
	else
		IRONMAN=$(/fabos/bin/slotshow | grep 31 | wc -l)
	fi

	return $IRONMAN
}

#
# positron_present
#
# Check whether there are any positron blades enabled in the system.
#
positron_present() {
	slotshow_cmd=$(ls /fabos/cliexec/slotshow)
	if [ "$slotshow_cmd" == "/fabos/cliexec/slotshow" ]; then
		POSITRON=$(/fabos/cliexec/slotshow | grep 36 | wc -l)
	else
		POSITRON=$(/fabos/bin/slotshow | grep 36 | wc -l)
	fi

	return $POSITRON
}

#
# lsport_present
#
# Check whether there are any long distance ports configured in LS mode.
#
lsport_present() {
	case "$(/fabos/bin/hashow | ( read a; echo $a ))" in
        	*Local*Active*) 
			lsport_active
			;;
        	*Local*Standby*)
			lsport_standby
			;;
        	*"Not supported"*)
			lsport_active
			;;
        	*)
			;;
	esac
}

#
# config_check
#
# Check the port configurations
#
cfgcheck51() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			cfgcheck_active51
			;;
        	*Local*Standby*)
			cfgcheck_standby51
			;;
        	*"Not supported"*)
			cfgcheck_active51
			;;
        	*)
			;;
	esac
}

#
# config_check
#
# Check the port configurations
#
cfgcheck50() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			cfgcheck_active50
			;;
        	*Local*Standby*)
			cfgcheck_standby50
			;;
        	*"Not supported"*)
			cfgcheck_active50
			;;
        	*)
			;;
	esac
}

#
# ad_present
#
# Check whether there are any long distance ports configured in LS mode.
#
ad_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			adcheck_active
			;;
        	*Local*Standby*)
			adcheck_standby
			;;
        	*"Not supported"*)
			adcheck_active
			;;
        	*)
			;;
	esac
}

#
# interop_present
#
# Check whether interop is configured
#
interop_present() {
	if [ -f /fabos/cliexec/hashow ];then
		hashow_cmd="/fabos/cliexec/hashow"
	else 
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			interopcheck_active
			;;
        	*Local*Standby*)
			interopcheck_standby
			;;
        	*"Not supported"*)
			interopcheck_active
			;;
        	*)
			;;
	esac
}

#
# fastwrite_present
#
# Check whether fastwrite is configured
#
fastwrite_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			fastwritecheck_active
			;;
        	*Local*Standby*)
			fastwritecheck_standby
			;;
        	*"Not supported"*)
			fastwritecheck_active
			;;
        	*)
			;;
	esac
}

#
# ipsec_present
#
# Check whether ipsec is configured
#
ipsec_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			ipseccheck_active
			;;
        	*Local*Standby*)
			ipseccheck_standby
			;;
        	*"Not supported"*)
			ipseccheck_active
			;;
        	*)
			;;
	esac
}

#
# tpipe_present
#
# Check whether tape pipelineing is configured
#
tpipe_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			tpipecheck_active
			;;
        	*Local*Standby*)
			tpipecheck_standby
			;;
        	*"Not supported"*)
			tpipecheck_active
			;;
        	*)
			;;
	esac
}


#
# dhcp_present
#
# Check whether dhcp is configured
#
dhcp_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			dhcpcheck_active
			;;
        	*Local*Standby*)
			dhcpcheck_standby
			;;
        	*"Not supported"*)
			dhcpcheck_active
			;;
        	*)
			;;
	esac
}

#
# fabwidestrict_present
#
# Check whether fabwide strict mode is configured
#
fabwidestrict_present() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			fabwidestrictcheck_active
			;;
        	*Local*Standby*)
			fabwidestrictcheck_standby
			;;
        	*"Not supported"*)
			fabwidestrictcheck_active
			;;
        	*)
			;;
	esac
}

#
# secmodeflag_check
#
# Check the value of sec.mode flag. If it's either 16 or 32, we are in
# non-secure mode, but the non-zero flag causes secTelnet/SSH become
# mandatory required in pre-5.2.
#
secmodeflag_check() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			secmodeflagcheck_active
			;;
        	*Local*Standby*)
			secmodeflagcheck_standby
			;;
        	*"Not supported"*)
			secmodeflagcheck_active
			;;
        	*)
			;;
	esac
}

#
# cfgsizecheck
#
# get the configuration size via cfgsize commane
#
cfgsizecheck() {
	hashow_cmd=$(ls /fabos/cliexec/hashow)
	if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
		hashow_cmd="/fabos/bin/hashow"
	fi

	case "$($hashow_cmd | ( read a; echo $a ))" in
        	*Local*Active*) 
			cfgsizecheck_active
			;;
        	*Local*Standby*)
			cfgsizecheck_standby
			;;
        	*"Not supported"*)
			cfgsizecheck_active
			;;
        	*)
			;;
	esac
}

#
# cfgsizecheck_active
#
# check the cfgsize on the active cp
# This will only run on 5.2 so we can safely expect the ad=255 output
#
cfgsizecheck_active() {
	PATH=/bin:/fabos/cliexec:/fabos/bin 
#	cfgsize | /bin/sed -n -e 's/.\+committed - //gp'
	SIZE=$(CURRENT_AD=0 cfgsize | /bin/sed -n -e 's/.\+committed - //gp')
#	SIZE=${SIZE%% bytes}
	echo $SIZE
}

#
# cfgsizecheck_standby
#
# check the cfgsize on the standby cp
#
cfgsizecheck_standby() {
	#cfgsize | /bin/sed -n -e 's/.\+committed - //gp'

	sync=`/fabos/bin/hashow | sed -n -e 's/^.\+\(State sync\).\+$/\1/gp'`
	if [ "$sync" != "State sync" ]; then
		return 0
	else
		#determine the active cp version so we know which binary to run
		ACTV_MJR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Major: | sed -n -e 's/Major://gp')
		ACTV_MNR=$(/usr/bin/rsh -n $(otherhost) /sbin/getfabosver | grep Minor: | sed -n -e 's/Minor://gp')

		if [ $ACTV_MJR == "5" -a $ACTV_MNR -ge 2 ]; then
			PATH=/bin:/fabos/cliexec:/fabos/bin
			SIZE=$(	/usr/bin/rsh -n $(otherhost)  ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 /fabos/bin/cfgsize | /bin/sed -n -e 's/.\+committed - //gp')
#			SIZE=${SIZE%% bytes}
			echo $SIZE
		else
			# Active already a version pre 5.2 we do not have to worry
			return 0
		fi
	fi
}

#
# Routing policy
#
# Check what routing policy it is. 
#
routing_policy() {
	/fabos/sbin/aptpolicy | sed -n -e 's/^ Cur.\+Policy: \([[:digit:]]\{1,\}\).*$/\1/gp'
}

#
# portmirror_present
#
# Check whether there are any port mirror connections enabled in the system.
# If there are not portmirror connections, portmirrorshow:
# 	switch:root> portmirrorshow
#	There are no port mirror connections configured
#
portmirror_present() {
	CMD='s/^mirror\.portmirrorcnt:\([0-9]*\).*$/\1/p' 
	PATH=/bin:/fabos/cliexec:/fabos/bin  configshow | sed -n -e "$CMD"
}

#
# agmode_enabled
#
# Check whether agmode is enabled
#
agmode_enabled() {
	if [ -f "/etc/fabos/ag_platform.0" ]; then
		return 0
	else
		return 1
	fi
}

# We only go n-2 versions 
# Since this handles the latest of two preinstall scripts
# this is the 5.2->5.0 scenario
handle_to_50()
{
	#	Put Software Requirements before hardware since the software
	#	might not be able to be reset after the hardware is pulled out

	#
	# Pre 5.2 checks software checks
	#
	$ECHO handle_to_50
	PORTMIRROR=`portmirror_present` 
	if [ ${PORTMIRROR:-0} != "0" ]; then
		add_err_status "$PORTMIRRORMSG"
		OVERALL_STS=$ERR_CONF_PORTMIRROR
	fi

	agmode_enabled
	AG_MODE=$?
	if [ $AG_MODE == 0 ]; then
		add_err_status "$AGMODEMSG"
		OVERALL_STS=$ERR_CONF_AGMODE
		return $OVERALL_STS
	fi

	ad_present
	AD=$?
	if [ $AD != "0" ]; then
		add_err_status "$ADMSG"
		OVERALL_STS=$ERR_CONF_AD
	fi

	fastwrite_present
	FASTWRITE=$?
	if [ $FASTWRITE != "0" ]; then
		add_err_status "$FASTWRITEMSG"
		OVERALL_STS=$ERR_CONF_FASTWRITE
	fi

	ipsec_present
	IPSEC=$?
	if [ $IPSEC != "0" ]; then
		add_err_status "$IPSECMSG"
		OVERALL_STS=$ERR_CONF_IPSEC
	fi

	tpipe_present
	TPIPE=$?
	if [ $TPIPE != "0" ]; then
		add_err_status "$TPIPEMSG"
		OVERALL_STS=$ERR_CONF_TPIPE
	fi

	dhcp_present
	DHCP=$?
	if [ $DHCP != "0" ]; then
		add_err_status "$DHCPMSG"
		OVERALL_STS=$ERR_CONF_DHCP
	fi

	fabwidestrict_present
	FABWIDESTRICT=$?
	if [ $FABWIDESTRICT != "0" ]; then
		add_err_status "$FABWIDESTRICTMSG"
		OVERALL_STS=$ERR_CONF_FABWIDESTRICT
	fi

	radiusmodecheck
	RADIUSMODE=$?
	if [ $RADIUSMODE != "0" ]; then
		add_err_status "$RADIUSMODEMSG"
		OVERALL_STS=$ERR_CONF_RADIUSMODE
	fi

	secmodeflag_check
	SECMODEFLAGNEW=$?
	if [ $SECMODEFLAGNEW != "0" ]; then
		add_err_status "$SECMODEFLAGMSG"
		OVERALL_STS=$ERR_CONF_SECMODE
	fi

	#
	# Check to see if port configuration is valid for 
	# various software issues arbitrary versions only
	# supported by 5.2
	#
	cfgcheck51
		
	PORTCFG=$?
	if [ $PORTCFG == "1" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG1

	elif [ $PORTCFG == "2" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG2

	elif [ $PORTCFG == "3" ]; then
		OVERALL_STS=$ERR_CONF_EXTRUNK
	fi

	CFGSIZE=$(cfgsizecheck)
	if [ -n "$CFGSIZE" ]; then
			if [ $CFGSIZE -gt 258798 ]; then
				add_err_status "$CFGSIZEMSG"
				OVERALL_STS=$ERR_CONF_CFGSIZE
			fi
	fi

	#
	# Pre 5.1 software checks
	#
	lsport_present

	LS_PORT=$?

	$ECHO The number of long distance port in LS mode is $LS_PORT
	if [ $LS_PORT != 0 ]; then
		add_err_status "$LONGDISTANCEMSG"
		OVERALL_STS=$ERR_CONF_LSPORTTYPE
	fi

	#
	# Check to see if port configuration is valid for 
	# various software issues arbitrary versions only
	# supported by 5.2
	#
	cfgcheck50
		
	PORTCFG=$?

	if [ $PORTCFG == "1" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG1
	elif [ $PORTCFG == "2" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG2
	fi

	# Software checks are done return status if not ok
	if [ $OVERALL_STS != $STS_OK ]; then
		return $OVERALL_STS
	fi

	#
	# Hardware checks are here. Return first for all software checks then hardware checks
	#

	ironman_present
	IRONMAN=$?
	if [ $IRONMAN != "0" ]; then
		add_err_status "$IRONMANMSG"
		OVERALL_STS=$ERR_CONF_BLADETYPE31
	fi

	positron_present
	POSITRON=$?
	if [ $POSITRON != "0" ]; then
		add_err_status "$POSITRONMSG"
		OVERALL_STS=$ERR_CONF_BLADETYPE36
	fi

	marathon_present
	MARATHON=$?
	if [ $MARATHON != "0" ]; then
		add_err_status "$MARATHONMSG"
		OVERALL_STS=$ERR_CONF_BLADETYPE24
	fi

	return $OVERALL_STS


}


# We only go n-2 versions 
# Since this handles the latest of two preinstall scripts
# this is the 5.2->5.1 scenario
handle_to_51()
{
	$ECHO handle_to_51

	#	Put Software Requirements before hardware since the software
	#	might not be able to be reset after the hardware is pulled out
	#
	# Pre 5.2 checks
	#
	PORTMIRROR=`portmirror_present` 
	if [ ${PORTMIRROR:-0} != "0" ]; then
		add_err_status "$PORTMIRRORMSG"
		OVERALL_STS=$ERR_CONF_PORTMIRROR
	fi

	agmode_enabled
	AG_MODE=$?
	if [ $AG_MODE == 0 ]; then
		add_err_status "$AGMODEMSG"
		OVERALL_STS=$ERR_CONF_AGMODE
		return $OVERALL_STS
	fi

	ad_present
	AD=$?
	if [ $AD != "0" ]; then
		add_err_status "$ADMSG"
		OVERALL_STS=$ERR_CONF_AD
	fi

	fastwrite_present
	FASTWRITE=$?
	if [ $FASTWRITE != "0" ]; then
		add_err_status "$FASTWRITEMSG"
		OVERALL_STS=$ERR_CONF_FASTWRITE
	fi

	ipsec_present
	IPSEC=$?
	if [ $IPSEC != "0" ]; then
		add_err_status "$IPSECMSG"
		OVERALL_STS=$ERR_CONF_IPSEC
	fi

	tpipe_present
	TPIPE=$?
	if [ $TPIPE != "0" ]; then
		add_err_status "$TPIPEMSG"
		OVERALL_STS=$ERR_CONF_TPIPE
	fi

	dhcp_present
	DHCP=$?
	if [ $DHCP != "0" ]; then
		add_err_status "$DHCPMSG"
		OVERALL_STS=$ERR_CONF_DHCP
	fi
	
	fabwidestrict_present
	FABWIDESTRICT=$?
	if [ $FABWIDESTRICT != "0" ]; then
		add_err_status "$FABWIDESTRICTMSG"
		OVERALL_STS=$ERR_CONF_FABWIDESTRICT
	fi

	radiusmodecheck
	RADIUSMODE=$?
	if [ $RADIUSMODE != "0" ]; then
		add_err_status "$RADIUSMODEMSG"
		OVERALL_STS=$ERR_CONF_RADIUSMODE
	fi

	secmodeflag_check
	SECMODEFLAGNEW=$?
	if [ $SECMODEFLAGNEW != "0" ]; then
		add_err_status "$SECMODEFLAGMSG"
		OVERALL_STS=$ERR_CONF_SECMODE
	fi

	#
	# Check to see if port configuration is valid for 
	# various software issues arbitrary versions only
	# supported by 5.2
	#
	cfgcheck51
		
	PORTCFG=$?

	if [ $PORTCFG == "1" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG1

	elif [ $PORTCFG == "2" ]; then
		OVERALL_STS=$ERR_CONF_PORTCFG2

	elif [ $PORTCFG == "3" ]; then
		OVERALL_STS=$ERR_CONF_EXTRUNK
	fi

	CFGSIZE=$(cfgsizecheck)
	if [ -n "$CFGSIZE" ]; then
			if [ $CFGSIZE -gt 258798 ]; then
				add_err_status "$CFGSIZEMSG"
				OVERALL_STS=$ERR_CONF_CFGSIZE
			fi
	fi


	#
	# Device based routine policy is not supported in 5.1
	#
	routing=`routing_policy 2> ${NULL}`

	if ! test -z $routing; then
	    if [ $routing == "2" ]; then
		add_err_status "$ROUTINGMSG"
		OVERALL_STS=$ERR_CONF_ROUTING
	    fi
	fi

	SWBD=`sin | swbd 2> ${NULL}`
	case ${SWBD##SWBD} in
		'21') 
			option=`$CHASSISCONFIG | chassisopt 2> ${NULL}`
			$ECHO "option is $option"

			if test -z $option; then
				add_err_status "No chassisconfig option found"
				OVERALL_STS=$ERR_CONF_CHASSIS
			fi

			if [ $option == "3" -o $option == "4" ] ; then
				add_err_status "Only platform options 1 and 2 are supported by version 5.1 with SWBD21. Use chassisconfig to reset the option before downloading the firmware."
				OVERALL_STS=$ERR_CONF_CHASSIS
			fi
			;;
	esac


	# Software checks are done return status if not ok
	if [ $OVERALL_STS != $STS_OK ]; then
		return $OVERALL_STS
	fi

	# HARDWARE REMOVAL tests here
	ironman_present
	IRONMAN=$?
	if [ $IRONMAN != "0" ]; then
		add_err_status "$IRONMANMSG"
		OVERALL_STS=$ERR_CONF_BLADETYPE31
	fi

	positron_present
	POSITRON=$?
	if [ $POSITRON != "0" ]; then
		add_err_status "$POSITRONMSG"
		OVERALL_STS=$ERR_CONF_BLADETYPE36
	fi

	return $OVERALL_STS
}

handle_mcdata()
{
	$ECHO handle_mcdata
	#
	# Block downgrade if this is not a SWBD58
	#
	SWBD=`sin | swbd 2> ${NULL}`
        case ${SWBD##SWBD} in
                '58')
			RET_CODE=$STS_OK
 			;;
		*)
	        NONPULSAR2_OK=`/fabos/bin/configshow | grep "switch.nonpulsar2ok__" | wc -l`
		if [ $NONPULSAR2_OK == "0" ]; then
		   RET_CODE=$ERR_NI_PLATFORM
		   add_err_status "$NIPLATFORMMSG"
		   return $RET_CODE
		fi
	        $ECHO "I am ok with $SWBD"
	        ;;	
	esac
	#
	# if I am in SFOS mode, I should not allow the firmwaredownload
	# to proceed. Print error message and return
    SFOS_MODE=`/fabos/cliexec/secmodeshow | grep "ENABLED" | wc -l`
	if [ $SFOS_MODE == "1" ]; then
	    add_err_status "$SFOS_ON_MSG"
	    return $ERR_CONF_SFOS_ON
	fi
	
	#
	# I am here because I am upgrading to 5.2.1_ni version & I am not in
	# mcdata fabric mode yet. I need to
	# 1) check to see if platform is enabled. if so, fail.
	# 2) ask ad to be cleared
	# 3) check to see if zone database has something. if so, fail.
	# 4) check to see if security is in strict mode. if so, fail.
	# 5) pid format should be 1.
	#
	NEED_PROMPT=0

	AD_NEED_CLEAR=0
    AD_MODE=`/fabos/bin/configshow | grep "^ad." | wc -l`
    if [ $AD_MODE != "0" ]; then
	    AD_NEED_CLEAR=1
	    NEED_PROMPT=1
    fi

	MCDATA_NEED_CLEAR=0
    INTEROP_MODE=`/fabos/bin/configshow | grep switch.interopMode | grep 0 | wc -l`
    if [ $INTEROP_MODE == "0" ]; then
	    MCDATA_NEED_CLEAR=1
    fi

    MCDATA_MODE=`/fabos/bin/configshow | grep switch.mcdtFabricMode | grep 1 | wc -l`

	SECURITY_NEED_CLEAR=0

    if [ $MCDATA_MODE == "0" ]; then
	    MCDATA_NEED_CLEAR=1
	    # no need to check for the msplatshow if we are already in mcdata mode
        MSPLAT_MODE=`/fabos/bin/msplatshow | grep "NOT enabled" | wc -l`
        if [ $MSPLAT_MODE != "1" ]; then
            add_err_status "$MCDATA_MSPLAT_ON_MSG"
            return $ERR_CONF_MCDATA_MSPLAT_ON
        fi

	    STRICT_SEC=`/fabos/abin/fddcfg --showall | grep "Fabric Wide Consistency Policy" | grep ":S" | wc -l`
	    if [ $STRICT_SEC == "1" ]; then
	        SECURITY_NEED_CLEAR=1
	        NEED_PROMPT=1
	    fi
    fi

	PID_FORMAT=`/fabos/bin/configshow | grep fabric.ops.mode.pidFormat | grep 1 | wc -l`
	PID_NEED_CLEAR=0
	if [ $PID_FORMAT == "0" ]; then
	    PID_NEED_CLEAR=1
	    NEED_PROMPT=1
	fi

	MY_DOMAIN=`/fabos/bin/configshow | grep fabric.domain | sed -e "s/fabric.domain://g"`
	MY_DOMAIN_NEED_RESET=0
	if [ $MY_DOMAIN -gt 31 ]; then
	    MY_DOMAIN_NEED_RESET=1
	    NEED_PROMPT=1
	fi

	if [ $MY_DOMAIN_NEED_RESET == 1 ]; then
	    add_err_status "$DOMAIN_RANGE_MSG"
	fi

	if [ $PID_NEED_CLEAR == 1 ]; then
	    add_err_status "$PID_CORE_MSG"
	fi

	if [ $AD_NEED_CLEAR == 1 ]; then
	    add_err_status "$AD_DB_MSG"
	fi

	if [ $SECURITY_NEED_CLEAR == 1 ]; then
	    add_err_status "$SECUR_STRICT_MSG"
	fi

	if [ $NEED_PROMPT == 1 ]; then
	        return $ERR_CONF_MCDATA_NOT_ACCEPTED
	fi

	return $STS_OK
}

# This is the upgrade scenario, we must echo since old codes and file format
# reading cannot be done.
handle_to_52()
{
	$ECHO handle_to_52

	#
	# Device based routine policy is not supported in 5.1
	#
	routing=`routing_policy 2> ${NULL}`

	if ! test -z $routing; then
	if [ $routing == "2" ]; then
		echo "Cannot download 5.2 because Device Based Routing policy is not supported by 5.2."
		echo "Use aptPolicy to change the routing policy before proceeding."
		return $ERR_CONF_ROUTING
		fi
	fi

	#
	# If agmode is enabled, dont' allow downgrade to 5.2.0
	#
	agmode_enabled
	AG_MODE=$?
	if [ $AG_MODE == 0 -a "$TO_PATCH" == "0" ]; then
		add_err_status "$AGMODEMSG"
		OVERALL_STS=$ERR_CONF_AGMODE
		return $OVERALL_STS
	fi

	SWBD=`sin | swbd 2> ${NULL}`
	case ${SWBD##SWBD} in
		'21') 
			option=`$CHASSISCONFIG | chassisopt 2> ${NULL}`
			$ECHO "option is $option"

			if test -z $option; then
				echo "No chassisconfig option found"
				return $ERR_CONF_CHASSIS
			fi

			if [ $option == "3" -o $option == "4" ] ; then
				echo "Only platform options 1, 2, 5 are supported by version 5.2."
				echo "Use chassisconfig to reset the option before downloading the firmware."
				return $ERR_CONF_CHASSIS
			fi
			;;
	esac

	#
	# Additional check for 5.2.1 ni
	#
	if [ ! -f $RELEASE_FILE ]; then
		return $STS_OK
	fi

	/bin/cat $RELEASE_FILE | /fabos/bin/grep $NI_VERSION_STR 1>/dev/null
	if [ $? == 0 ]; then

		handle_mcdata

		OVERALL_STS=$?

		return $OVERALL_STS
	fi

	return ${STS_OK}
}

#
# Preprocessing for fwdl from v5.2 to v4.4 or lower.  Two level downgrades not accepted.
#
handle_to_44_or_lower()
{
	$ECHO handle_to_44_or_lower
	add_err_status "Cannot downgrade to version 4.4 or lower. Please downgrade to 5.1 or 5.0 first and then download the desired version."
	return ${ERR_UNSUPPORTED_VER}

}


#
# main()
#
$ECHO Requested version is ${TO_MAJOR}.${TO_MINOR}.${TO_PATCH} Type is $TYPE

#
# clean up to free up disk space on /mnt before firmwaredownload
#
if [ -z $TYPE ]; then
	$ECHO removing tracedump.dmp
	rm -f /mnt/var/tracedump.dmp
fi


#
# Handle a specific firmware version
#
case "${TO_MAJOR}.${TO_MINOR}" in
	'5.2')
		handle_to_52
		RET=$?
	;;
	'5.1')
		handle_to_51
		RET=$?
	;;
	'5.0')
		handle_to_50
		RET=$?
	;;
	'4.4')
		handle_to_44_or_lower
		RET=$?
	;;
	'4.3')
		handle_to_44_or_lower
		RET=$?
	;;
	'4.2')
		handle_to_44_or_lower
		RET=$?
	;;
	'4.1')
		handle_to_44_or_lower
		RET=$?
	;;
	*)
	;;
esac

if [ $RET != $STS_OK ]; then
	add_err_status "Firmwaredownload command failed."
fi


#
# There is no preprocessing function, just return OK.
#
$ECHO return code is $RET
exit $RET
