#!/bin/bash 
#
#    Copyright (c) 2005-2013 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#	This post install script will be run after firmware 
#	download has been completed.
#
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
# Passed in version numbers
#
TO_MAJOR=$1
TO_MINOR=$2
TO_PATCH=$3

FROM_VER=`rpm -q fabos | cut -f 2 -d '-'`
FROM_MAJOR=`echo $FROM_VER|cut -d '.' -f 1`
FROM_MINOR=`echo $FROM_VER|cut -d '.' -f 2`
FROM_PATCH_STR=`echo $FROM_VER|cut -d '.' -f 3`
FROM_PATCH=${FROM_PATCH_STR:0:1}

#################################################################################
# Attention!                                            						# 
#																				#
# Please do not remove these lines in all future merges. This define indicates	#
# that firmware version supports optimized BE trunk deskew value 				#
UID_CONVERSION="Version Supports UID Conversion"
#################################################################################

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


STS_ERR=255

TATOOINE_UID_SUPPORT="udrole"
TATOOINE_FILEPERM_SUPPORT="ppermisson"
MNT_POSTINST="/mnt/sbin/postinst" # This is the new firmware being downloaded
POSTINST="/sbin/postinst"         # This is the current / running firmware

$ECHO Requested version is ${TO_MAJOR}.${TO_MINOR}.${TO_PATCH}
$ECHO Current version is ${FROM_MAJOR}.${FROM_MINOR}.${FROM_PATCH}

# untar ras man pages
# Unzip the rasman.tar on all platforms whose CF size is greater than 256MB.
# And remove the .gz files in the platform whose CF size is less than 256MB
# which were unzipped from rasman.tar by previous firmware.
swbd=`sin | awk ' {print $2} ' | grep SWBD | sed 's@,@@'`
total_cf=`df | awk ' NR==2 { print $2 } '`
# Defect 562160: Since CF size is compared, specify the minimum size to
# untar the rasman.tar -- 256MB * 1024 == 262144K
low_cf=262144
if [ $total_cf -gt $low_cf ]; then
	if [[ -e /mnt/fabos/man/cat7/rasman.tar ]]; then
		tar -xf /mnt/fabos/man/cat7/rasman.tar -C /mnt/fabos/man/cat7/ 2>/dev/null
		> /mnt/fabos/man/cat7/rasman.tar
	else 
		echo "rasman.tar does not exist."
	fi
