#!/bin/sh
#
#    Copyright (c) 2006 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.
#

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

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

validate_fs() {
COCPU=0
if test "$SWBD" = "SWBD63"; then
	if test "`bootenv bootcmd | grep dhcp`" != ""; then
	   	COCPU=1;
 	fi
fi
#
# The following lines are needed to exercise the file system structures on
# both partitions. If a file system shutdown and repair is to occur, it
# needs to happen here before both partitions are changed and potentially
# incomplete making repair impossible.
#
	/bin/sync
    /usr/bin/find / -print -mount -exec ls -lasi {} \; > /dev/null 2> /dev/null
    /usr/bin/find /mnt -print -mount -exec ls -lasi {} \; > /dev/null 2> /dev/null

#
if test $COCPU  -ne 1; then
	kernel=`uname -r`
	if [[ "$kernel" > "3.12.37rt50" ]] ; then
		/bin/rpm -Va | /bin/grep -Ev "var|tmp|mnt|proc|/etc/fabos/ag|/dev|/etc/fabos/skm/IngrianNAE.properties|/etc/fabos/mace/kvcredentials.cfg" | /bin/grep missing
	else
		/bin/rpm -Va | /bin/grep -Ev "var|tmp|mnt|proc|/etc/fabos/ag|/etc/fabos/skm/IngrianNAE.properties|/etc/fabos/mace/kvcredentials.cfg" | /bin/grep missing
	fi
else
	/bin/rpm -Va | /bin/grep -Ev "var|tmp|mnt|proc|initrd|libexec|users|tftpboot|/tftpboot/SWBD36|/tftpboot/SWBD88|/etc/fabos/supportsave.conf|/fabos/libexec/icpd|/sbin/bootcount|/etc/fabos/ag|/fabos/libexec/psd" | /bin/grep missing
fi
}

#########################
# Main			#
#########################

echo "Validating the filesystem ..."
validate_fs
if [ $? == 0 ]; then
	exit $ERR_VALIDATION
fi

#
# 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
(cd /mnt/core_files && /sbin/core_tidy.sh 2> /dev/null; cd /core_files && /sbin/core_tidy.sh 2>/dev/null)

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

exit $STS_OK
