#!/bin/bash
#
#    Copyright (c) 2005 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

#
# 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
}

#
# Firmwaredowngrade from 6.2+ to 5.1-6.1 has a bug where bswd.conf
# does not get properly updated when doing a firmwaredowngrade.  It
# instead puts the new bswd.conf as bswd.conf.rpmnew.  Manually 
# copy the new bswd.conf here.
#
cleanup_bswd()
{
	echo $1 $2 $3
	if [ $TO_MAJOR -eq 5 ]; then
		if [ $TO_MINOR -ge 1 ]; then
			/bin/cp /mnt/etc/fabos/bswd.conf.rpmnew /mnt/etc/fabos/bswd.conf
		fi
	elif [ $TO_MAJOR -eq 6 ]; then
		if [ $TO_MINOR -eq 1 ] || [ $TO_MINOR -eq 0 ]; then
			/bin/cp /mnt/etc/fabos/bswd.conf.rpmnew /mnt/etc/fabos/bswd.conf
		fi
	fi
}


#
# Removing obsolete files in /mnt 
#
cleanup_error_log
/sbin/fwclean -o -s

cleanup_bswd


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


# 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'
}


# 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

}

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

# create the trace dump file on the /mnt dir
if [ -f '/mnt/fabos/cliexec/traceCreateDb' ] ;then
        /mnt/fabos/cliexec/traceCreateDb ${SWBD##SWBD}
fi

# remove mbd_cfg because it causes a driver mismatch at bootup when downgrading
/bin/rm -f /mnt/dev/mbd_cfg


#
# 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;$OPT"

#
# 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
#
>/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

copy_flag=0
#
# save Hifn configuration files to be read during warm start
#  
/bin/cp /var/gwtable.txt /mnt/var/gwtable.txt 2> /dev/null
/bin/cp /var/ipif.txt /mnt/var/ipif.txt 2> /dev/null
/bin/cp /var/iproute.txt /mnt/var/iproute.txt 2> /dev/null
/bin/cp /var/iptable.txt /mnt/var/iptable.txt 2> /dev/null
/bin/cp /var/policies.txt /mnt/var/policies.txt 2> /dev/null
/bin/cp /var/tunnel.txt /mnt/var/tunnel.txt 2> /dev/null
/bin/cp /var/tunnelstruct.txt /mnt/var/tunnelstruct.txt 2> /dev/null

#
# Replicate SP configuration files
#
/bin/cp -p -f /tftpboot/sp/tftp/* /mnt/tftpboot/sp/tftp/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
	copy_flag=1
fi	
#
# Replicate all the mace protocol files to the mnt
#
/bin/cp -p -f /etc/fabos/mace/*.txt /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/cp -p -f /etc/fabos/mace/*.txt_old /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/cp -p -f /etc/fabos/mace/*.txt_old.tar /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/rm -f /mnt/etc/fabos/mace/*.db 2> /dev/null
/bin/cp -p -f /etc/fabos/mace/*.db /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/rm -f /mnt/etc/fabos/mace/rekey/* 2> /dev/null
/bin/cp -p -f /etc/fabos/mace/rekey/* /mnt/etc/fabos/mace/rekey 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/rm -f /mnt/etc/fabos/mace/decom_* 2> /dev/null
/bin/cp -p -f /etc/fabos/mace/decom_* /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/rm -f /mnt/etc/fabos/mace/decommissioned_key_ids 2> /dev/null
/bin/cp -p -f /etc/fabos/mace/decommissioned_key_ids /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	

#
# Remove all the stale rekey log files backed up in rkd_trc_sav.txt
# The backup operation is now not needed after the recent logging
# enhancements (compress and archive old logs)
#
/bin/rm -f /etc/fabos/mace/rkd_trc_sav.txt* 2> /dev/null
/bin/rm -f /mnt/etc/fabos/mace/rkd_trc_sav.txt* 2> /dev/null

#
# Replicate all the SP log  files to the mnt
#
/bin/cp -p -f /etc/fabos/mace/*.log /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	
/bin/cp -p -f /etc/fabos/mace/*.log_old /mnt/etc/fabos/mace/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	

if [ ! -d "/mnt/etc/fabos/mace/splog/" ]; then
		mkdir -p "/mnt/etc/fabos/mace/splog/"
fi
/bin/cp -p -f /etc/fabos/mace/splog/* /mnt/etc/fabos/mace/splog/ 2> /dev/null
if [ $? -ne 0 -a $copy_flag -ne 1 ]; then
    copy_flag=1
fi	

if [ $copy_flag -eq 1 ]; then
	/fabos/link_sbin/errlogtest -i LOG-5000
fi

# The following case statement 
# is an work around for legacy misconfigured system
# watchdog remained off for marathon/scimitar
# make it on for mace and lance

case ${SWBD##SWBD} in

	'36' )
	echo off > /etc/config/wdtd
	echo off > /mnt/etc/config/wdtd
	;;

	'55' )
	echo off > /etc/config/wdtd
	echo off > /mnt/etc/config/wdtd
	;;

	'68' )
	echo on > /etc/config/wdtd
	echo on > /mnt/etc/config/wdtd
	;;

    '67' )
    echo on > /etc/config/wdtd
    echo on > /mnt/etc/config/wdtd
    ;;
	
esac

/fabos/link_bin/sync

checkId="3334 312d 3032 3330"
if [ -f /mnt/fabos/modules/wd.ko ];then
    FLASH=$(cat /proc/ide/hda/model | grep -c "3625")
    if [ $FLASH -ne 0 ];then
        flash_verified=$(cat /proc/ide/hda/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
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