else
	/bin/rm -f /mnt/fabos/man/cat7/*.gz 2>&1
fi

#Push Manufacturing key to SP
if [ -f '/fabos/libexec/spmPublicKeyUpdate' ] ; then
	/fabos/libexec/spmPublicKeyUpdate
fi
# Removing obsolete (4.2 and early) error log file in /mnt
#
cleanup_error_log()
{
if [ -f '/mnt/var/log/el_save.0.0' ] ; then
	/bin/rm -f /mnt/var/log/el_save.* 2>&1
	/bin/rm -f /var/log/el_save.* 2>&1
fi
if [ -f '/mnt/var/log/el_common.0' ] ; then
	/bin/rm -f /mnt/var/log/el_common.* 2>&1
	/bin/rm -f /var/log/el_common.* 2>&1
fi
}
#Pharos specific
#swbd=`sin | awk ' {print $2} ' | grep SWBD | sed 's@,@@'`
if [ $swbd == "SWBD117" ]; then
	ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`
	if [ "$ag_mode" -eq 0 ]; then
		/fabos/abin/ipsecConfig --enable
	fi
fi

# Recreate the trace dump file for the appropriate platforms.  These values must
# match those in the trace .c files.
trace_create_dump_file()
{
if [ -f '/mnt/fabos/cliexec/traceCreateDb' ] ;then
	/mnt/fabos/cliexec/traceCreateDb ${SWBD##SWBD} 2>/dev/null
fi
}

if [  -d /etc/fabos/mace/logger/stage ] ; then
	if [ -d /mnt/etc/fabos/mace/logger/stage ] ; then
		/bin/rm -rf /mnt/etc/fabos/mace/logger/stage
	fi
	cp -r /etc/fabos/mace/logger/stage /mnt/etc/fabos/mace/logger/stage
fi

#
#Copying sendmail to /mnt
#
cp /etc/mail/sendmail.cf /mnt/etc/mail/sendmail.cf

#
# Removing obsolete files in /mnt 
#
cleanup_error_log
/mnt/bin/bash /mnt/sbin/fwclean -o -s
#
# Workaround for missing secd issue.
#
if [ ! -f /mnt/fabos/libexec/secd ];then
	echo Creating secd
	/bin/touch /mnt/fabos/libexec/secd
fi

# Reestablish the links of the shared libraries
#
/usr/sbin/chroot /mnt /sbin/ldconfig 2>/dev/null
#

# Remove the persistent_NPIV_config file from both partitions
# if the platform do not support agd
#
if [ ! -e /etc/fabos/factory_ag.conf ] ; then
	/bin/rm -f /etc/fabos/persistent_NPIV_config
	/bin/rm -f /mnt/etc/fabos/persistent_NPIV_config
fi
#

# Ensure the module dependencies are correct, show undefined symbols
( /usr/sbin/chroot /mnt /sbin/depmod -e -F /boot/System.map ) 2>/dev/null

# handle the login PAM configuration files to be in sync with firmware version
handle_pam_conf() {

$ECHO "Handling PAM login configuration files"

CONF_DIR=/mnt/etc/pam.d
CONSOLE_CONF=$CONF_DIR/ttylogin
NET_CONF=$CONF_DIR/netlogin
SSH_CONF=$CONF_DIR/sshd

grep "pam_radius_auth.so" $NET_CONF > /dev/null 2>&1 
radius=$?

grep "pam_radius_auth.so" $NET_CONF | grep "optional" > /dev/null 2>&1
plus_local=$?

if [ $plus_local -eq 0 ]; then
	$ECHO "RADIUS + Local configuration"
	cp $CONF_DIR/login.radius-local $CONSOLE_CONF
	cp $CONF_DIR/login.radius-local $NET_CONF
	cp $CONF_DIR/sshd.radius-local $SSH_CONF
elif [ $radius -eq 0 ] && [ $plus_local -ne 0 ]; then
	$ECHO "RADIUS configuration"
	cp $CONF_DIR/login.radius-local $CONSOLE_CONF
	cp $CONF_DIR/login.radius $NET_CONF
	cp $CONF_DIR/sshd.radius $SSH_CONF
else
	$ECHO "Local configuration"
	cp $CONF_DIR/login.noradius $CONSOLE_CONF
	cp $CONF_DIR/login.noradius $NET_CONF
	cp $CONF_DIR/sshd.noradius $SSH_CONF
fi

#also copy the netlogin script. Otherwise, PDM overwrites the changes
#we made above.
cp /fabos/libexec/netlogincp /mnt/fabos/libexec/netlogincp

#With the PDM fix, the following two lines are no longer needed.
#cat /mnt/etc/fabos/sync.conf | /bin/sed -e 's%\(/etc/pam.d/[[:alpha:]]\{1,\}\)\+$%\1  /fabos/libexec/netlogincp%g'  > /mnt/etc/fabos/sync.conf.tmp
#mv /mnt/etc/fabos/sync.conf.tmp /mnt/etc/fabos/sync.conf

sync
}

#handle the files left in *.rpmsave state
copy_rpmsave() {
fname="$1"
$ECHO "Handling $1.rpmsave files"

if [ -f $fname.rpmsave ] && [ -f $fname ]; then
	mv $fname.rpmsave $fname
	sync
fi
}

# Defect 52841 & 56397 - Hot Code Load takes so long that neighbor Hello
# messages are not sent within the 80 seconds and switch
# gets segmented. This script starts a background task as part
# of firmware download that will refresh the Hello messages
# every two seconds up until the switch reboots or until
# a minute goes by (which ever comes first)

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

#
# cpid
#
# Retrieve the current Control Processor ID
#
cpid() {
    sed -n -e 's/^Control.\+No: \([[:digit:]]\{1,\}\)$/\1/gp'
}

# Determine the system platform identifier.
SWBD=$(sin | swbd 2> /dev/null)
CPID=$(sin | cpid 2> /dev/null)


otherhost(){
	case ${SWBD##SWBD} in
	'62')
	    printf 127.1.1.$((8 - CPID % 2))
	    ;;
	'77')
	    printf 127.1.1.$((6 - CPID % 2))
	    ;;
	'141')
		printf 127.1.$((17 - CPID % 2)).$((17 - CPID % 2))
		;;
	'142')
		printf 127.1.$((15 - CPID % 2)).$((15 - CPID % 2))
		;;
	*)
	    printf 10.0.0.$((6 - CPID % 2))
	    ;;
	esac
}

correcthost() {
    if [ $ACTIVECP ]; then
	"$@"
    else
	/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 CURRENT_AD=0 "$@"
    fi
}

#
# Helper functions
#



hashow_cmd=$(ls /fabos/cliexec/hashow)
if [ "$hashow_cmd" != "/fabos/cliexec/hashow" ]; then
    hashow_cmd="/fabos/bin/hashow"
fi


ha_role() {
    case "$($hashow_cmd | ( read a; echo $a ))" in
        *Local*Active*)
        printf "ACTIVE"
        ;;
        *Local*Standby*)
        printf "STANDBY"
        ;;
        *"Not supported"*)
        printf "ACTIVE"
        ;;
        *)
        printf "STANDBY"
        ;;
    esac
}

if [ $(ha_role) == "ACTIVE" ]; then
    ACTIVECP=1
	STANDBYCP=0
else
	STANDBYCP=1
fi

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

    if [ "$sync" != "State sync" ]; then
        printf "NOSYNC"
    else
        printf "SYNC"
    fi
}

NULL=/dev/null

ffdc_config(){
        RAS_FFDC_FILE=/etc/fabos/ras_ffdc.conf
        RAS_FFDC_FILE_MNT=/mnt/etc/fabos/ras_ffdc.conf
        slot_based=`/fabos/bin/slotshow | grep "Not supported on this platform"`

        if [ "$slot_based" == "" ]; then
               if [ $STANDBYCP -eq 1 ]; then
                       if [ -f $RAS_FFDC_FILE_MNT ]; then
				rasConfig=`correcthost_to_curr_vfid cat $RAS_FFDC_FILE | grep ENABLE| /usr/bin/awk  '{print $NF}'`
                                if [ "$rasConfig" -eq 0 ]; then
                                       sed '/%ENABLE/c\
%ENABLE 0' $RAS_FFDC_FILE_MNT  > temp && mv temp $RAS_FFDC_FILE_MNT
					/bin/cp $RAS_FFDC_FILE_MNT  $RAS_FFDC_FILE
                                        /bin/rcp $RAS_FFDC_FILE root@$(otherhost):$RAS_FFDC_FILE_MNT
                                       /bin/rcp $RAS_FFDC_FILE root@$(otherhost):$RAS_FFDC_FILE

                                fi
                        fi
                fi
        else
                if [ -f $RAS_FFDC_FILE_MNT ]; then
                         rasConfig=`cat $RAS_FFDC_FILE  | grep ENABLE | /usr/bin/awk  '{print $NF}'`
                        if [ "$rasConfig" -eq 0 ]; then
                                  sed '/%ENABLE/c \
%ENABLE 0' $RAS_FFDC_FILE_MNT  > temp && mv temp $RAS_FFDC_FILE_MNT
                        fi
                fi
        fi
}

IDENTIFY="cat /proc/ide/ide0/hda/identify"
MODEL="cat /proc/ide/hda/model"
INSMOD="/sbin/insmod /mnt/fabos/modules/wd.ko force=1"
checkId="3334 312d 3032 3330"

RSHCMD="/usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0"
RIDENTIFY="$RSHCMD $IDENTIFY"
RMODEL="$RSHCMD $MODEL"
RINSMOD="$RSHCMD $INSMOD"
RCPCMD="/bin/rcp /mnt/fabos/modules/wd.ko root@$(otherhost):/mnt/fabos/modules"

if [ -f /mnt/fabos/modules/wd.ko ];then
    FLASH=$($MODEL | grep -E "1GB|2GB|512MB" | grep -c "3625")
    if [ $FLASH -ne 0 ];then
        flash_verified=$($IDENTIFY | awk '{ if (NR == 3) { printf "%s ", $0 } if (NR == 4) { print $0 } }' | grep -c "$checkId")
        if [ $flash_verified -eq 0 ];then
            /sbin/insmod /mnt/fabos/modules/wd.ko force=1
        fi
    fi
    if [ $STANDBYCP -eq 1 ] && [ $(ha_state) == "SYNC" ] ; then
        ACTIVEFLASH=$($RMODEL | grep -E "1GB|2GB|512MB" | grep -c 3625)
        if [ $ACTIVEFLASH -ne 0 ];then
        	flash_verified=$($RIDENTIFY | awk '{ if (NR == 3) { printf "%s ", $0 } if (NR == 4) { print $0 } }' | grep -c "$checkId")
			if [ $flash_verified -eq 0 ];then
				($RCPCMD)
				($RINSMOD) > /dev/null 2>&1
			fi
		fi
	fi
fi

STS_OK=0
STS_ERR=255
CONTEXTS=""

CHASSISCMD=""
cliidx=0
export PATH=/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/factory:/fabos/xtool
export VFROLEMAP=-1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
export CHASSIS_ROLEID=0
if /fabos/bin/fosconfig --show | grep "Virtual Fabric" | grep enabled > /dev/null 2>&1 ; then VF_ENABLED=$STS_ERR; else VF_ENABLED=$STS_OK; fi

cmd_chassis() {

  context_switch chassis

  if [ "$CONFIG" = "TRUE" ]; then
    echo -n . > $TTY
    cliidx=`/usr/bin/expr $cliidx + 1`
    echo "$cliidx $1 $2 $3 $4 $5 $6 $7 $8 $9" >> $cshow
    echo -e "$1 $2 $3 $4 $5 $6 $7 $8 $9:"
      echo "** $1 $2 $3 $4 $5 $6 $7 $8 $9: Command:$rc **" >> $cshow
  else
    echo -e "$1 $2 $3 $4 $5 $6 $7 $8 $9:"
    $1 $2 $3 $4 $5 $6 $7 $8 $9
  fi
}

#
# check_vf_enabled
#
# Check whether VF is enabled
# both Active and Standby have this information
#
check_vf_enabled() {
    $CHASSISCMD /fabos/bin/fosconfig --show | grep "Virtual Fabric" | grep enabled > /dev/null 2>&1

    if [ $? -eq 0 ]; then
        return $STS_ERR
    else
        return $STS_OK
    fi

}

#
# both Active and Standby have the information of
# VF and logical switches configured.
## VF case: on Standby CP, in order to login to each logical switch, 
# we need to set CHASSIS_ROLEID FABOS_SWITCHNO CURRENT_VF in rsh.
#
correcthost_to_curr_vfid() {
    if [ $ACTIVECP ] ; then
        # for_all_context already did "context_switch --switch vfid"
        "$@"
    else
        if [ $VF_ENABLED -eq $STS_OK ]; then
            # VF disabled
            /usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CURRENT_AD=0 "$@"
        else
            # VF enabled, for_all_context changed glb_ls_id and glb_vf_id to each logical switch's
            /usr/bin/rsh -n $(otherhost) ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 FABOS_SWITCHNO=$glb_ls_id CURRENT_VF=$glb_vf_id CURRENT_AD=0 "$@"
        fi
    fi
}

correcthost_all_vfs() {
    if [ $VF_ENABLED -ne $STS_OK ]; then
        # VF is enabled
        if [ $ACTIVECP ] ; then
            for_all_contexts correcthost_to_curr_vfid $@
        else
            # this is the standby cp, examine the configuration of the
            # active cp
            for_all_contexts_standby correcthost_to_curr_vfid $@
        fi
    else
        correcthost_to_curr_vfid $@
    fi
    if [ $? -eq 0 ]; then
        return $STS_OK
    else
        return $STS_ERR
    fi
}

check_remove_nsfport_bm()
{
    nsfport_no=0
    removed_nsfport=0
    ispresent_nsfport_bm=0
	nsfport_totalports=0

    `/fabos/bin/configshow > /tmp/tmp_nsfport_bm`

    if [ -f /tmp/tmp_nsfport_bm ]; then

        nsfport_totalports=`cat /tmp/tmp_nsfport_bm | grep -c "ag.port.nsfporttopo."`

        if [ "$nsfport_totalports" -eq "0" ]; then
            $ECHO "check_remove_nsfport_bm: invalid total ports"
        else

            nsfport_totalports=`expr $nsfport_totalports - 1`
            while [ "$nsfport_no" -le "$nsfport_totalports" ];
            do
                ispresent_nsfport_bm=`cat /tmp/tmp_nsfport_bm | grep -c "ag.port.nsfporttopo.$nsfport_no:"`

                if [ "$ispresent_nsfport_bm" -eq "1" ]; then
                    /fabos/cliexec/config remove ag.port.nsfporttopo.$nsfport_no
                    removed_nsfport=1
                fi

	        nsfport_no=`expr $nsfport_no + 1`
            done

            if [ $removed_nsfport == "1" ]; then
                /fabos/cliexec/configcommit
            fi
        fi
        rm -f /tmp/tmp_nsfport_bm
	else
    $ECHO "check_remove_nsfport_bm: config updation failed"
    fi
}

context_switch()
{
    if [ "$1" != "chassis" ]
    then
        eval $(/fabos/cliexec/lscfg_util --switch $1)
    else
        eval $(/fabos/cliexec/lscfg_util --chassis)
        #export CHASSIS_ROLEID=0
    fi
    export ROLE_ID=root
}

# Helper function. It sets the global variable CONTEXTS to be a list of fabric
# IDs.  Typical usage is seen in the function for_all_contexts, where we use
# CONTEXTS if it is defined, and otherwise call this function. This means we
# only need to do the configshow once for each run of preinst, rather than once
# for each test that wants to run in all contexts
get_all_contexts(){
    CONTEXTS=$($CHASSISCMD /fabos/cliexec/configshow -all | sed -n 's/Fabric ID =//p')
    echo "$CONTEXTS"
}

glb_ls_id=-1
glb_vf_id=255
def_vf_id=128


# Helper function. Loop over the valid fabric IDs running the test that is
# passed as a parameter. As soon as one test fails then return $STS_ERR. If
# all pass then return $STS_OK.
# WARNING. This command will change to a random context (The first one that
# fails the test or the last context if all pass). Random terms in case someone
# is searching this file, VF Virtual Fabric AD setcontext FID context and
# wants to find this warning
for_all_contexts(){
    for FID in ${CONTEXTS:-$(get_all_contexts)} ; do
        context_switch $FID
        glb_vf_id=$CURRENT_VF; glb_ls_id=$FABOS_SWITCHNO
        if "$@"
        then    $ECHO -n "$FID-passed "
        else    $ECHO -n "$FID-failed "
                return $STS_ERR
        fi
    done
    return $STS_OK
}

for_all_contexts_standby(){
    rval=$STS_OK
    /bin/ln -s /fabos/cliexec/lscfg_util /fabos/link_sbin/lscfg_test 2> /dev/null
    local_flag=$?
    export PATH=/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/factory:/fabos/xtool
    lscfg_test --all | grep -e "[0-9]" | grep -v "Total" |
    while read glb_ls_id glb_vf_id role
    do
        context_switch $glb_vf_id
        if "$@"
        then    $ECHO -n "$glb_vf_id-passed "
        else    $ECHO -n "$glb_vf_id-failed "
            # note the following return just return from the "|" subshell
            return $STS_ERR
        fi
    done
    if [ $? -ne $STS_OK ]; then rval=$STS_ERR; fi
    if [ $local_flag -eq 0 ];then /bin/rm /fabos/link_sbin/lscfg_test 2> /dev/null; fi
    return $rval
}

#
# Helper functions (END)
#


# Take the list of ports that are in the FULL state and send a Hello
# But this needs to be done repeatedly because it could be awhile
# before the other steps of firmwaredownload are completed.
# This part will create a background task that resends the Hello messages
# every 2 seconds. This will be done for up to a minute.
#
# Input:
#    $iface_list - a list of port numbers
#

send_hellos()
{
 nohup /bin/bash <<'TO_HERE'
 ports=$iface_list
 declare -i reps=0

 while [ $reps -lt 30 ]
 do

  date

  for portnum in $ports
  do
   echo fspfHATest hloto $portnum
   ./fspfHATest hloto $portnum
  done

  sleep 2

  reps=$(($reps+1))

 done

TO_HERE

}

#
# set up configuration related to McDATA NI to specific contents
#
handle_mcdata()
{
	$ECHO "start mcdata"
    ZONE_ACTIVE=`/fabos/bin/cfgsize | grep "committed - 0" | wc -l`
	MCDATA_MODE=`/fabos/bin/configshow | grep switch.mcdtFabricMode | grep 1 | wc -l`
    if [ $ZONE_ACTIVE == "0" ] && [ $MCDATA_MODE == "0" ]; then
	    echo yes | /fabos/link_abin/cfgdisable > /dev/null
	    echo yes | /fabos/link_abin/cfgclear > /dev/null
	    echo yes | /fabos/link_abin/cfgsave > /dev/null
	    echo "Zone DB cleared"
    fi

	$ECHO "start interopmode"
	CONFIG_CHANGED=0
	INTEROP_MODE=`/fabos/bin/configshow | grep switch.interopMode | grep 0 | wc -l`
    if [ $INTEROP_MODE == "0" ]; then
	    #we need to set the mode to 0
	    /fabos/cliexec/config set switch.interopMode 2 0
	    CONFIG_CHANGED=1
    fi

	$ECHO "start mcdata mode"
    if [ $MCDATA_MODE == "0" ]; then
	    #we need to set the mode to 0
	    #we also need to set the wwn to McDATA
	    NEED_TO_SET_TO_MCDATA=1
	    MCDATA_WWN=`/fabos/link_bin/wwn | sed -e 's/\(..:..\):..:..:../\1:08:00:88/'`
	    echo no | ROLE_ID=root /fabos/link_bin/wwn $MCDATA_WWN > /dev/null
	    /fabos/cliexec/config set switch.mcdtFabricMode 2 1
	    echo "Setting to McDATA Fabric Mode"
	    CONFIG_CHANGED=1

	    $ECHO "start default domain id"
	    MY_DOMAIN=`/fabos/bin/configshow | grep fabric.domain | sed -e "s/fabric.domain://g"`
	    if [ $MY_DOMAIN -gt 31 ]; then
	        #we need to set the domain id to be 1 in such case
	        /fabos/cliexec/config set fabric.domain 2 1
	    echo "Domain ID of " $MY_DOMAIN "reset to value of 1"
        CONFIG_CHANGED=1
    fi

	    # security command doesn't work right if we are already in McDATA Fabric
	    $ECHO "start security"
	    STRICT_SEC=`/fabos/abin/fddcfg --showall | grep "Fabric Wide Consistency Policy" | grep ":S" | wc -l`
        if [ $STRICT_SEC == "1" ]; then
	        /fabos/cliexec/config set fdd.fabwidecfg 5 ""
	        echo "Security set to non-strict"
	        CONFIG_CHANGED=1
        fi
    fi

	$ECHO "start domain id offset"
	MCDATA_DO=`/fabos/bin/configshow | grep switch.domain_id_offset | wc -l`
	if [ $MCDATA_DO == "0" ]; then
	    #we don't have any domain ID offset set here. set to default
	    /fabos/cliexec/config set switch.domain_id_offset 2 96
	    echo "Domain ID Offset to default value of 96"
	    CONFIG_CHANGED=1
	fi

	$ECHO "start default zone"
	MCDATA_DZ=`/fabos/bin/configshow | grep switch.mcdtDefaultZone | wc -l`
	if [ $MCDATA_DZ == "0" ]; then
	    #we don't have any default zone set here. set to default
	    /fabos/cliexec/config set switch.mcdtDefaultZone 2 0
	    echo "Default Zone disabled by default"
	    CONFIG_CHANGED=1
	fi

	$ECHO "start safe zone"
	MCDATA_SZ=`/fabos/bin/configshow | grep switch.mcdtSafeZone | wc -l`
	if [ $MCDATA_SZ == "0" ]; then
	    #we don't have any default zone set here. set to default
	    /fabos/cliexec/config set switch.mcdtSafeZone 2 0
	    echo "Safe Zone disabled by default"
	    CONFIG_CHANGED=1
	fi

	$ECHO "start ad"
    AD_MODE=`/fabos/bin/configshow | grep "^ad." | wc -l`
    if [ $AD_MODE != "0" ]; then
	    echo yes | /fabos/link_abin/ad --exec 255 /fabos/link_abin/ad --clear -f > /dev/null
	    echo "AD DB cleared"
    fi

	$ECHO "start pid format"
    PID_FORMAT=`/fabos/bin/configshow | grep fabric.ops.mode.pidFormat | grep 1 | wc -l`
    if [ $PID_FORMAT == "0" ]; then
	    /fabos/cliexec/config set fabric.ops.mode.pidFormat 2 1
	    echo "PID format set to Core"
	    CONFIG_CHANGED=1
    fi

	$ECHO "start config commit"
    if [ $CONFIG_CHANGED == "1" ]; then
	    /fabos/cliexec/configcommit
	    echo "Saving config changes"
    fi

	$ECHO "mcdata done"
}

remove_fabric_name_key()
{
	/fabos/bin/configshow | grep -q fabric.name

	if [ $? -eq $STS_OK ]; then
		/fabos/cliexec/config remove fabric.name
		/fabos/cliexec/configcommit;
	fi
}

#check any QOS mode for all ports during upgrade
check_upgrade_qos_port_mode()
{
	qos_license=`/fabos/bin/licenseshow | grep "Adaptive Networking"`

	if [ "$qos_license" == "" ] ; then
	# Check for Virtual Fabrics
	check_vf_enabled
	if [ $? -ne $STS_OK ]; then
		CHASSISCMD="cmd_chassis" ;
		current_context=$CURRENT_VF ;
		for_all_contexts check_upgrade_qos_mode ;
		context_switch $current_context ;
	else
		check_upgrade_qos_mode ;
	fi

		rm -rf /tmp/tmp_switch_show.txt
		rm -rf /tmp/tmp_isl_show.txt
	fi
}

# This routine checks whether any of port has QoS mode auto enabled
# and if so, disable it.
#
# If AN and SAO licenses are installed on pre X-Wing switch:
#  - No QOS mode configuration changes will happen in this case 
#
# If AN and SAO licenses are not installed on pre X-Wing switch:
#  - If any port(s) has QOS mode set to AE, it is modified to OFF for that port(s).
#  - If any port(s) has QOS mode set to ON, there will not be any QOS mode change(s) for that port(s).
#  - If any E-Port is using QOS feature, there will not be any change(s) for that port.
#
# For ICL ports and Odin platform -  there will not be any QOS mode changes.
#
check_upgrade_qos_mode()
{
    # For Odin platform, these checks are not applicable
    if [ ${SWBD##SWBD} == '133' ]; then
        return $STS_OK
    fi

    SWITCH_SHOW=/tmp/tmp_switch_show.txt
    ISL_SHOW=/tmp/tmp_isl_show.txt

    correcthost_to_curr_vfid /fabos/bin/switchshow > $SWITCH_SHOW
    correcthost_to_curr_vfid /fabos/cliexec/islshow > $ISL_SHOW

    slot_based=`/fabos/bin/slotshow | grep "Not supported on this platform"`

        if [ "$slot_based" == "" ] ; then
         port_list=`cat $SWITCH_SHOW | grep " FC " | grep -Ev "AoQ|E-Port" | awk '{print $2"/"$3}'`
         e_port_index=`cat $SWITCH_SHOW | grep "E-Port" | awk '{print $1}'`
         e_port_list=`cat $SWITCH_SHOW | grep "E-Port" | awk '{print $3}'`
         e_port_slot=`cat $SWITCH_SHOW | grep "E-Port" | awk '{print $2}'`

         # check QOS mode is set to AE, if so disable it except for e-ports.
         for port in $port_list
         do
            qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow $port | grep "QOS" | awk '{print $3}'`
            if [ "$qos_mode" == "AE" ] ; then
                  correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable $port > /dev/null
            fi
            done

            index=0
            # extract each slot from e_slot list for e-ports.
            for slot in $e_port_slot
            do
                e_slot_array[$index]=$slot
                index=`expr $index + 1`
            done

            index=0
            # extract each port from e_port list for e-ports.
            for port in $e_port_index
            do
                e_port_array[$index]=$port
                index=`expr $index + 1`
            done

            index=0
            #Check whether E-port is actually running QOS feature or QOS mode is ON, if so do nothing; else disable QOS on that port.
            for port in $e_port_list
            do
            non_qos_eport=`cat $ISL_SHOW | grep "${e_port_array[$index]}->" | grep -Ev "QOS" |  cut -d '-' -f 1 | cut -d ':' -f 2`
                if [ "$non_qos_eport" != "" ] ; then
                    for eport in $non_qos_eport
                    do
                        if [ $eport -eq ${e_port_array[$index]} ] ; then
                         qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow ${e_slot_array[$index]}/$port | grep "QOS" | awk '{print $3}'`
                            if [ "$qos_mode" == "AE" ] ; then
                             correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable ${e_slot_array[$index]}/$port > /dev/null
                            fi

                            #Get the slave ports associated with the master port if trunking is enabled.
                         slave_port=`cat $SWITCH_SHOW | grep "Trunk port, master is" | grep " ${e_slot_array[$index]} " | grep " $port " | awk '{print $3}'`
                            if [ "$slave_port" != "" ] ; then
                               for sport in $slave_port
                               do
                                  #Change the QOS mode configuration of slave ports from AE to OFF.
                               qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow ${e_slot_array[$index]}/$sport | grep "QOS" | awk '{print $3}'`
                                  if [ "$qos_mode" == "AE" ] ; then
                                   correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable ${e_slot_array[$index]}/$sport > /dev/null
                                  fi
                               done
                            fi
                        fi
                    done
                fi
                index=`expr $index + 1`
            done

        else
        port_list=`cat $SWITCH_SHOW  | grep " FC " | grep -Ev "AoQ|E-Port" | awk '{print $1}'`
        e_port_list=`cat $SWITCH_SHOW  | grep "E-Port" | awk '{print $1}'`

            # check QOS mode is set to AE, if so disable it except for e-ports.
            for port in $port_list
            do
            qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow $port | grep "QOS" | awk '{print $3}'`
                if [ "$qos_mode" == "AE" ] ; then
                 correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable $port > /dev/null
                fi
            done

            #Check whether E-port is actually running QOS feature or QOS mode is ON, if so do nothing; else disable QOS on that port.
            for port in $e_port_list
            do
            non_qos_eport=`cat $ISL_SHOW | grep "$port->" | grep -Ev "QOS" |  cut -d '-' -f 1 | cut -d ':' -f 2`
                if [ "$non_qos_eport" != "" ] ; then
                    for eport in $non_qos_eport
                    do
                        if [ $eport -eq $port ]  ; then
                      qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow $port | grep "QOS" | awk '{print $3}'`
                            if [ "$qos_mode" == "AE" ] ; then
                          correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable $port > /dev/null
                            fi

                            #Get the slave ports associated with the master port if trunking is enabled.
                      slave_port=`cat $SWITCH_SHOW | grep "Trunk port, master is Port" | grep " $eport " | awk '{print $1}'`
                            if [ "$slave_port" != "" ] ; then
                                for sport in $slave_port
                                do
                                   #Change the QOS mode configuration of slave ports from AE to OFF.
                              qos_mode=`correcthost_to_curr_vfid /fabos/cliexec/portcfgshow $sport | grep "QOS" | awk '{print $3}'`
                                   if [ "$qos_mode" == "AE" ] ; then
                                   correcthost_to_curr_vfid /fabos/link_sbin/portcfgqos --disable $sport > /dev/null
                                   fi
                                done
                            fi
                        fi
                    done
                fi
            done
    fi
}


handle_losslessDLS_upgrade()
{
    policy=`/fabos/cliexec/config get rte.external_policy 2` ;
    iod=`/fabos/cliexec/config get route.delayReroute 2` ;

    #
    # If we have a port-based routing policy, and losslessDLS is set in IOD,
    # set the lossless value.  Otherwise, clear it.
    #
    if [ -n "$policy" ] && [ $policy -eq 1 ] && [ $iod -eq 2 ]; then
        /fabos/cliexec/config set route.lossless 2 1 ;
        /fabos/cliexec/configcommit ;
	else
        /fabos/cliexec/config set route.lossless 2 0 ;
        /fabos/cliexec/configcommit ;
    fi
}

# Need to check VF enabled and do the same for all logical switches.
remove_snmp_trap_change()
{
	correcthost /fabos/cliexec/config remove trackChanges.status
	correcthost /fabos/cliexec/config remove trackChanges.snmp-trap
	correcthost /fabos/cliexec/configcommit ;
}

remove_snmp_keys()
{   
	for  key in `/fabos/bin/configshow -c | grep snmp.64mibCap | cut -f 1 -d ':'`
	do
		/fabos/cliexec/config remove $key
	done

	for  key1 in `/fabos/bin/configshow -c | grep snmp.desSev | cut -f 1 -d ':'`
	do
		/fabos/cliexec/config remove $key1
	done


	key2=`/fabos/bin/configshow -c | grep snmp.portFilterBlockMask | cut -f 1 -d ':'`
	if [ $key2 != "0" ]; then	
		/fabos/cliexec/config remove $key2
	fi

	/fabos/cliexec/configcommit ;
}

handle_conv_keys_swstatus_upgrade()
{
	#
	# Search with pattern "FC" will include FCIP, FcoE, FC Routers etc.
	# To get the number of ports which of proto type FC,
	# searching for the pattern " FC  ". 
	#
	if [ $ACTIVECP ]; then
		portCnt=`/fabos/link_bin/switchshow | grep -c " FC  "` ;
	else
		return $STS_OK
	fi

	# Get MarginalPorts.down
	key=`/fabos/bin/configshow | grep policy.MarginalPorts.down`
	if [ "$key" != "" ]; then
		value=`echo $key | grep down | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
    	    value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.MarginalPorts.down" 5 $value_per ;
	fi

	# Get MarginalPorts.marginal
	key=`/fabos/bin/configshow | grep policy.MarginalPorts.marginal`
	if [ "$key" != "" ]; then
		value=`echo $key | grep marginal | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
			value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.MarginalPorts.marginal" 5 $value_per ;
	fi

	# Get MissingSFPs.down
	key=`/fabos/bin/configshow | grep policy.MissingSFPs.down`
	if [ "$key" != "" ]; then
		value=`echo $key | grep down | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
    	    value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.MissingSFPs.down" 5 $value_per ;
	fi

	# Get MissingSFPs.marginal
	key=`/fabos/bin/configshow | grep policy.MissingSFPs.marginal`
	if [ "$key" != "" ]; then
		value=`echo $key | grep marginal | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
	        value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.MissingSFPs.marginal" 5 $value_per ;
	fi

	# Get FaultyPorts.down
	key=`/fabos/bin/configshow | grep policy.FaultyPorts.down`
	if [ "$key" != "" ]; then
		value=`echo $key | grep down | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
	        value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.FaultyPorts.down" 5 $value_per
	fi

	 # Get FaultyPorts.marginal
	key=`/fabos/bin/configshow | grep policy.FaultyPorts.marginal`
	if [ "$key" != "" ]; then
		value=`echo $key | grep marginal | cut -f 2 -d ':'`
		if [ "$value" -eq 0 ]; then
			value_per=0.00
		elif [ "$value" -ge "$portCnt" ] || [ "$portCnt" -eq 0 ]; then
			value_per=100.00
		else
			AWKS=' { printf( "%3.2f\n", ($1*100/$2) ) } '
	        value_per=`echo $value $portCnt | awk "$AWKS"`
		fi
		/fabos/cliexec/config set "switch.status.policy.percentage.FaultyPorts.marginal" 5 $value_per ;
	fi

	/fabos/cliexec/configcommit ;
}
update_fwemail_oldkeys_upgrade()
{
	for  key in `/fabos/bin/configshow | grep switch.mail.agent | grep item_112`
        do
		value=`echo $key | grep item_112 | cut -f 2 -d ':'`
		agent=`echo $key | grep item_112 | cut -f 2 -d '_' | cut -f 1 -d '.'`
		/fabos/cliexec/config set "switch.mail.multi_rcpt.agent_$agent.item_112" 5 $value ;
        done

	for  key in `/fabos/bin/configshow | grep switch.mail.agent | grep item_113`
	do
		value=`echo $key | grep item_113 | cut -f 2 -d ':'`
		agent=`echo $key | grep item_113 | cut -f 2 -d '_' | cut -f 1 -d '.'`
		/fabos/cliexec/config set "switch.mail.multi_rcpt.agent_$agent.item_113" 5 $value ;
	done

	/fabos/cliexec/configcommit ;
}

#
# check whether the two input files are the same by comparing the checksum
# returns 0 if the files are same. Otherwise 1.
#
comp_file()
{
	f1="$1"
	f2="$2"
	result=1
	if [ -f $f1 ] && [ -f $f2 ]; then
		sum1=`md5sum $f1 |cut -f 1 -d ' '`
		sum2=`md5sum $f2 |cut -f 1 -d ' '`
		$ECHO "$f1: $sum1, $f2: $sum2"
		if [ $sum1 = $sum2 ]; then
			result=0
		fi
	fi
	return $result
}

cp_or_rm_alpa_files()
{
	if [ $ACTIVECP ]; then
		ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`

		if [ "$ag_mode" -eq 0 ]; then
			/bin/rm -f /etc/fabos/persistent_NPIV_config_6_4
			/bin/rm -f /mnt/etc/fabos/persistent_NPIV_config_6_4
			/bin/rm -f /etc/fabos/fcid_virt_config
			/bin/rm -f /mnt/etc/fabos/fcid_virt_config
		fi

		if [ "$ag_mode" -eq 1 ]; then
			/bin/cp /etc/fabos/persistent_NPIV_config_6_4 /mnt/etc/fabos/persistent_NPIV_config_6_4
			/bin/cp /etc/fabos/fcid_virt_config /mnt/etc/fabos/fcid_virt_config
		fi
	fi
}

# temporary workaround for changes in user.db file
if [ $TO_MAJOR -eq 5 ] && [ $TO_MINOR -eq 2 ]; then
	$ECHO "check for user.db upgrade"
	comp_file /mnt/etc/fabos/user.db.rpmnew /mnt/etc/fabos/user.db.default
	DIFF_UDB=$?
	if [ $DIFF_UDB == "0" ]; then
		mv /mnt/etc/fabos/user.db.rpmnew /mnt/etc/fabos/user.db
	fi
fi

# 
# Handle all pre 5.3 versions 
#
# Update Telnet in inetd.conf as neccessary

TELNET_ENTRY=`/bin/grep "^telnet    " /mnt/etc/inetd.conf | /usr/bin/cut -d' ' -f 1`
if [ "$TELNET_ENTRY" != "" ]; then
	/bin/cat /mnt/etc/inetd.conf | /bin/sed -e 's/^#telnet  /telnet /g' > /mnt/etc/inetd.conf
fi

#
# Handle all pre-5.1 versions
#
#if [ $TO_MAJOR -lt 5 -o $TO_MINOR -lt 1 ]; then
#	handle_pam_conf
#	RET=$?
#	if [ $RET -ne 0 ]; then
#		exit $RET 
#	fi
#fi

if [ $FROM_MAJOR -le 5 -a $FROM_MINOR -le 1 ]; then
	copy_rpmsave /mnt/etc/fabos/user.db
	if [ $TO_MAJOR -eq 5 ] && [ $TO_MINOR -eq 2 ]; then
		# The following step upgrades accounts with default AD member list
		/usr/sbin/chroot /mnt /fabos/libexec/passwd_convert /etc/passwd $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
	fi
fi

#Upgrade from < 8.0.1 to >=8.0.1
if [ $FROM_MAJOR -le 7 -a $FROM_MINOR -le 4 ] || [ $FROM_MAJOR -eq 8 -a $FROM_MINOR -eq 0 -a $FROM_PATCH -lt 1 ]; then
	if [ $TO_MAJOR -gt 8 ] || [ $TO_MAJOR -eq 8 -a $TO_MINOR -gt 0 ] || [ $TO_MAJOR -eq 8 -a $TO_MINOR -eq 0 -a $TO_PATCH -ge 1 ]; then
		CONVERT_FROM_MAJOR=7
		CONVERT_FROM_MINOR=4
		CONVERT_TO_MAJOR=8
		CONVERT_TO_MINOR=0
		/mnt/fabos/libexec/passwd_convert /mnt/etc/passwd $CONVERT_FROM_MAJOR.$CONVERT_FROM_MINOR $CONVERT_TO_MAJOR.$CONVERT_TO_MINOR > /dev/null 2>&1
		/bin/cp /mnt/etc/passwd /etc/passwd > /dev/null 2>&1
		/mnt/fabos/libexec/group_convert /mnt/etc/group $CONVERT_FROM_MAJOR.$CONVERT_FROM_MINOR $CONVERT_TO_MAJOR.$CONVERT_TO_MINOR > /dev/null 2>&1
		/bin/cp /mnt/etc/group /etc/group > /dev/null 2>&1
		/usr/sbin/chroot /mnt /bin/chown -R root:udrole /fabos/users/udrole
		/bin/chmod 600 /mnt/etc/shadow
		/bin/chmod 600 /mnt/etc/passwd
		/bin/chmod 600 /mnt/etc/fabos/opasswd
		/bin/chmod 600 /mnt/etc/ssh_config
		/bin/chmod 600 /mnt/etc/sshd_config
		/bin/chmod 600 /mnt/etc/fabos/user.db
		/bin/chmod 600 /mnt/etc/fabos/pwron_fips_status
		/bin/chmod 600 /mnt/etc/fabos/pki/switch.0.csr 
		/bin/chmod 600 /mnt/etc/fabos/pki/switch.0.key
		/bin/chmod 600 /mnt/etc/fabos/pki/switch.0.crt
		/bin/chmod 600 /mnt/etc/fabos/pki/switch.0.pp
		/bin/chmod 600 /mnt/etc/fabos/pki/switch.0.rootcrt
		/bin/chmod 600 /mnt/etc/fabos/pki/tp/switch.0.csr 
		/bin/chmod 600 /mnt/etc/fabos/pki/tp/switch.0.key
		/bin/chmod 600 /mnt/etc/fabos/pki/tp/switch.0.crt
		/bin/chmod 600 /mnt/etc/fabos/pki/tp/switch.0.pp
		/bin/chmod 600 /mnt/etc/fabos/pki/tp/switch.0.rootcrt
		if [ -f /mnt/root/.ssh/known_hosts ]; then
			/bin/chmod 600 /mnt/root/.ssh/known_hosts
		fi
	fi
fi

#Downgrade from >= 8.0.1 to < 8.0.1
if [ $FROM_MAJOR -gt 8 ] || [ $FROM_MAJOR -eq 8 -a $FROM_MINOR -gt 0 ] || [ $FROM_MAJOR -eq 8 -a $FROM_MINOR -eq 0 -a $FROM_PATCH -ge 1 ]; then
	if [ $TO_MAJOR -lt 8 ] || [ $TO_MAJOR -eq 8 -a $TO_MINOR -eq 0 -a $TO_PATCH -lt 1 ]; then
		CONVERT_FROM_MAJOR=8
		CONVERT_FROM_MINOR=0
		CONVERT_TO_MAJOR=7
		CONVERT_TO_MINOR=4
		correcthost_to_curr_vfid /fabos/libexec/passwd_convert /mnt/etc/passwd $CONVERT_FROM_MAJOR.$CONVERT_FROM_MINOR $CONVERT_TO_MAJOR.$CONVERT_TO_MINOR > /dev/null 2>&1
		correcthost_to_curr_vfid /bin/cp /mnt/etc/passwd /etc/passwd > /dev/null 2>&1
		correcthost_to_curr_vfid /fabos/cliexec/config save /etc/passwd 
		correcthost_to_curr_vfid /fabos/libexec/group_convert /mnt/etc/group $CONVERT_FROM_MAJOR.$CONVERT_FROM_MINOR $CONVERT_TO_MAJOR.$CONVERT_TO_MINOR > /dev/null 2>&1
		correcthost_to_curr_vfid /bin/cp /mnt/etc/group /etc/group > /dev/null 2>&1
		correcthost_to_curr_vfid /fabos/cliexec/config save /etc/group
	fi
fi

checksmmsuser()
{
	file="$1"
	hassmmsuser=`cat $file | grep "smmsp:" | wc -l`
    if [ $hassmmsuser -eq 0 ]; then
		echo 'smmsp:*:25:25:sendmail:/home/smmsp:/bin/false'>>$file;
	fi
}

checksmmsgrp()
{
file="$1"
hassmmsgrp=`cat $file | grep "smmsp:" | wc -l`
if [ $hassmmsgrp -eq 0 ]; then
echo 'smmsp::25:smmsp'>>$file
fi
}

#copying login config files from primary to seconday partition
#overwriting on the files, if any, copied by the rpm
cp /etc/passwd /mnt/etc/passwd
cp /etc/shadow /mnt/etc/shadow
cp /etc/group /mnt/etc/group
cp /etc/fabos/user.db /mnt/etc/fabos/user.db

# check for smmsp user and group and in the files and add entry if needed.
# the smmsp user is needed for the new sendmail package that is installed as
# part of f/w that has linux 2.6 kernel.
checksmmsuser /etc/passwd
checksmmsuser /etc/passwd.default
checksmmsgrp /etc/group
checksmmsgrp /etc/group.default
checksmmsuser /mnt/etc/passwd
checksmmsuser /mnt/etc/passwd.default
checksmmsgrp /mnt/etc/group
checksmmsgrp /mnt/etc/group.default

#log the FWDL from & to versions
cp /var/log/user.log /mnt/var/log/user.log
echo -e "\n`date` - FWDL from $FROM_MAJOR.$FROM_MINOR to $TO_MAJOR.$TO_MINOR\n" >> /mnt/var/log/user.log

if [ ! -e /mnt/etc/fabos/mii.cfg ]; then
	if [ -e /mnt/etc/fabos/mii.0.cfg ]; then
		cp /mnt/etc/fabos/mii.0.cfg /etc/fabos/mii.cfg
		cp /mnt/etc/fabos/mii.0.cfg /mnt/etc/fabos/mii.cfg
	elif [ -e /mnt/etc/fabos/mii.1.cfg ] ; then
		cp /mnt/etc/fabos/mii.1.cfg /etc/fabos/mii.cfg
		cp /mnt/etc/fabos/mii.1.cfg /mnt/etc/fabos/mii.cfg
	fi
fi

if [ $FROM_MAJOR -le 6 -a $FROM_MINOR -lt 4 ]; then
	# The following step upgrades accounts with default AD member list
	/usr/sbin/chroot /mnt /fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
fi

#upgrading from <6.2 to 6.2 
if [ $FROM_MAJOR -le 6 -a $FROM_MINOR -le 1 ]; then
	#
	# clean up files. These files will be re-created when the
	# switch is running is 6.2
	#
	/bin/rm -f /etc/fabos/port2Bound*
	/bin/rm -f /etc/fabos/config/switch-conf.*
	/bin/rm -f /mnt/etc/fabos/port2Bound*
	/bin/rm -f /mnt/etc/fabos/config/switch-conf.*

	# Set VF mode to off (this line has to be after the rm)
	/usr/sbin/chroot /mnt /sbin/setvf 0

	# Now update switch_conf file port trunk area map
	/usr/sbin/chroot /mnt /sbin/settrunkareamap /etc/fabos/trunk_area.0 6.1.0 6.2.0 
	# The following step upgrades accounts with default AD member list
	/usr/sbin/chroot /mnt /fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1

	SWNAME=`ROLE_ID=root /fabos/bin/switchname`
	/usr/sbin/chroot /mnt /sbin/setswname 0 $SWNAME

	/bin/cp /mnt/etc/fabos/config/switch-conf.${SWBD##SWBD} /mnt/etc/fabos/config/switch-conf.${SWBD##SWBD}.fwdl

fi


#fwdl from 7.0 to 7.0
if [ $FROM_MAJOR -eq 7 -a $FROM_MINOR -eq 0 ]; then
        if [ $TO_MAJOR -eq 7 -a $TO_MINOR -eq 0 ]; then
		cp_or_rm_alpa_files ;
	fi
fi

#upgrading from any 6.x version prior to v6.4 to 6.4 or higher
if [ $FROM_MAJOR -eq 6 -a $FROM_MINOR -lt 4 ]; then
	if [ $TO_MAJOR -eq 6 -a $TO_MINOR -ge 4 ]; then
		#
		# Put the pre-6.4 losslessDLS config into 6.4 values.
		#
		# Check for Virtual Fabrics
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
			# For some reason, not equal to STS_OK means VF is enabled
			CHASSISCMD="cmd_chassis" ;
			current_context=$CURRENT_VF ;
			for_all_contexts handle_losslessDLS_upgrade ;
			context_switch $current_context ;
		else
			# echo "NON-VF" ;
			handle_losslessDLS_upgrade ;
		fi

		remove_snmp_keys ;
	fi
fi

# Defect 322065
# Removing switch.xisluse key
remove_swattr_key() {
	/fabos/cliexec/configshow | grep -qi "switch.xisluse"
	if [ $? -eq 0 ]; then
		/fabos/cliexec/config remove "switch.xisluse"
		/fabos/cliexec/configcommit
	fi
}

# Defect 322065
# Removing switch.xisluse key while downgrading from Matador to
# previous version of firmware
if [ $FROM_MAJOR -ge 7 -a $FROM_MINOR -ge 0 ]; then
	if [ $TO_MAJOR -le 6 ]; then
		check_vf_enabled
		if [ $? -eq $STS_ERR ]; then
			current_context=$CURRENT_VF
			for_all_contexts remove_swattr_key	
			context_switch $current_context
		fi

		# Remove obsolete files
		/bin/rm -f /etc/fabos/trunk_area*
		/bin/rm -f /mnt/etc/fabos/trunk_area*
	fi
fi

# Removing vs.vpwwn key while downgrading from Matador to
# previous version of firmware
if [ $FROM_MAJOR -ge 7 -a $FROM_MINOR -ge 0 ]; then
	if [ $TO_MAJOR -le 6 ]; then
		/fabos/cliexec/config remove vs.vpwwn
	fi
fi

#upgrading from 6.2/6.3 to higher
if [ $FROM_MAJOR -eq 6 -a $FROM_MINOR -ge 2 -a $FROM_MINOR -le 3 ]; then
	# get rid of stray vf-conf.xx
	/bin/rm -f /etc/fabos/config/vf-conf.${SWBD##SWBD}
	/bin/rm -f /mnt/etc/fabos/config/vf-conf.${SWBD##SWBD}

	# Remove obsolete files
	/bin/rm -f /etc/fabos/trunk_area*
	/bin/rm -f /mnt/etc/fabos/trunk_area*
	
	if [ $ACTIVECP ]; then	
	ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`

	#when AG mode not enabled, remove ALPA files when upgrading from 
	#6.3 to 6.4
		if [ "$ag_mode" -eq 0 ]; then
		/bin/rm -f /etc/fabos/persistent_NPIV_config
		/bin/rm -f /mnt/etc/fabos/persistent_NPIV_config
	fi
fi
fi


#upgrading from >=6.1 to >=6.2 
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -ge 1 ]; then
	if [ $TO_MAJOR -ge 6 -a $TO_MINOR -ge 4 ]; then
		# The following step enable the PermitUserEnvironment variable in sshd_config file
		#Enabling PermitUserEnvironment
		sed "s/#PermitUserEnvironment no/PermitUserEnvironment yes/" /mnt/etc/sshd_config > /tmp/sshd_tmp
		cat /tmp/sshd_tmp > /mnt/etc/sshd_config

		#Enabling UsePAM
		sed "s/#UsePAM no/UsePAM yes/" /mnt/etc/sshd_config > /tmp/sshd_tmp
		cat /tmp/sshd_tmp > /mnt/etc/sshd_config
		
		#Disabling Challelenge Response Authentication
		sed "s/#ChallengeResponseAuthentication yes/ChallengeResponseAuthentication no/" /mnt/etc/sshd_config > /tmp/sshd_tmp
		cat /tmp/sshd_tmp > /mnt/etc/sshd_config
		rm /tmp/sshd_tmp
	fi
fi

#downgrading from >=6.2 to <6.2
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -ge 2 ]; then
	if [ $TO_MAJOR -le 6 -a $TO_MINOR -le 1 ]; then	
		# The following step upgrades accounts with default AD member list
		/sbin/getvf
		if [ $? == 1 ]; then
			/usr/sbin/chroot /mnt /sbin/passwddefault > /dev/null 2>&1
		else
			/fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
		fi

		#
		# remove some obsolete file. They only exist in 6.2
		#
		/bin/rm -f /mnt/etc/fabos/config/switch-conf*
		/bin/rm -f /mnt/etc/fabos/port2Bound*

		#Following steps will disable the PermitUserEnvironment variable in sshd_config file
		sed "s/PermitUserEnvironment yes/#PermitUserEnvironment no/" /mnt/etc/sshd_config > /tmp/tmp
        cat /tmp/tmp > /mnt/etc/sshd_config
        rm /tmp/tmp
	fi	
fi

#downgrading from >=6.3 to <6.3
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -ge 3 ]; then
	if [ $TO_MAJOR -le 6 -a $TO_MINOR -le 2 ]; then	

		#
		# remove some obsolete file. They only exist in 6.3
		#
		/bin/rm -f /etc/fabos/wwn2*
		/bin/rm -f /mnt/etc/fabos/wwn2*
	fi	
fi

# Kestrel2 Specific
if [ "$SWBD" == "SWBD87" ]; then
        /bin/rm -f /fabos/share/fwdltime
        /bin/rm -f /mnt/fabos/share/fwdltime
        date +"%m/%d/%Y" >> /fabos/share/fwdltime
        date +"%m/%d/%Y" >> /mnt/fabos/share/fwdltime
fi

#downgrading from 7.0 to 6.3
if [ $FROM_MAJOR -eq 7 ] && [ $FROM_MINOR -eq 0 ]; then
        if [ $TO_MAJOR -eq 6 -a $TO_MINOR -eq 3 ]; then

                if [ $ACTIVECP ]; then
                        ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`

                        #when AG mode not enabled, remove the FCID files in native mode
                        #when downgrading from 6.4 to 6.3
                        if [ "$ag_mode" -eq 0 ]; then
                                /bin/rm -f /etc/fabos/persistent_NPIV_config_6_4
                                /bin/rm -f /mnt/etc/fabos/persistent_NPIV_config_6_4
                                /bin/rm -f /etc/fabos/fcid_virt_config
                                /bin/rm -f /mnt/etc/fabos/fcid_virt_config
                        fi

                        if [ "$ag_mode" -eq 1 ]; then
                                /fabos/bin/ag --fw_downgrade
                        fi
        fi

        # Remove the files /etc/fabos/auth.[0-7].fcap.conf and
        # /mnt/etc/fabos/auth.[0-7].fcap.conf
        for f in /etc/fabos/auth.[0-7].fcap.conf /mnt/etc/fabos/auth.[0-7].fcap.conf 
        do
            if [ -e $f ]; then
                /bin/rm -f $f
            fi
        done
fi
fi


if [ "$SWBD" == "SWBD62" -o "$SWBD" == "SWBD67" ]
then
	if [ -f /mnt/etc/fabos/mace/kvcredentials.cfg ]
	then
		if [ $FROM_MAJOR -eq  6 -a $TO_MAJOR  -gt 6 ]
		then
			/bin/cp /mnt/etc/fabos/mace/kvcredentials.cfg /tmp/kvcredentials.cfg 2>/dev/null 
			/usr/bin/openssl  enc -d -aes-256-cbc -a -pass pass:Password1 -in /tmp/kvcredentials.cfg -out /tmp/kvcredentials.cfg.decrypt 2>/dev/null
			KV_CRED_CHECK=`/bin/cat /tmp/kvcredentials.cfg.decrypt |wc -c`
			#check if kvcredential file is  not encrypted then encrypt it.
			if [ ${KV_CRED_CHECK} -eq 0 ]
			then
				/usr/bin/openssl enc -aes-256-cbc -a -pass pass:Password1 -in /tmp/kvcredentials.cfg -out /mnt/etc/fabos/mace/kvcredentials.cfg  2>/dev/null
				 cp /mnt/etc/fabos/mace/kvcredentials.cfg /etc/fabos/mace/kvcredentials.cfg -f
			fi
			/bin/rm /tmp/kvcredentials.cfg /tmp/kvcredentials.cfg.decrypt 2>/dev/null
		elif [ $FROM_MAJOR -gt 6 -a $TO_MAJOR -le 6 ]
		then
			/bin/cp /mnt/etc/fabos/mace/kvcredentials.cfg /tmp/kvcredentials.cfg 2>/dev/null
			/usr/bin/openssl enc -d -aes-256-cbc -a -pass pass:Password1 -in /tmp/kvcredentials.cfg -out /tmp/kvcredentials.cfg.decrypt 2>/dev/null
			KV_CRED_CHECK=`/bin/cat /tmp/kvcredentials.cfg.decrypt |wc -c`
			#check if kvcredential file is   encrypted then decrypt it.
			if [ ${KV_CRED_CHECK} -ne 0 ]
			then
				/usr/bin/openssl enc -d -aes-256-cbc -a -pass pass:Password1 -in /tmp/kvcredentials.cfg -out /mnt/etc/fabos/mace/kvcredentials.cfg 2>/dev/null
				cp /mnt/etc/fabos/mace/kvcredentials.cfg /etc/fabos/mace/kvcredentials.cfg -f
			fi
			/bin/rm /tmp/kvcredentials.cfg /tmp/kvcredentials.cfg.decrypt 2>/dev/null
		fi
	fi
fi

#downgrading from >=6.4 to <=6.3
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -ge 4 ]; then	
	if [ $TO_MAJOR -le 6 -a $TO_MINOR -le 3 ]; then	
		#
		# remove vf-conf.bd to avoid stray vf-conf
		#
		/bin/rm -f /mnt/etc/fabos/config/vf-conf.${SWBD##SWBD}

		# When downgrading to a version below 6.4 remove the
		# port2BoundAreaMap_v1 files to avoid stale configuration when we
		# upgrade later e.g. 6.4 -> 6.3 -> 6.4
		/bin/rm -f /mnt//etc/fabos/port2BoundAreaMap_v1.*
	fi	

	# Remove obsolete files
	/bin/rm -f /etc/fabos/trunk_area*
	/bin/rm -f /mnt/etc/fabos/trunk_area*
fi

#downgrading from >=6.4 to <6.4
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -ge 4 ]; then
	if [ $TO_MAJOR -le 6 -a $TO_MINOR -le 3 ]; then	
		#
		# We dont want the per port NPIV value key to exist.
		#
		for f in /mnt/etc/fabos/fabos.*.conf
		do
			awk 'match($0,"portNpivLimit") == 0 {print $0}' $f > tmpfile
			cp tmpfile $f
		done
		rm -f tmpfile
		for f in /etc/fabos/fabos.*.conf
                do
                        awk 'match($0,"portNpivLimit") == 0 {print $0}' $f > tmpfile
                        cp tmpfile $f
                done
                rm -f tmpfile
				if [ -f '/fabos/libexec/syslog_cfg' ] ; then
					/fabos/libexec/syslog_cfg -R
				fi
	fi	
fi

#Retain the non-secure syslog ip to < 7.4
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -lt 4 ]; then
	/fabos/sbin/syslogadmin --update
fi

#upgrading from <6.4 to >=6.4
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -le 3 ]; then
	if [ $TO_MAJOR -eq 6 -a $TO_MINOR -ge 4 ]; then	
		#
		# We dont want the per port NPIV value key to exist.
		#
		for f in /mnt/etc/fabos/fabos.*.conf
		do
			awk 'match($0,"portNpivLimit") == 0 {print $0}' $f > tmpfile
			cp tmpfile $f
		done
		rm -f tmpfile
		for f in /etc/fabos/fabos.*.conf
		do
			awk 'match($0,"portNpivLimit") == 0 {print $0}' $f > tmpfile
			cp tmpfile $f
        done
		rm -f tmpfile
	fi	
fi

#Upgrading from <7.2 to 7.2
if [ $FROM_MAJOR -eq 7 -a $FROM_MINOR -lt 2 ]; then
        if [ $TO_MAJOR -eq 7 -a $TO_MINOR -ge 2 ]; then
        	check_upgrade_qos_port_mode
        fi
fi


#Downgrading from 7.0 to 6.4
if [ $FROM_MAJOR -eq 7 ] && [ $FROM_MINOR -eq 0 ]; then
	if [ $TO_MAJOR -eq 6 -a $TO_MINOR -ge 4 ]; then	

		if [ ${SWBD##SWBD} == 62 -o ${SWBD##SWBD} == 77 ]; then		
			/sbin/setpmap
		fi
		cp_or_rm_alpa_files ;
	fi
fi

#Upgrading from 6.4 to 7.0
if [ $FROM_MAJOR -eq 6 ] && [ $FROM_MINOR -eq 4 ]; then
        if [ $TO_MAJOR -eq 7 -a $TO_MINOR -eq 0 ]; then
		cp_or_rm_alpa_files ;
        fi
fi

SSHD_CONFIG=/etc/sshd_config
IMPORT_PUBKEY_DIR=/fabos/users/admin/.ssh
IMPORT_PUBKEY_FILE=authorized_keys
MNT_IMPORT_PUBKEY_DIR=/mnt/fabos/users/admin/.ssh

#
# Defect000618708
# User defined members are added to udrole if udrole is available and
# members wrere created. 

check_vf_enabled
if [ $? -eq $STS_OK ]; then
		udmembers=`correcthost grep "udrole::" /etc/group | wc -c`
		if [ $udmembers -le 13 ]; then
			udrole=`correcthost grep "udrole::" /etc/group`
			first=1
			declare -a def_list=("factory" "user" "admin" "switchadmin" "zoneadmin" "fabricadmin" "basicswitchadmin" "securityadmin" "operator" "root")
			role_list=`correcthost /fabos/link_bin/userconfig --show -a | grep Role | awk '{print $2}' | uniq`
			for role in $role_list
				do
				def_flag=0
				for default in "${def_list[@]}"
					do
					if [ $role == $default ]; then
						def_flag=1
						break
					fi
				done  
    			if [ $def_flag -eq 1 ]; then
        			continue
    			fi   

				name_list=`correcthost /fabos/link_bin/userconfig --show -r $role | grep "Account name" | awk '{print $3}'`
				for name in $name_list
					do
					if [ $first -eq 1 ]; then
						name1="$name"
						first=0
					else
						name1="$name1,$name"
					fi       
				done
			done
			udrole="$udrole$name1" 
			`correcthost sed -e 's/udrole::550:/'$udrole'/g' /etc/group > /tmp/tmp1`
			`correcthost sed -e 's/udrole::550:/'$udrole'/g' /mnt/etc/group > /tmp/tmp2`
			if [ $STANDBYCP -eq 1 ] ; then
				rcp /tmp/tmp1 root@$(otherhost):/etc/group
				rcp /tmp/tmp2 root@$(otherhost):/mnt/etc/group
				/bin/rm /tmp/tmp1
				/bin/rm /tmp/tmp2
			else
				/bin/mv /tmp/tmp1 /etc/group
				/bin/mv /tmp/tmp2 /mnt/etc/group
			fi
		fi
fi

#upgrading from <7.0 to >=7.0
if [ $FROM_MAJOR -lt 7 ] && [ $FROM_MINOR -le 4 ]; then
	if [ $TO_MAJOR -eq 7 -a $TO_MINOR -ge 0 ]; then
		#
		# To convert old config keys values of Marginal, Faulty and
		# Missing SFPs components of switchstatuspolicy which is
		# absolute value to new config key which is percentage of
		# number of physical ports present in the switch at a
		# given instance of time
		#
        # Check for Virtual Fabrics
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
		# For some reason, not equal to STS_OK means VF is enabled
			CHASSISCMD="cmd_chassis" ;
			current_context=$CURRENT_VF ;
			for_all_contexts handle_conv_keys_swstatus_upgrade ;
			for_all_contexts update_fwemail_oldkeys_upgrade ;
			context_switch $current_context ;
		else
			# echo "NON-VF" ;
			handle_conv_keys_swstatus_upgrade ;
			update_fwemail_oldkeys_upgrade ;
		fi
                # public key ssh authetication is enhanced to support all
                # users from release 7.0. If authorized_keys file is present,
                # rename it to authorized_keys.<username> and also create a tar
                # file authorizedKeys.tar which has authorized_keys.<username>
                # in tar format.
                if [ -f $IMPORT_PUBKEY_DIR/$IMPORT_PUBKEY_FILE ]; then
                        allowlogin=`cat $SSHD_CONFIG|grep -i "AllowedLogin" |\
                                   grep -v "#" |cut -d" " -f2`
                        cd $MNT_IMPORT_PUBKEY_DIR
                        cp $IMPORT_PUBKEY_FILE $IMPORT_PUBKEY_FILE.$allowlogin
                        tar -cf authorizedKeys.tar $IMPORT_PUBKEY_FILE.*
                        cd -
                fi
	fi
fi


#downgrading from >= 7.0 to <= 6.4
if [ $FROM_MAJOR -ge 7 ] && [ $FROM_MINOR -ge 0 ]; then
	if [ $TO_MAJOR -le 6 -a $TO_MINOR -le 4 ]; then

		#
		# Remove static fport bitmap
		#
		check_remove_nsfport_bm;

		#
		# Remove fabric.name
		#
		# Check for Virtual Fabrics
		check_vf_enabled

		if [ $? -ne $STS_OK ]; then
			# For some reason, not equal to STS_OK means VF is enabled
			CHASSISCMD="cmd_chassis" ;
			current_context=$CURRENT_VF ;
			for_all_contexts remove_fabric_name_key ;
			context_switch $current_context ;
		else
			# echo "NON-VF" ;
			remove_fabric_name_key ;
		fi
                # public key ssh authetication is enhanced to support all
                # users from release 7.0. If authorized_keys.* files are
                # present, remove them. Also remove authorizedKeys.tar.
                cd $MNT_IMPORT_PUBKEY_DIR
                rm -f $IMPORT_PUBKEY_FILE.* 2> /dev/null
                rm -f authorizedKeys.tar 2> /dev/null
                cd -
            	
                # Removing id_rsa* files.
                rm -f /mnt/root/.ssh/id_rsa* 2> /dev/null

		# Remove new-format Bottleneck Detection configuration files
		rm -f /mnt/etc/fabos/bneck.*.cfg > /dev/null 2>&1
	fi
fi

#FWDL from 7.x to 7.y
if [ $FROM_MAJOR -eq 7 ] && [ $TO_MAJOR -eq 7 ]; then
    if [ $FROM_MINOR -le $TO_MINOR ]; then
        #FWDL from 7.x to 7.y where x<=y
        /usr/sbin/chroot /mnt /fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
        else
            #FWDL from 7.x to 7.y where x>y
            /fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
        fi
		cp /mnt/etc/fabos/rbac/dynamic /etc/fabos/rbac/dynamic

	# Remove obsolete files
	/bin/rm -f /etc/fabos/trunk_area*
	/bin/rm -f /mnt/etc/fabos/trunk_area*

    # Determine if portCfg and lsportCfg need to have garbage bits removed.
    # Need to do this on the Active CP.
    if [ $STANDBYCP -eq 1 ] && [ -e /mnt/sbin/clean_portcfg ] ; then
		if [ $FROM_MINOR -le 2 ] ; then
        	/bin/rcp /mnt/sbin/clean_portcfg root@$(otherhost):/mnt/sbin
		fi
    fi
    if [ -e /mnt/sbin/clean_portcfg ] ; then
		if [ $FROM_MINOR -le 2 ] ; then
        	correcthost_all_vfs /mnt/sbin/clean_portcfg $FROM_MAJOR $FROM_MINOR $TO_MAJOR $TO_MINOR
		fi
    fi
fi

ffdc_config

#Upgrade from <7.0 to 7.x
if [ $FROM_MAJOR -lt 7 ] && [ $TO_MAJOR -eq 7 ]; then
        /usr/sbin/chroot /mnt /fabos/libexec/userdb_convert $FROM_MAJOR.$FROM_MINOR $TO_MAJOR.$TO_MINOR > /dev/null 2>&1
fi

# handling the updating of /etc/hosts
#

/mnt/fabos/libexec/hosts_convert

comp_file /mnt/etc/passwd.rpmnew /mnt/etc/passwd.default
DIFF_PWD=$?
if [ $DIFF_PWD == "0" ]; then
	$ECHO "cleaning up /mnt/etc/passwd.rpmnew"
	rm /mnt/etc/passwd.rpmnew
fi


#
# After a netinstall, there are different private-public keys,
# we would copy from the active partition to secondary.
#
if [ -f "/etc/ssh_host_rsa_key" ]; then
	cp /etc/ssh_host_rsa_key /mnt/etc/ssh_host_rsa_key
fi

if [ -f "/etc/ssh_host_rsa_key.pub" ]; then
	cp /etc/ssh_host_rsa_key.pub /mnt/etc/ssh_host_rsa_key.pub
fi

if [ -f "/etc/ssh_host_dsa_key" ]; then
	cp /etc/ssh_host_dsa_key /mnt/etc/ssh_host_dsa_key
fi

if [ -f "/etc/ssh_host_dsa_key.pub" ];then
	cp /etc/ssh_host_dsa_key.pub /mnt/etc/ssh_host_dsa_key.pub
fi



# If we are upgrading from pre-matador, then generate protocol 2 RSA keys
if [ $FROM_MAJOR -le 6 ] && [ $TO_MAJOR -eq 7 ]; then
	if [ -f "/etc/ssh_host_rsa_key" ]; then
		/bin/rm /mnt/etc/ssh_host_rsa_key > /dev/null  2>&1
	fi

	if [ -f "/etc/ssh_host_rsa_key.pub" ]; then
		/bin/rm /mnt/etc/ssh_host_rsa_key.pub > /dev/null  2>&1
	fi

	/usr/sbin/chroot /mnt /usr/bin/ssh-keygen -b 1024 -t rsa -f \
		/etc/ssh_host_rsa_key -N "" > /dev/null  2>&1

	if [ -f "/etc/ssh_host_dsa_key" ]; then
		/bin/rm /mnt/etc/ssh_host_dsa_key > /dev/null  2>&1
	fi

	if [ -f "/etc/ssh_host_dsa_key.pub" ]; then
		/bin/rm /mnt/etc/ssh_host_dsa_key.pub > /dev/null  2>&1
	fi 

	/usr/sbin/chroot /mnt /usr/bin/ssh-keygen -d -f \
		/etc/ssh_host_dsa_key -N "" > /dev/null  2>&1

	/bin/cp /mnt/etc/ssh_host* /etc/ > /dev/null  2>&1

	/bin/chmod 600 /etc/ssh_host*

	# Remove obsolete files
	/bin/rm -f /etc/fabos/trunk_area*
	/bin/rm -f /mnt/etc/fabos/trunk_area*
fi

# Need to set auth policy to off on AG mode
set_auth_policy() {
	ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`
	if [ "$ag_mode" -eq 1 ]; then
		/fabos/cliexec/config set auth.policy 2 3
		/fabos/cliexec/config set auth.policy.dev 2 3
		/fabos/cliexec/configcommit
	fi
}

# Need to set CR disable file if in AG mode
set_cr_disable() {
	/bin/rm -f /mnt/etc/fabos/ag/fos_crdisable
	/bin/rm -f /etc/fabos/ag/fos_crdisable
	ag_mode=`/fabos/link_bin/switchshow | grep -c "Access Gateway Mode"`
	if [ "$ag_mode" -eq 1 ]; then
		if [ ! -f /mnt/etc/fabos/ag/fos_crdisable ]; then
			/bin/touch /mnt/etc/fabos/ag/fos_crdisable 2> /dev/null
		fi
	fi
}

remove_snmp_config_key()
{
	/fabos/cliexec/config remove snmp.64mibCap.ISCSI-MIB
	snmpMibCap=`correcthost /fabos/cliexec/config get snmp.mibCap 2`
	mask=0x91000
        let "snmpMibCap=snmpMibCap & ~mask"
	correcthost /fabos/cliexec/config set snmp.mibCap 2 $snmpMibCap
        correcthost /fabos/cliexec/configcommit;
	
}

remove_apm_config_keys_in_all_contexts()
{
	# Check for Virtual Fabrics
	check_vf_enabled
	if [ $? -ne $STS_OK ]; then
		if [ $ACTIVECP ]; then
			CHASSISCMD="cmd_chassis" ;
			current_context=$CURRENT_VF ;
			for_all_contexts remove_apm_config_keys ;
			context_switch $current_context ;
		else
			for_all_contexts_standby remove_apm_config_keys ;
		fi
	else
		remove_apm_config_keys ;
	fi
}

remove_apm_config_keys()
{

	# For TTmon and EEmon configurations on the base switch remove the
	# keys on the active CP, so they don't get synced when
	# firmwaredownload is done on the standby
	ag_mode=`/fabos/bin/ag --modeshow | grep -c "NOT enabled"` 
	BASE=`correcthost_to_curr_vfid /fabos/bin/switchshow | grep "Base Switch:" | cut -d ' ' -f 6 | cut -d ',' -f 1`
	if [ "$BASE" == "Yes" -o $ag_mode == 0 ]; then
		for key in `/fabos/bin/configshow | grep "perf.eemonc\." | cut -f 1 -d':'`
		do
			correcthost_to_curr_vfid /fabos/cliexec/config remove $key
		done

		for key in `/fabos/bin/configshow | grep "perf.ttmonc" | cut -f 1 -d ':'`
		do
			correcthost_to_curr_vfid /fabos/cliexec/config remove $key
		done
		correcthost_to_curr_vfid /fabos/cliexec/configcommit
	fi

	for  key in `/fabos/bin/configshow | grep "perf\." | cut -f 1 -d':'`
	do		
		str=`echo $key | cut -f 1 -d '.'`
		if [ "$str" == "perf" ]; then
			/fabos/cliexec/config remove $key
		fi
	done
	/fabos/cliexec/configcommit ;
}

add_ag_fporttrunking_key()
{
	/fabos/bin/configshow | grep -q ag.fporttrunking

	if [ $? -ne $STS_OK ]; then
		/fabos/cliexec/config set ag.fporttrunking 2 0
		/fabos/cliexec/configcommit ;
	fi
}

#	Upgrading from pre-El Dorado. 
if [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -lt 1 ] || [ "$FROM_MAJOR" -lt 7 ]; then
	if [ "$TO_MAJOR" -ge 7 -a  $TO_MINOR -ge 1 ]; then
		if [ $ACTIVECP ]; then
			set_auth_policy ;
			set_cr_disable ;
		fi
	fi
fi

# Create power-on test skip indicator file on standby
#
>/mnt/etc/skip_poweron_test

#
# Store checksum of ldconfig and ld.so (shared loader)
# The output hash values will be used during power on test
#
if [ "$TO_MAJOR" -ge 6 ]; then
$ECHO "Creating loader checksum file"
>/mnt/etc/ldchksum

    #Find the loader checksum
    if [ -s /mnt/var/glibc-linuxthreads/copyback/ld-* ]
    then
	md5sum /mnt/var/glibc-linuxthreads/copyback/ld-* | \
	/bin/sed -e "s,\/mnt\/var\/glibc-linuxthreads\/copyback\/ld-,\/lib\/ld-,g" \
	>> /mnt/etc/ldchksum
    else
	md5sum /mnt/lib/ld-* | \
	/bin/sed -e "s,\/mnt\/lib\/ld-,\/lib\/ld-,g" \
	>> /mnt/etc/ldchksum
    fi

    #Now manipulate the ldconfig checksum
    if [ -s /mnt/var/glibc-linuxthreads/copyback/ldconfig ]
    then
	md5sum /mnt/var/glibc-linuxthreads/copyback/ldconfig | \
	/bin/sed -e "s,\/mnt\/var\/glibc-linuxthreads\/copyback\/ldconfig,\/sbin\/ldconfig,g" \
	>> /mnt/etc/ldchksum
    else
	md5sum /mnt/sbin/ldconfig | \
	/bin/sed -e "s,\/mnt\/sbin\/ldconfig,\/sbin\/ldconfig,g" \
	>> /mnt/etc/ldchksum
    fi

else
    if [ -f /mnt/etc/ldchksum ]; then
	$ECHO "Removing loader checksum file"
	rm /mnt/etc/ldchksum
    fi
fi


remove_ipfilter_rule()
{
	total_policy=8
	ipfilter1="897"
	ipfilter2="898"
	ipfilter3="111"
	for (( i = 0; i <= total_policy ; i++ ))
	do	
		rule_name=`/fabos/cliexec/config get ipfilter.$i.name 5`
		if [[ -n $rule_name && "$rule_name" = "default_ipv4" || 
			"$rule_name" = "default_ipv6" ]]; then
			new_rule=`/fabos/cliexec/config get ipfilter.$i.numofrules 2`
			old_rule=$new_rule
			for (( j = 1; j <= $new_rule && $new_rule != 0; j++ ))
			do
				d_port=`/fabos/cliexec/config get ipfilter.$i.rule.$j 5 |
						cut -f 6- -d ','`
				if [[ "$d_port" = "$ipfilter1" ||  "$d_port" = "$ipfilter2" ||
						"$d_port" = "$ipfilter3" ]]; then
					for (( k = $j; k <= $new_rule; k++ ))
					do
						if [ $k != $new_rule ]; then
							next_rule=`/fabos/cliexec/config get ipfilter.$i.rule.$((k+1)) 5`
							/fabos/cliexec/config set ipfilter.$i.rule.$k 5 "$next_rule"
						else
							/fabos/cliexec/config remove ipfilter.$i.rule.$k
						fi
					done
					(( new_rule-- ))
					(( j-- ))
				fi
			done
			if [ $old_rule != $new_rule ];then
				/fabos/cliexec/config set ipfilter.$i.numofrules 2 $new_rule
				/fabos/cliexec/config update
			fi
		else
			return
		fi			
	done
}

add_default_audit_key()
{
	default_key=10,
	clicommand="/fabos/cliexec/config get audit.cfg.class 5"
	ret=`correcthost $clicommand`;
	last=`echo $ret | awk '{print $NF}'` 
	key_found=`echo $ret | grep $default_key |wc -l`
	if [ $key_found == "0" ]; then 
		clicommand="/fabos/cliexec/config set audit.cfg.class 5 $last$default_key"
		ret=`correcthost $clicommand`;
		clicommand="/fabos/cliexec/config commit"
		ret=`correcthost $clicommand`;
	fi
}
# defect- 410381 Removing RPC and portmapper entries
# defect- 516611 Adding Default RAS class here will be updated in Configuration
# file in case of warm recovery.
if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -ge 1 ]; then
	check_vf_enabled
	if [ $? -eq $STS_ERR ]; then
		current_context=$CURRENT_VF
		if [ ! -e /fabos/link_sbin/lscfg_test ]; then
			/bin/ln -s /fabos/cliexec/lscfg_util /fabos/link_sbin/lscfg_test 2> /dev/null
		fi 
		vf_id=`lscfg_test --fid 0`
		if [ $current_context != $vf_id ]; then
			context_switch $vf_id
			remove_ipfilter_rule
			add_default_audit_key
			context_switch $current_context
		else
			remove_ipfilter_rule
			add_default_audit_key
		fi
		if [ -e /fabos/link_sbin/lscfg_test ]; then
			/bin/rm /fabos/link_sbin/lscfg_test
		fi
	else
		remove_ipfilter_rule
		add_default_audit_key
	fi
fi
# create the trace dump file on the /mnt dir
trace_create_dump_file


# Touch the /etc/fabos/ag/fos_config.backup file
# if it doesn't exist
#
if [ ! -f /etc/fabos/ag/fos_config.backup ]; then
	/bin/touch /etc/fabos/ag/fos_config.backup 2> /dev/null
fi

#

#
# for pizzabox with firmware v5.0 or older, make sure the
# upgrade to new firmware (v5.1) to boot in a quiet mode.
# for chassis-based platforms, we honor this mode as well.
#
OPT="$(echo $(cat /boot/extra_boot_opts 2>/dev/null) quiet)"
bootenv OSLoadOptions "$OPT"
	
if [ ${SWBD##SWBD} == 63 ]; then
	bootenv AutoLoad no
fi

if [ ${SWBD##SWBD} == 156 ]; then
	# extract out slub_debug if there is any
	newarg=`bootenv bootargs | sed 's/slub_debug //'`
	bootenv bootargs "$newarg"
fi


# temporary for dcf branch: disable POST and set LABMode to 8
#/fabos/link_sbin/diagpost 0
#/fabos/link_sbin/setlabmode 8

disable_peer_beacon() {
	MAX_PARTITION=8
	partition=0
	counter=0
	fids=""
	current_context=-1
	while [ $partition -le $MAX_PARTITION ]; do
		if [ -f /proc/fabos/switch/$partition/route_info ]; then
			lcb=`grep "Peer beacon port =" /proc/fabos/switch/$partition/route_info | cut -d "=" -f2 | awk '{print $1}'`
			if [ "$lcb" != "" ] && [ $lcb -ne "-1" ]; then
				fids="$fids `cat /proc/fabos/switch/$partition/info | grep "LS Attributes:" | cut -d "," -f1 | awk '{print $4}'`"
				counter=`expr $counter + 1`
				break
			fi
		fi
		partition=`expr $partition + 1 `
	done
	if [ $ACTIVECP ] && [ $lcb -ne "-1" ]; then
		cliCommand="/fabos/link_sbin/portpeerbeacon --disable "$lcb"";
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
			current_context=$CURRENT_VF;
			context_switch $fids
		fi
		ret=`ROLE_ID=root $cliCommand `
		if [ $current_context -ne "-1" ]; then
			context_switch $current_context
		fi
		if [ -n "$ret" ]; then
			echo "post install script failed to run peer beacon command, cmd : cliCommand error: $ret";
		fi
	fi
}
disable_maps()
{
#    echo " in disable_maps function"
    if [ -f /tmp/maps.err.log ]; then
	if [ $ACTIVECP ]; then
		# echo "Disabling MAPS trynig again."
	    cliCommand="/fabos/link_sbin/mapsconfig --disablemaps";
#	    echo "active cp disable_maps $cliCommand\n"
		ret=`ROLE_ID=root $cliCommand`
	    if [ -n "$ret" ]; then
		echo "port install script failed to run MAPS command, cmd : $cliCommand error: $ret";
	    fi
	else
	    cliCommand="CHASSIS_ROLEID=0 CURRENT_VF=255 /fabos/link_sbin/mapsconfig --disablemaps"
	    ret=`correcthost $cliCommand`
	    # echo "standby cp disable_maps $cliCommand $ret $(otherhost)\n"
	    # this is error case when script failed to run command
	    # this could only happen in following condition
	    # 1. maps dead, 2. err.log exist but maps is disabled 
	    if [ -n "$ret" ]; then
		echo "port install script failed to run MAPS command, cmd : $cliCommand error: $ret";
	    fi
	fi
	rm /tmp/maps.err.log
   #else
	#echo "MAPS is not active"
    fi
}
#
# maps_enabled_check
#
# Check whether the switch is currently in auto csctl mode
#
maps_enabled_check() {
    # on active cp we need diffrent command than on the stand by.
    # dont know the reason why?
    if [ $ACTIVECP ]; then
	ret=`/fabos/cliexec/configshow -chassis | grep maps.migrated:1 |wc -l`
	# need to truncate all the leading blank characters 
	last=`echo $ret | tr -d ' '`
    else
	cmd="CHASSIS_ROLEID=0 CURRENT_VF=255 /fabos/cliexec/configshow -chassis | grep maps.migrated:1 | wc -l"
	ret=`correcthost  $cmd`;
	# ret will have command plus the result so we just need result. result will be 
	# either zero or 1 so we just need last character from the end
	last=${ret: -1:1}
    fi
    # this is check for debugging 
    #if  [ $last -eq 1 ]; then
	#echo "1: maps is enabled $last"
    #else
	#echo "1: maps is disabled $last"
    #fi
    # return the status
    return $last
}

#check for maps enabled
check_for_maps_enabled()
{
    maps_enabled_check
    if [ $? -gt 0 ]; then
	echo $MAPS_ACTIVE > /tmp/maps.err.log
    fi
    return $STS_OK
}

# checks for downgrade
if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -le 1 ]; then
    # echo "$TO_MAJOR $TO_MINOR, now MAPS has to be disabled"  
    check_for_maps_enabled
    disable_maps
fi

# MAPS rule cleanup
psRulesList=("defCHASSISBAD_PWR_MARG:defCHASSISBAD_PWR_CRIT")
#psFanRulesList is applicable only for tomtoo
psFanRulesList=("defCHASSISBAD_PWR_MARG:1" "defCHASSISBAD_PWR_CRIT:1" "defCHASSISBAD_FAN_MARG:1" "defCHASSISBAD_FAN_CRIT:1")
swbd=`sin | awk ' {print $2} ' | grep SWBD | sed 's@,@@'`


##### strstr function to search a sub string in the string 
function strstr ( ) {
    #echo "eheh",$1 $2,"done"
    # if s2 points to a string of zero length, strstr echoes s1
    [ ${#2} -eq 0 ] && { echo "$1" ; return 0; }

    # strstr echoes nothing if s2 does not occur in s1
    case "$1" in
    *$2*) ;;
    *) return 1;;
    esac

    # use the pattern matching code to strip off the match and everything
    # following it
    first=${1/$2*/}

    # then strip off the first unmatched portion of the string
    xxx="${1##$first}"
    echo "$xxx" 
}

