#!/bin/sh
#
#    Copyright (c) 2006-2011 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#
#        This pre-install script will be run before firmwarecommit.
#	 It validate the filesystem and remove some unneeded file
#	 before starting firmware commit.
#
#    precommit [restore]
#

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

STS_OK=0
SWBD=`sin | sed -n -e "s/^.\+\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp"`

#########################
# Main			#
#########################
#
# remove /lost+found files and
# call core_tidy.sh to remove core files
# to free up space if neccessary.
#
/bin/rm -rf /lost+found
/bin/rm -rf /mnt/lost+found
/bin/rm -rf /mnt/etc/fabos/Dcmd/Dcmd.Linux.powerpc.cfg
/bin/rm -rf /mnt/etc/fabos/Ccmd/Ccmd.Linux.powerpc.cfg
/bin/rm -rf /mnt/etc/fabos/Dcmd/WaveDatabase
/bin/rm -rf /mnt/etc/fabos/Ccmd/WaveDatabase
(cd /mnt/core_files && /sbin/core_tidy.sh 2> /dev/null; cd /core_files && /sbin/core_tidy.sh 2>/dev/null)

#
# this is just restore operation?
#
if [ "$1" == "restore" ]; then
	restore=1
fi

#
# truncate GLIBC copyback files
#
glibc_linuxthread_pkg=`rpm -qa | grep glibc-linuxthreads`
if [ "$glibc_linuxthread_pkg" != "" ]; then
    rpm -e "$glibc_linuxthread_pkg"
    if [ -f /var/images/$SWBD/plist ]; then
		/bin/cat /var/images/$SWBD/plist | /bin/grep -a -v "$glibc_linuxthread_pkg" > /var/images/$SWBD/.tmp_plist
		/bin/mv /var/images/$SWBD/.tmp_plist /var/images/$SWBD/plist
    fi
    for glibccopybackfile in /var/glibc-linuxthreads/copyback/*
    do
	if [ -f $glibccopybackfile ]; then
	    > $glibccopybackfile
	fi
    done
    /bin/sync
fi

#
# FPGA image commit or restore. for LC_SYS and SFM_SYS, I assume
# that fpgaupgrade utility will check and only allow the active
# LC partition or MM to commit or restore.
#
#if [ -n "$restore" ]; then
#	opt=r
#else
#	opt=c
#fi

#if [ "/sbin/pkgbm -c 0x00000008" ]; then
#	echo "commit fpga image LC_SYS"
# fpgaupgrade -t LC_SYS -$opt	# local LC slot
#fi

#if [ "/sbin/pkgbm -c 0x00000010" ]; then
#	echo "commit fpga image MM_SYS"
#	fpgaupgrade -t MM_SYS -$opt	# local MM slot
#fi

#if [ "/sbin/pkgbm -c 0x00000020" ]; then
#	echo "commit fpga image SFM_SYS"
#	fpgaupgrade -t SFM_SYS -$opt    # all SFM slots
#fi

exit $STS_OK
