#!/bin/sh
#
#    Copyright (c) 2006-2012 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#   The prefwdl script will be run before the actual downloading of
#	RPM packages starts. In a chassis, if dual-MM firmwaredownload
#	command	is entered (without -s) on the active MM, the script will
#	only be invoked on the standby MM because that is where the RPM
#	downloading takes place. If single firmwaredownload command
#	(with -s) is entered on both MMs, the script will be run on both
#	of them.
#
#	Firmwaredownload will choose the newer prefwdl script to run
#	by comparing the major, minor, and patch numbers of the existing
#	and to-be-downloaded firmware. If the existing firmware is newer,
#	it will run the prefwdl script that is already on the switch.
#	If the existing firmware is older or the same as the firmware
#	being downloaded, it will run the prefwdl script that is part of
#	the to-be-downloaded firmware.
#
#	Any pre-processing that will get the system ready for
#	firmwaredownload can be done here.
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
# Passed in version numbers
#
TO_MAJOR=$1
TO_MINOR=$2
TO_PATCH=$3
TO_RID=$4
OUTPUT=$5 		# output tty

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

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

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

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
}

# Determine whether this is a M4
is_m4_chassis()
{
	case ${SWBD##SWBD} in
		'1000')
			return 1;;
        *)
			return 0;;
	esac
}

#
# Check whether this is a MM
#
is_mm_system()
{
	case ${SWBD##SWBD} in
		'1000' | '1001')
			return 1;;
        *)
			return 0;;
	esac
}

ha_role()
{
    /fabos/cliexec/hashow | while read ha; do
		case $ha in
        	*Local*Active*)
				return 1;
        	;;
        	*Local*Standby*)
				return 0;
        	;;
        	*"Not supported"*)
        		return 1; 
        	;;
        	*)
        		return 0;
        	;;
    	esac
	done
	return $?
}

sync_lc_dir() {
	rm /var/log/lc_cmd.sav 2> /dev/null
	cp /var/log/lc_cmd /var/log/lc_cmd.sav 2> /dev/null
	>/var/log/lc_cmd
	is_m4_chassis
	if [ $? == 1 ]; then
		slots="7 8 9 10" 
	else
		slots="1 2 3 4 11 12 13 14" 
	fi
	for slot in $slots; do
		/fabos/link_sbin/rcli -as $slot "/bin/mkdir /mnt/var/images/base 2>/dev/null" >> /var/log/lc_cmd
		/fabos/link_sbin/rcli -as $slot "/bin/cp /mnt/var/images/SWBD*/* /mnt/var/images/base/" >> /var/log/lc_cmd
	done
}

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

#
# Synchronize these directories in LCs for now to workaround
# a compatbility issue between dcmd_int and pit branches
#
is_mm_system
if [ $? == 1 ];then
	ha_role
	if [ $? == 1 ]; then
		sync_lc_dir
	fi
fi

#
# Sanity check and make sure the plist file in these two directory
# the same. They should be synchronized in the preinstall script
# already. They may not be synchronized on standby CP because preinstall
# script is not run on standby CP in dual-mm firmwaredownload.
#
if [ -f /mnt/var/images/base/plist -a -f /mnt/var/images/${SWBD}/plist ]; then
	comp_file /mnt/var/images/${SWBD}/plist /mnt/var/images/base/plist
	if [ $? != "0" ]; then
		echo "You have encountered a bug that only exists in this" \
			"run and it has been correctly automatically. Please try the " \
			"firmwaredownload command again."
		mkdir /var/images/base 2>/dev/null
		cp /var/images/${SWBD}/* /var/images/base/
		cp /mnt/var/images/${SWBD}/* /mnt/var/images/base/
		exit 1
	fi
fi

#
# clean up to free up disk space on /mnt before firmwaredownload
#
/bin/rm -f /mnt/var/tracedump.dmp
/bin/rm -rf /lost+found
/bin/rm -rf /mnt/lost+found
/bin/rm -f /mnt/var/pkg_bitmap
(cd /mnt/core_files && /sbin/core_tidy.sh 2> /dev/null; cd /core_files && /sbin/core_tidy.sh 2>/dev/null)
exit 0