##### runs cli command on active CP
function runMapsCliCommand ( ) {
    ret=`correcthost_to_curr_vfid $1`
    echo $ret
}

# This function is the replacement of CONFIGSHOW function.
# This function  returns the configuration from local copy instead of doing RSH
# to active CP and get the configuration
# In the begining copyAllConfigurationFiles function copy all the configuration
# to /tmp directory and expect below function would use that configuration.
CONFIGSHOW_ADV() {
    if [ "$1" = "-chassis"  ]; then
        cat /tmp/config_chassis
    elif [ "$1" = "-a" ] || [ "$1" = "-all" ] ; then
        cat /tmp/config_all
    else
        if [ $VF_ENABLED -ne $STS_OK ]; then
            cat /tmp/config_$CURRENT_VF
        else
            cat /tmp/config_128
        fi
    fi
}

#### deletes rule(s) and if need be replaced with new rule
function deleteRules ( ) {
    rulesList=$1
    ret=0


    #configuration for each logical switch
    CONFIGSHOW_ADV>xxx

    configBuffer=`awk '/^maps\.policy\./' xxx`
    #echo "configBuffer",$configBuffer
    if [ -z "$configBuffer" ]
    then
        return $ret
    fi
    # getting maps.policy.list token value. this token has all the user
    # policies names
    policyList=`cat xxx | grep maps.policy.list`
    policyList=$(strstr "$policyList" ':')
    policyList=${policyList:1}
    policyList=$(echo $policyList | tr "," "\n")

    #echo "policyList",$policyList

    # iterating all the policies
    for policy in $policyList
    do
        # getting rules present in the policy
        ruleBuffer=`cat xxx | grep "maps.policy.$policy"`
        # if no rules are present the continue
        if [ -z "$ruleBuffer" ]
        then
            continue
        fi

        #iterating user rule database. user rules could come in two forms:
        # 1. simply delete the rule from user policy
        # 2. follow step #1 and then replace with new rule
        for ruleRec in $rulesList
        do
            rule=${ruleRec%%:*}
            newRule=${ruleRec#*:}
	
	    #searching old rule in user policy rules
            found=`echo "$ruleBuffer" | grep "$rule"`
	    #rule not found then continue
            if [ -z "$found" ]
	    then
	         continue
            fi
	    #echo "rule",$rule
            #deleting a rule and adding new rule back
            cmd="/fabos/sbin/mapspolicy --delrule $policy -rulename $rule"
            ret=$(runMapsCliCommand "$cmd")

            #adding a rule if ruleName is given
            if [ "$newRule" !=  1 ]
            then
                  cmd="/fabos/sbin/mapspolicy --addrule $policy -rulename $newRule"
                ret=$(runMapsCliCommand "$cmd")
            fi
        done
    done
}

#### deletes rules from all FIDs
function deleteRulesFromAllFIDs ( ) {
    # check VF enabled
    if [ $VF_ENABLED -ne $STS_OK ]; then
        if [ $ACTIVECP ]
        then
            for_all_contexts deleteRules "$1"
        else
            for_all_contexts_standby deleteRules "$1"
        fi
    else
        deleteRules "$1"
    fi
}

buf=`/fabos/cliexec/hashow`
buf=$(strstr "$buf" 'Not supported on this platform')
# if the platform is pizza box then delete critical ps rules
if [ "$buf" ]
then
    if [ $swbd != "SWBD118" ]
    then
        psRules=${psRulesList[@]}
        deleteRulesFromAllFIDs "$psRules"
	
    ##
    # dont allow user to upgrade if for any reason user has added ALL_BE_PORTS in user defined rules.
    # this is extremely unexpected but could happen
    ##
    else
	#### PS rules
	psFanRules=${psFanRulesList[@]}
	deleteRulesFromAllFIDs "$psFanRules"
    fi

fi
#
# Downgrade from v7.3.0 to v7.2.0
#
if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -le 2 ]; then
	#echo "$TO_MAJOR $TO_MINOR, now peer beacon has to be disabled"
	disable_peer_beacon
fi

cipher_config_73_upgrade() {
	cipher_default=`/fabos/abin/cipherconfig --show | /bin/grep  -c '!ECDH:!DH:HIGH:-MD5:!CAMELLIA:!SRP:!PSK:!AESGCM'`
	if [ "$cipher_default" -eq 0 ]; then
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
			echo "INFO: Ciphersuite change on FID: $CURRENT_VF"
		else
			echo "INFO: Ciphersuite change on switch"
		fi
		echo "HTTPS ciphers will be modified to be compatible with new firmware version"
		correcthost_to_curr_vfid /fabos/cliexec/config set cipher.https 5 '!ECDH:!DH:HIGH:-MD5:!CAMELLIA:!SRP:!PSK:!AESGCM';
		correcthost_to_curr_vfid /fabos/cliexec/configcommit
	fi
}

cipher_config_73_downgrade() {
	cipher_default=`/fabos/abin/cipherconfig --show | /bin/grep  -c '!ECDH:!DH:HIGH:-MD5:!CAMELLIA:!SRP:!PSK:!AESGCM'`
	if [ "$cipher_default" -eq 1 ]; then
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
			echo "INFO: Ciphersuite change on FID: $CURRENT_VF"
		else
			echo "INFO: Ciphersuite change on switch"
		fi
		echo "HTTPS ciphers will be modified to be compatible with new firmware version"
		correcthost_to_curr_vfid /fabos/cliexec/config set cipher.https 5 '!DH:HIGH:-MD5'
		correcthost_to_curr_vfid /fabos/cliexec/configcommit
	fi
}

	if [ $TO_MAJOR -eq 7 -a $TO_MINOR -eq 3 -a $TO_PATCH -ge 1 ]; then
		ret=`/bin/ps -e | /bin/grep "agd" | /bin/grep -v "diag" | wc -l`
		if [ $ret -ne 0 ]; then
			check_vf_enabled
			if [ $? -ne $STS_OK ]; then
				FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
				FABOS_SWITCHNO=0
				add_ag_fporttrunking_key
				FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
			else
				add_ag_fporttrunking_key
			fi
		fi
	fi
#
#upgrade to >= v7.3.0
#
if [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -le 2 ] && [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -ge 3 ]; then
	#if 7.3default cipher is not set, set to 7.3default ciphher
	check_vf_enabled
	if [ $? -ne $STS_OK ]; then
		if [ $ACTIVECP ]; then
			for_all_contexts cipher_config_73_upgrade
		else
			for_all_contexts_standby cipher_config_73_upgrade
		fi
	else
		cipher_config_73_upgrade
	fi
fi

add_snmp_informsTag()
{
    inform=`/fabos/cliexec/config get snmp.inform 2`;
    informTagStatus="0";
    informTagStatusDefault="0";

    if [ "$inform" = "1" ]; then
        informTagStatus=1;
    else
        informTagStatus=0;
    fi

    i=0;
    while [ $i -lt 6 ]; do
    /fabos/bin/configshow -all | grep -qi snmp.snmpv3TrapTarget.$i.informenabled
    if [ $? -ne 0 ]; then
        /fabos/cliexec/config set snmp.snmpv3TrapTarget.$i.informenabled 2 $informTagStatus;
        /fabos/cliexec/config set snmp.snmpv3TrapTarget.$i.informenabled.default 2 $informTagStatusDefault;
    fi
    i=`expr $i + 1`
    done
    /fabos/cliexec/configcommit
}

remove_snmp_informsTag()
{
    i=0
    while [ $i -lt 6 ]; do
    /fabos/bin/configshow -all | grep -qi snmp.snmpv3TrapTarget.$i.informenabled
    if [ $? -eq 0 ]; then
        /fabos/cliexec/config remove snmp.snmpv3TrapTarget.$i.informenabled
        /fabos/cliexec/config remove snmp.snmpv3TrapTarget.$i.informenabled.default
    fi
    i=`expr $i + 1`
    done
    /fabos/cliexec/configcommit
}


#
# Downgrade from v7.2.1 to v7.2.0
#
SSHD_CONFIG_FILE=/etc/sshd_config
SSH_CONFIG_FILE=/etc/ssh_config
SSHD_CONFIG_FILE_MNT=/mnt/etc/sshd_config
SSH_CONFIG_FILE_MNT=/mnt/etc/ssh_config

if [ $TO_MAJOR -eq 7 -a $TO_MINOR -eq 2 -a $TO_PATCH -eq 0 ]; then

    if [ -f $SSHD_CONFIG_FILE ]; then
       /bin/grep -v "MinPrime" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
       /bin/grep -v "ShaValue" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
       /bin/grep -v "KexAlgorithms diffie-hellman-group-exchange-sha256" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
    fi
    if [ -f $SSH_CONFIG_FILE ]; then
       /bin/grep -v "MinPrime" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
       /bin/grep -v "ServerHostKeySize" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
       /bin/grep -v "ShaValue" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
       /bin/grep -v "KexAlgorithms diffie-hellman-group-exchange-sha256" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
    fi

    if [ -f $SSHD_CONFIG_FILE_MNT ]; then
    	/bin/grep -v "MinPrime" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	/bin/grep -v "ShaValue" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	/bin/grep -v "KexAlgorithms diffie-hellman-group-exchange-sha256" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
    fi

    if [ -f $SSH_CONFIG_FILE_MNT ]; then
	/bin/grep -v "MinPrime" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
	/bin/grep -v "ServerHostKeySize" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
	/bin/grep -v "ShaValue" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
	/bin/grep -v "KexAlgorithms diffie-hellman-group-exchange-sha256" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
    fi
fi

#Add ecdsa host key to sshd_config for releases >= v7.3.0
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -ge 3 ]; then
    if [ -f $SSHD_CONFIG_FILE ]; then
	/bin/grep "HostKey /etc/ssh_host_ecdsa_key" $SSHD_CONFIG_FILE > /dev/null 2>&1
	ret=`echo $?`
	if [ $ret -ne 0 ]; then
    		echo "HostKey /etc/ssh_host_ecdsa_key" >> $SSHD_CONFIG_FILE
	fi
    fi
    if [ -f $SSHD_CONFIG_FILE_MNT ]; then
	/bin/grep "HostKey /etc/ssh_host_ecdsa_key" $SSHD_CONFIG_FILE_MNT > /dev/null 2>&1
	ret=`echo $?`
	if [ $ret -ne 0 ]; then
    		echo "HostKey /etc/ssh_host_ecdsa_key" >> $SSHD_CONFIG_FILE_MNT
	fi
    fi
fi
#Remove ecdsa host key from sshd_config for releases < v7.3.0
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -lt 3 ]; then
    if [ -f $SSHD_CONFIG_FILE ]; then
       /bin/grep -v "HostKey /etc/ssh_host_ecdsa_key" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
    fi
    if [ -f $SSHD_CONFIG_FILE_MNT ]; then
       /bin/grep -v "HostKey /etc/ssh_host_ecdsa_key" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
    fi
fi


if [ -f $SSHD_CONFIG_FILE ]; then
	cat $SSHD_CONFIG_FILE | grep -v "#" | grep Banner | grep "/etc/fabos/motd" > /dev/null 2>&1
	retVal=$?
	if [ $retVal -ne 0 ]; then
	  	/bin/grep -v "Banner" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
	        echo "Banner /etc/fabos/motd" >> $SSHD_CONFIG_FILE
		/fabos/cliexec/config save $SSHD_CONFIG_FILE 
	fi
	cat $SSHD_CONFIG_FILE | grep -v "#" | grep "Port " | grep -v "22" > /dev/null 2>&1
	retVal=$?
	if [ $retVal -eq 0 ]; then
	  	/bin/grep -v "Port " $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
	        echo "Port 22" >> $SSHD_CONFIG_FILE
		/fabos/cliexec/config save $SSHD_CONFIG_FILE 
	fi
fi
if [ -f $SSHD_CONFIG_FILE_MNT ]; then
	cat $SSHD_CONFIG_FILE_MNT | grep -v "#" | grep Banner | grep "/etc/fabos/motd" > /dev/null 2>&1
	retVal=$?
	if [ $retVal -ne 0 ]; then
	  	/bin/grep -v "Banner" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	        echo "Banner /etc/fabos/motd" >> $SSHD_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSHD_CONFIG_FILE_MNT 
	fi
	cat $SSHD_CONFIG_FILE_MNT | grep -v "#" | grep "Port " | grep -v "22" > /dev/null 2>&1
	retVal=$?
	if [ $retVal -eq 0 ]; then
	  	/bin/grep -v "Port " $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	        echo "Port 22" >> $SSHD_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSHD_CONFIG_FILE_MNT 
	fi
fi

# Syncing known_hosts file from primary to secondary partition so as to avoid host key mismatch error after fwdl
/fabos/cliexec/config save /root/.ssh/known_hosts > /dev/null 2>&1

#Add extra macs from sshd_config for releases >= v7.3.0
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -ge 3 ]; then
    # "config get" for fips mode returns value only if called from
    # default VF. So, temporarily set FABOS_SWITCHNO to 0 and then
    # revert it back to its original value after "config get"
    FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
    FABOS_SWITCHNO=0
    retVal=`/fabos/cliexec/config get fips.mode 2`
    FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
    if [ $retVal -eq 1 ]; then
	if [ -f $SSHD_CONFIG_FILE ]; then
	  	/bin/grep -v "Macs" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
	        echo "Macs hmac-sha1,hmac-sha2-256,hmac-sha2-512" >> $SSHD_CONFIG_FILE

 		/bin/grep -v "Macs" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
		echo "Macs hmac-sha1,hmac-sha2-256,hmac-sha2-512" >> $SSH_CONFIG_FILE

	       /bin/grep -v "KexAlgorithms" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
	        echo "KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256" >> $SSHD_CONFIG_FILE

	       /bin/grep -v "KexAlgorithms" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
	        echo "KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256" >> $SSH_CONFIG_FILE
		/fabos/cliexec/config save $SSHD_CONFIG_FILE 
		/fabos/cliexec/config save $SSH_CONFIG_FILE
    		if [ $STANDBYCP -eq 1 ] ; then
			/bin/rcp $SSHD_CONFIG_FILE root@$(otherhost):$SSHD_CONFIG_FILE
			/bin/rcp $SSH_CONFIG_FILE root@$(otherhost):$SSH_CONFIG_FILE
		fi
	fi
	if [ -f $SSHD_CONFIG_FILE_MNT ]; then
	       /bin/grep -v "Macs" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	        echo "Macs hmac-sha1,hmac-sha2-256,hmac-sha2-512" >> $SSHD_CONFIG_FILE_MNT

	       /bin/grep -v "Macs" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
        	echo "Macs hmac-sha1,hmac-sha2-256,hmac-sha2-512" >> $SSH_CONFIG_FILE_MNT

	       /bin/grep -v "KexAlgorithms" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	        echo "KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256" >> $SSHD_CONFIG_FILE_MNT

	       /bin/grep -v "KexAlgorithms" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
	        echo "KexAlgorithms ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256" >> $SSH_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSHD_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSH_CONFIG_FILE_MNT
    		if [ $STANDBYCP -eq 1 ] ; then
			/bin/rcp $SSHD_CONFIG_FILE_MNT root@$(otherhost):$SSHD_CONFIG_FILE_MNT
			/bin/rcp $SSH_CONFIG_FILE_MNT root@$(otherhost):$SSH_CONFIG_FILE_MNT
		fi
	fi
    fi
fi
#Remove extra macs from sshd_config for releases < v7.3.0
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -lt 3 ]; then
    # "config get" for fips mode returns value only if called from
    # default VF. So, temporarily set FABOS_SWITCHNO to 0 and then
    # revert it back to its original value after "config get"
    FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
    FABOS_SWITCHNO=0
    retVal=`/fabos/cliexec/config get fips.mode 2`
    FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
    if [ $retVal -eq 1 ]; then
	if [ -f $SSHD_CONFIG_FILE ]; then
       		/bin/grep -v "Macs" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
		echo "Macs hmac-sha1" >> $SSHD_CONFIG_FILE

	        /bin/grep -v "Macs" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
        	echo "Macs hmac-sha1" >> $SSH_CONFIG_FILE

	        /bin/grep -v "KexAlgorithms" $SSHD_CONFIG_FILE > temp && mv temp $SSHD_CONFIG_FILE
        	echo "KexAlgorithms diffie-hellman-group-exchange-sha256" >> $SSHD_CONFIG_FILE

	        /bin/grep -v "KexAlgorithms" $SSH_CONFIG_FILE > temp && mv temp $SSH_CONFIG_FILE
        	echo "KexAlgorithms diffie-hellman-group-exchange-sha256" >> $SSH_CONFIG_FILE
		/fabos/cliexec/config save $SSHD_CONFIG_FILE
		/fabos/cliexec/config save $SSH_CONFIG_FILE
    		if [ $STANDBYCP -eq 1 ] ; then
			/bin/rcp $SSHD_CONFIG_FILE root@$(otherhost):$SSHD_CONFIG_FILE
			/bin/rcp $SSH_CONFIG_FILE root@$(otherhost):$SSH_CONFIG_FILE
		fi
	fi
	if [ -f $SSHD_CONFIG_FILE_MNT ]; then
	        /bin/grep -v "Macs" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
		echo "Macs hmac-sha1" >> $SSHD_CONFIG_FILE_MNT

  	        /bin/grep -v "Macs" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
         	echo "Macs hmac-sha1" >> $SSH_CONFIG_FILE_MNT

	        /bin/grep -v "KexAlgorithms" $SSHD_CONFIG_FILE_MNT > temp && mv temp $SSHD_CONFIG_FILE_MNT
	        echo "KexAlgorithms diffie-hellman-group-exchange-sha256" >> $SSHD_CONFIG_FILE_MNT

	        /bin/grep -v "KexAlgorithms" $SSH_CONFIG_FILE_MNT > temp && mv temp $SSH_CONFIG_FILE_MNT
 	        echo "KexAlgorithms diffie-hellman-group-exchange-sha256" >> $SSH_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSHD_CONFIG_FILE_MNT
		/fabos/cliexec/config save $SSH_CONFIG_FILE_MNT
    		if [ $STANDBYCP -eq 1 ] ; then
			/bin/rcp $SSHD_CONFIG_FILE_MNT root@$(otherhost):$SSHD_CONFIG_FILE_MNT
			/bin/rcp $SSH_CONFIG_FILE_MNT root@$(otherhost):$SSH_CONFIG_FILE_MNT
		fi
	fi
    fi
fi

#Remove ssh ecdsa keypair for releases < v7.3.0 in fips mode
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -lt 3 ]; then
    # "config get" for fips mode returns value only if called from
    # default VF. So, temporarily set FABOS_SWITCHNO to 0 and then
    # revert it back to its original value after "config get"
    FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
    FABOS_SWITCHNO=0
    retVal=`/fabos/cliexec/config get fips.mode 2`
    FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
    PRIVKEY_DIR=/root/.ssh
    PRIVKEY_FILE_ECDSA=id_ecdsa
    EXPORT_PUBKEY_FILE_ECDSA=id_ecdsa.pub
    if [ $retVal -eq 1 ]; then
    	if [ -e $PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA ]; then
		size=`/bin/ls -la $PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA | /bin/awk ' { print $5 } '`
		correcthost_to_curr_vfid /bin/dd if=/dev/zero of=$PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA bs=1 count=$size 2> /dev/null
		correcthost_to_curr_vfid /fabos/cliexec/config save $PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA

		correcthost_to_curr_vfid /bin/rm $PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA
		correcthost_to_curr_vfid /bin/rm $PRIVKEY_DIR/$EXPORT_PUBKEY_FILE_ECDSA
		correcthost_to_curr_vfid /fabos/cliexec/config save $PRIVKEY_DIR/$PRIVKEY_FILE_ECDSA
		correcthost_to_curr_vfid /fabos/cliexec/config save $PRIVKEY_DIR/$EXPORT_PUBKEY_FILE_ECDSA
	fi
    fi
fi

#Remove ecdsa host key for releases < v7.3.0 in fips mode
if [ $TO_MAJOR -eq 7 -a $TO_MINOR -lt 3 ]; then
    # "config get" for fips mode returns value only if called from
    # default VF. So, temporarily set FABOS_SWITCHNO to 0 and then
    # revert it back to its original value after "config get"
    FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
    FABOS_SWITCHNO=0
    retVal=`/fabos/cliexec/config get fips.mode 2`
    FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
    if [ $retVal -eq 1 ]; then
	HOST_KEY_DIR=/etc
	HOST_ECDSA_KEY=ssh_host_ecdsa_key
	PUB_SUFFIX=.pub
	if [ -e $HOST_KEY_DIR/$HOST_ECDSA_KEY ]; then
			size=`/bin/ls -la $HOST_KEY_DIR/$HOST_ECDSA_KEY | /bin/awk ' { print $5 } '`
			correcthost_to_curr_vfid /bin/dd if=/dev/zero of=$HOST_KEY_DIR/$HOST_ECDSA_KEY bs=1 count=$size 2> /dev/null
			correcthost_to_curr_vfid /fabos/cliexec/config save $HOST_KEY_DIR/$HOST_ECDSA_KEY

			correcthost_to_curr_vfid /bin/rm $HOST_KEY_DIR/$HOST_ECDSA_KEY
			correcthost_to_curr_vfid /bin/rm $HOST_KEY_DIR/$HOST_ECDSA_KEY$PUB_SUFFIX
			correcthost_to_curr_vfid /fabos/cliexec/config save $HOST_KEY_DIR/$HOST_ECDSA_KEY
			correcthost_to_curr_vfid /fabos/cliexec/config save $HOST_KEY_DIR/$HOST_ECDSA_KEY$PUB_SUFFIX

	fi
    fi
fi

#swTrackChangesTrap is deprecated in v7.2.0
if [ "$TO_MAJOR" -ge 7 -a "$TO_MINOR" -ge 3 ]; then
	# Check for Virtual Fabrics
	check_vf_enabled
	if [ $? -ne $STS_OK ]; then
		CHASSISCMD="cmd_chassis" ;
		current_context=$CURRENT_VF ;
		for_all_contexts  remove_snmp_trap_change;
		context_switch $current_context ;
	else
		remove_snmp_trap_change ;
	fi

	FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
	FABOS_SWITCHNO=0
	correcthost /fabos/cliexec/config remove system.intPortCrdRecov
	correcthost /fabos/cliexec/config remove system.fePortStuck
	correcthost /fabos/cliexec/config remove system.bePortToStuck
	correcthost /fabos/cliexec/config remove bottleneck.BECreditLossLRThresh
	correcthost /fabos/cliexec/config remove bottleneck.BECreditLossFaultingBlade
	correcthost /fabos/cliexec/configcommit
	FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
fi

if [ "$TO_MAJOR" -ge 7 -a "$TO_MINOR" -ge 3 ]; then
	FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
	FABOS_SWITCHNO=0
	correcthost /fabos/cliexec/config remove system.perrThreshold
	correcthost /fabos/cliexec/config remove system.perrFaultFlg
	correcthost /fabos/cliexec/configcommit
	FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
fi

#case 1:
#Removing Home VF and chassis role field in ldap.map file if it exists when downgrade from v7.4.2  to v7.4.1x or v7.3.x
	if [ "$TO_MAJOR" -lt 7 ] || [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -lt 4 ] || [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -eq 4 -a "$TO_PATCH" -lt 2 ]; then
		if [ -f /etc/fabos/ldap.map ]; then
		pizza=`/fabos/bin/slotshow | grep -c "Not supported on this platform"`
			if [ "$pizza" -ne 0 -o "$ACTIVECP" -eq 1 ]; then
				correcthost /bin/sed -r 's/:/*/2' /etc/fabos/ldap.map | sed 's|*\(.*\)*.$|\1|' > /tmp/ldap
				correcthost /bin/sed -r 's/=/*/1' /tmp/ldap | sed 's|*\(.*\)*.$|\1|' > /tmp/ldap1
				correcthost /bin/sed '/^$/d' /tmp/ldap1 > /tmp/ldap2
				correcthost /bin/cat /tmp/ldap2 > /etc/fabos/ldap.map
				correcthost /bin/cat /tmp/ldap2 > /mnt/etc/fabos/ldap.map
				correcthost /bin/rm /tmp/ldap
				correcthost /bin/rm /tmp/ldap1
				correcthost /bin/rm /tmp/ldap2
			else
				correcthost "/bin/sed -r 's/:/*/2' /etc/fabos/ldap.map | sed 's|*\(.*\)*.$|\1|' > /tmp/ldap"
				correcthost "/bin/sed -r 's/=/*/1' /tmp/ldap | sed 's|*\(.*\)*.$|\1|' > /tmp/ldap1"
				correcthost "/bin/sed '/^$/d' /tmp/ldap1 > /tmp/ldap2"
				correcthost "/bin/cat /tmp/ldap2 > /etc/fabos/ldap.map"
				correcthost "/bin/cat /tmp/ldap2 > /mnt/etc/fabos/ldap.map"
				correcthost "/bin/rm /tmp/ldap"
				correcthost "/bin/rm /tmp/ldap1"
				correcthost "/bin/rm /tmp/ldap2"
			fi
		correcthost /fabos/cliexec/config save /etc/fabos/ldap.map
		correcthost /fabos/cliexec/config save /mnt/etc/fabos/ldap.map
		fi
	fi


#case 2:
#Adding vf and chassis role field in ldap.map file if it exists when upgrade from v7.3.x or v7.4.1x to v7.4.2
if [ "$FROM_MAJOR" -lt 7 ] || [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -lt 4 ] || [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -eq 4 -a $FROM_PATCH -lt 2 ] ; then
	if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -eq 4 -a "$TO_PATCH" -ge 2 ]; then
		if [ -f /etc/fabos/ldap.map ]; then
        pizza=`/fabos/bin/slotshow | grep -c "Not supported on this platform"`
			if [ "$pizza" -ne 0 -o "$ACTIVECP" -eq 1 ]; then
				correcthost /bin/sed '/^$/d' /etc/fabos/ldap.map > /tmp/ldaps
				correcthost /bin/sed -r 's/$/:(null):(null)/' /tmp/ldaps > /tmp/ldap
				correcthost /bin/sed '/^$/d' /tmp/ldap > /tmp/ldap1
				correcthost /bin/cat /tmp/ldap1 > /etc/fabos/ldap.map
				correcthost /bin/cat /tmp/ldap1 > /mnt/etc/fabos/ldap.map
				correcthost /bin/rm /tmp/ldap
				correcthost /bin/rm /tmp/ldap1
				correcthost /bin/rm /tmp/ldaps
			else
				correcthost "/bin/sed '/^$/d' /etc/fabos/ldap.map > /tmp/ldaps"
				correcthost "/bin/sed -r 's/$/:(null):(null)/' /tmp/ldaps > /tmp/ldap"
				correcthost "/bin/sed '/^$/d' /tmp/ldap > /tmp/ldap1"
				correcthost "/bin/cat /tmp/ldap1 > /etc/fabos/ldap.map"
				correcthost "/bin/cat /tmp/ldap1 > /mnt/etc/fabos/ldap.map"
				correcthost "/bin/rm /tmp/ldap"
				correcthost "/bin/rm /tmp/ldap1"
				correcthost "/bin/rm /tmp/ldaps"
			fi
		correcthost /fabos/cliexec/config save /etc/fabos/ldap.map
		correcthost /fabos/cliexec/config save /mnt/etc/fabos/ldap.map
		fi
	fi
fi
#Remove the "auditlogall.config" for releases < 7.4.0
if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -lt 4 ]; then
	if [ -f /mnt/etc/config/auditlogall.config ]; then
		/bin/rm /mnt/etc/config/auditlogall.config
	fi
	# create a "/mnt/etc/audit.storage" file in 7.3 and copy last 256 messages
	echo "creating the downgrade audit storage"
	/fabos/sbin/auditdump -d
fi

#Create a copy of audit.storage when upgrading from 7.3 to 7.4 or higher
if [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -lt 4 ]; then
	if [ -f /etc/audit.storage ]; then
		echo "creating the old storage file"
		/bin/cp /etc/audit.storage /mnt/etc/audit.storage.old
	fi	
fi
#ISCSI-MIB is deprecated in v7.4.0
if [ "$TO_MAJOR" -ge 7 -a "$TO_MINOR" -ge 4 ]; then

	remove_snmp_config_key ;	
	if [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -lt 4 ]; then
		remove_apm_config_keys_in_all_contexts;
	fi

	ret=`/bin/ps -e | /bin/grep "agd" | /bin/grep -v "diag" | wc -l`
	if [ $ret -ne 0 ]; then
		check_vf_enabled
		if [ $? -ne $STS_OK ]; then
			FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
			FABOS_SWITCHNO=0
			add_ag_fporttrunking_key
			FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
		else
			add_ag_fporttrunking_key
		fi
	fi

fi

#add snmp informs tags if it is upgrade from pre7.4 to >=7.4
if [ "$FROM_MAJOR" -lt 7 ] || [ "$FROM_MAJOR" -eq 7 -a "$FROM_MINOR" -lt 4 ]; then
    add_snmp_informsTag
fi

#remove snmp informs tags if it is downgrade from >=7.4 to pre7.4
if [ "$TO_MAJOR" -lt 7 ] || [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -lt 4 ]; then
    remove_snmp_informsTag
fi

if [ "$SWBD" == "SWBD148" ]; then
	if [ "$TO_MAJOR" -eq 7 -a "$TO_MINOR" -eq 3 ]; then
		# disable SYSRM features by default in 7.3
		/bin/touch /mnt/etc/fabos/cpu_disable 2> /dev/null
		/bin/touch /mnt/etc/fabos/mem_disable 2> /dev/null
	fi
fi

#
# Upgrade from v7.2.0 to v7.2.1/v7.3
#
if [ $FROM_MAJOR -eq 7 -a $FROM_MINOR -eq 2 -a $FROM_PATCH -eq 0 ]; then
	# "config get" for fips mode returns value only if called from
	# default VF. So, temporarily set FABOS_SWITCHNO to 0 and then
	# revert it back to its original value after "config get"
	FABOS_SWITCHNO_TEMP=$FABOS_SWITCHNO
	FABOS_SWITCHNO=0
	retVal=`/fabos/cliexec/config get fips.mode 2`
	FABOS_SWITCHNO=$FABOS_SWITCHNO_TEMP
	if [ $retVal -eq 1 ]; then
        	/mnt/fabos/sbin/sshdconfigupdate ServerHostKeySize 1
	        /mnt/fabos/sbin/sshdconfigupdate MinPrime 1
        	/mnt/fabos/sbin/sshdconfigupdate ShaValue 1 postinst

		#Explicitly adding to secondary partition since config save does not happen on STANDBY CP
    		if [ $STANDBYCP -eq 1 ] ; then
        		/usr/sbin/chroot /mnt /fabos/sbin/sshdconfigupdate ServerHostKeySize 1
		        /usr/sbin/chroot /mnt /fabos/sbin/sshdconfigupdate MinPrime 1
        		/usr/sbin/chroot /mnt /fabos/sbin/sshdconfigupdate ShaValue 1 postinst
		fi

		if [ -f "/mnt/etc/ssh_host_rsa_key" ]; then
		        /bin/rm /mnt/etc/ssh_host_rsa_key > /dev/null  2>&1
		fi

		if [ -f "/mnt/etc/ssh_host_rsa_key.pub" ]; then
		        /bin/rm /mnt/etc/ssh_host_rsa_key.pub > /dev/null  2>&1
		fi
		/usr/bin/ssh-keygen -b 2048 -t rsa -f /mnt/etc/ssh_host_rsa_key -N "" > /dev/null  2>&1
		/bin/chmod 600 /mnt/etc/ssh_host_rsa_key
		/bin/chmod 600 /mnt/etc/ssh_host_rsa_key.pub
   	fi
fi

#
# When upgrading from v7.4.1c or below to v7.4.1d or above
# perform UID0 conversions and File Permission updates.
# The downgrade conversions are done in check_post_features.
# this is because of lack of granularity to differentiate 
# the releases at the patch level in postinst
#

#UID_CONVERSION="Version Supports UID Conversion"
uid_conversion()
{
	new_fw_is_SV=$(grep -c "UID_CONVERSION" $MNT_POSTINST) # grep in postinst of New fw
	cur_fw_is_SV=$( grep -c "UID_CONVERSION" $POSTINST)    # grep in postinst of Current fw

	# 8.0.1 supports above feature but not feature tag
	# Look for special string in postinstall 
	# to detect feature support
	if [ $new_fw_is_SV -eq 0 ]; then
		new_fw_is_SV=$(grep -c $TATOOINE_UID_SUPPORT $MNT_POSTINST) # grep in postinst of New fw
	fi	
	if [ $cur_fw_is_SV -eq 0 ]; then
		cur_fw_is_SV=$(grep -c $TATOOINE_UID_SUPPORT $POSTINST) # grep in postinst of Current fw
	fi	

	#
	# If Current fw does not support feature
	# And the new fw supports the feature
	# then perform forward conversion.
	#
	if [ $cur_fw_is_SV -eq 0 ]; then
		#Forward Conversion
		if [ $new_fw_is_SV -gt 0 ]; then
			#Password Conversion - UID Forward Conversion
			/mnt/fabos/libexec/passwd_convert /mnt/etc/passwd FWDL_CONVERSION UID_FORWARD_CONVERSION > /dev/null 2>&1
			/bin/cp /mnt/etc/passwd /etc/passwd > /dev/null 2>&1
			#Group Conversion - UID Forward Conversion
			/mnt/fabos/libexec/group_convert /mnt/etc/group FWDL_CONVERSION UID_FORWARD_CONVERSION > /dev/null 2>&1
			/bin/cp /mnt/etc/group /etc/group > /dev/null 2>&1
			#Permission correction for home directories
			pkgname=`/bin/rpm -qf /fabos/users/user --root /mnt`
			/usr/sbin/chroot /mnt /bin/rpm --setugids $pkgname > /dev/null 2>&1
			if [ $? -ne 0 -a "$SWBD" == "SWBD148" ]; then
				#RPM version of 2634_dev platforms doesn't support rpm --setguids, use chown instead
				#Udrole update
				/usr/sbin/chroot /mnt /bin/chown -R udrole:udrole /fabos/users/udrole
				#Permission correction for home directories
				/usr/sbin/chroot /mnt /bin/chown -R basicswitchadmin:basicswitchadmin /fabos/users/basicswitchadmin
				/usr/sbin/chroot /mnt /bin/chown -R fabricadmin:fabricadmin /fabos/users/fabricadmin
				/usr/sbin/chroot /mnt /bin/chown -R operator:operator /fabos/users/operator
				/usr/sbin/chroot /mnt /bin/chown -R securityadmin:securityadmin /fabos/users/securityadmin
				/usr/sbin/chroot /mnt /bin/chown -R switchadmin:switchadmin /fabos/users/switchadmin
				/usr/sbin/chroot /mnt /bin/chown -R user:user /fabos/users/user
				/usr/sbin/chroot /mnt /bin/chown -R zoneadmin:zoneadmin /fabos/users/zoneadmin
				/bin/grep "^factory" /mnt/etc/passwd > /dev/null 2>&1
				if [ $? -eq 0 ]; then
					if [ -d /mnt/fabos/users/diag ]; then
						/usr/sbin/chroot /mnt /bin/chown -R factory:factory /fabos/users/diag
					fi
				fi
			fi
		fi
	fi
}

# Sync passwd file if / and /mnt have different files
# To resolve /passwd file overwrite during RPM installation
passwd_file_sync()
{
    #Passwd file sync
    primsum=`/usr/bin/md5sum /etc/passwd | /usr/bin/cut -d ' ' -f 1`
    secsum=`/usr/bin/md5sum /mnt/etc/passwd | /usr/bin/cut -d ' ' -f 1`
    if [ "$primsum" != "$secsum" ]; then
        /bin/cp /etc/passwd /mnt/etc/passwd
    fi
}

#Modify File Permissions as required
file_permission_modifications()
{
	#File Permission Updates
	#Change file ppermisson to security config files after upgrade to 8.0.1 or above.
	
	filelist_perm_400="/etc/fabos/certs/sw0/pvt_key"

	filelist_perm_600="/etc/shadow \
	/etc/passwd \
	/etc/fabos/opasswd \
	/etc/ssh_config \
	/etc/sshd_config \
	/etc/fabos/user.db \
	/etc/fabos/pwron_fips_status \
	/etc/fabos/pki/switch.0.csr \
	/etc/fabos/pki/switch.0.key \
	/etc/fabos/pki/switch.0.crt \
	/etc/fabos/pki/switch.0.pp \
	/etc/fabos/pki/switch.0.rootcrt \
	/etc/fabos/pki/tp/switch.0.csr \
	/etc/fabos/pki/tp/switch.0.key \
	/etc/fabos/pki/tp/switch.0.crt \
	/etc/fabos/pki/tp/switch.0.pp \
	/etc/fabos/pki/tp/switch.0.rootcrt \
	/root/.ssh/known_hosts \
	/etc/ssh_host_dsa_key /etc/ssh_host_dsa_key.pub /etc/ssh_host_rsa_key /etc/ssh_host_rsa_key.pub /etc/ssh_host_ecdsa_key /etc/ssh_host_ecdsa_key.pub \
	/root/.ssh/id_dsa /root/.ssh/id_dsa.pub /root/.ssh/id_rsa /root/.ssh/id_rsa.pub /root/.ssh/id_ecdsa /root/.ssh/id_ecdsa.pub \
	/etc/fabos/fips_selftests.conf \
	/etc/fabos/tacplus.conf \
	/etc/fabos/ldap.conf \
	/etc/raddb/server \
	/etc/fabos/auth.0.conf /etc/fabos/auth.1.conf /etc/fabos/auth.2.conf /etc/fabos/auth.3.conf \
	/etc/fabos/auth.4.conf /etc/fabos/auth.5.conf /etc/fabos/auth.6.conf /etc/fabos/auth.7.conf \
	/etc/fabos/authutil.0.conf /etc/fabos/authutil.1.conf /etc/fabos/authutil.2.conf /etc/fabos/authutil.3.conf \
	/etc/fabos/authutil.4.conf /etc/fabos/authutil.5.conf /etc/fabos/authutil.6.conf /etc/fabos/authutil.7.conf"

	for i in $filelist_perm_400; do
		if [ -f $i ]; then
			/bin/chmod 400 $i
			/bin/chmod 400 /mnt/$i
			correcthost_to_curr_vfid /bin/chmod 400 $i
			correcthost_to_curr_vfid /bin/chmod 400 /mnt/$i
		fi
	done
	
	for i in $filelist_perm_600; do
		if [ -f $i ]; then
			/bin/chmod 600 $i
			/bin/chmod 600 /mnt/$i
			correcthost_to_curr_vfid /bin/chmod 600 $i
			correcthost_to_curr_vfid /bin/chmod 600 /mnt/$i
		fi
	done

}

# Taggged features Post install updates begin here:

#*****Passwd File Sync
passwd_file_sync

#******UID Conversion
uid_conversion

#******File Permission Updates  
file_permission_modifications

#Upgrade from <7.4.2 to 7.4.2
# If user defined role is exists in the switch then those roles
# will be mapped to the udrole in the group file
	pizza=`/fabos/bin/slotshow | grep -c "Not supported on this platform"`
		if [ $ACTIVECP ]; then
			isActiveCp=1
		else
			isActiveCp=0
		fi
		udrolecount=`cat /etc/group | grep ::7 | /bin/sed 's/::7.*//' | /bin/sed -r '$!s/$/\,/' | tr -d '\n' | wc -c`
		if [ $udrolecount -gt 0 ]; then
			if [ "$pizza" -ne 0 -o "$isActiveCp" -eq 1 ]; then
				udr=`cat /etc/group | grep ::7 | /bin/sed 's/::7.*//' | /bin/sed -r '$!s/$/\,/' | tr -d '\n'`
				udrole=`echo $udr,`
				udrolemem=`cat /etc/group | grep udrole | /bin/sed 's/udrole::550://'`
				echo $udrole >> /tmp/udgroup
				echo $udrolemem >> /tmp/udgroup
				udlist=`cat /tmp/udgroup | tr , '\n' | sort | /bin/sed '$!N; /^\(.*\)\n\1$/!P; D' | /bin/sed -r '$!s/$/\,/' | tr -d '\n' | /bin/sed 's/^/udrole::550:/'`
				/bin/sed 's/.*udrole::550:.*/'"$udlist"'/' /etc/group > /tmp/group
				/bin/cp /tmp/group /etc/group
				/bin/rm /tmp/udgroup
				/bin/rm /tmp/group
			else
				/bin/rcp root@$(otherhost):/mnt/etc/group /tmp/groupAcp
				udr=`cat /tmp/groupAcp | grep ::7 | /bin/sed 's/::7.*//' | /bin/sed -r '$!s/$/\,/' | tr -d '\n'`
				udrole=`echo $udr,`
				udrolemem=`cat /tmp/groupAcp | grep udrole | /bin/sed 's/udrole::550://'`
				echo $udrole >> /tmp/udgroup
				echo $udrolemem >> /tmp/udgroup
				udlist=`cat /tmp/udgroup | tr , '\n' | sort | /bin/sed '$!N; /^\(.*\)\n\1$/!P; D' | /bin/sed -r '$!s/$/\,/' | tr -d '\n' | /bin/sed 's/^/udrole::550:/'`
				/bin/sed 's/.*udrole::550:.*/'"$udlist"'/' /etc/group > /tmp/group
				/bin/rcp /tmp/group root@$(otherhost):/etc/group
				/bin/rcp /tmp/group root@$(otherhost):/mnt/etc/group
				/bin/rm /tmp/groupAcp
				/bin/rm /tmp/udgroup
				/bin/rm /tmp/group
			fi
			correcthost /fabos/cliexec/config save /etc/group
			correcthost /fabos/cliexec/config save /mnt/etc/group
		fi

set_secure_mode() {
	/fabos/cliexec/config set cfgload.secure 2 1
	/fabos/cliexec/configcommit
}

# Set the cfgload.secure to 1 for these platforms.
if [ "$SWBD" == "SWBD124" -o "$SWBD" == "SWBD125" -o "$SWBD" == "SWBD134" -o "$SWBD" == "SWBD156" ]; then
	check_vf_enabled
	if [ $? -eq $STS_ERR ]; then
		current_context=$CURRENT_VF
		if [ $current_context != $def_vf_id ]; then
			context_switch $def_vf_id
			set_secure_mode
		else
			set_secure_mode
		fi
	else
		set_secure_mode
	fi
fi

# Only perform this operation for the platforms that truly need it. 
# This list needs to be updated appropriately; Currently includes:
#   Dazzler(26), DazzlerJr(27),
#   Icecube(29), Pulsar(32), Stealth(34) 
case ${SWBD##SWBD} in

    '26' )
	PORTCNT=8
    ;;

    '27' )
	PORTCNT=16
    ;;

    '29' )
	PORTCNT=12
    ;;

    '32' )
	PORTCNT=32
    ;;

    '34' )
	PORTCNT=16
    ;;

    *)
	exit 0
    ;;
esac



# Check the port list for interfaces that are in the FULL state
# Build up a list of such ports

n=0

while [ $n -lt "$PORTCNT" ]
do

 # This command is interactive, so the extra linefeeds are used
 # to get the multiple pages

 i=$(/fabos/bin/interfaceShow $n <<!



!
)
 # Isolate the ports that are in the FULL state
 # Look at the output of InterfaceShow for the very distinct state
 # string that indicates a neighbor is expecting Hello messages.
 # Delete the line if it doesn't have this string. If the line survives,
 # then find the digit string following "Interface" and extract just
 # those digits to use as the port number in the Hello sender below.

 # Note that we are making use of the shell converting newlines to spaces
 # as we are not quoting i.
 k=$(echo $i|/bin/sed -ne "/NB_ST_FULL/s/.*Interface \([0-9]\+\).*/\1/p" )

 # Build up a list of ports that should get the Hello msgs
 iface_list="$iface_list $k"

 n=$(($n+1))

done


# Elminate all the white space to see if any ports are in the
# list.
t=$(echo $iface_list | sed -e "s/ //g" )


# If there are no non-white space characters left, then the
# port list is empty, so there is no need to send Hello messages
if [ -z $t ]
then
 exit 0
fi


# Create the link to the internal command
# First make sure we are in our home directory
cd /root
if [ ! -x fspfHATest ]
then
 /bin/ln -s /mnt/fabos/bin/fspf_tst fspfHATest
fi


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

send_hellos 1>postinst.out 2>&1 &

disown %1

exit 0
