#!/bin/bash
#
#This script cleans up the xscore and xsvhba module in the local installed
#initrd file. This issue started to occur with RHEL5U5
#

function cleanup_exit() {
	popd > /dev/null 2>&1
	rm -rf /tmp/kernel$$
	exit 0
}

#....
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi


kversion=`uname -r`

while getopts k: OPTION
do
     case $OPTION in
         k)
             kversion=$OPTARG
             ;;
         *)
             exit 0
             ;;
     esac
done

if [ ! -d /boot ] || [ ! -e /boot/initrd-$kversion.img ]; then
	exit 0
fi

files=(ib_cm ib_core ib_mad ib_sa mlx4_ib mlx4_core xscore xsvhba xsvnic)

mkdir /tmp/kernel$$ || exit $?
cp /boot/initrd-$kversion.img /tmp/kernel$$ || exit $?
pushd /tmp/kernel$$ > /dev/null 2>&1
zcat initrd-$kversion.img | cpio -uid > /dev/null 2>&1 || exit $?
#
# If all 3 modules exist, then it is OK
#
if [ -e lib/xsvnic.ko ] && [ -e lib/xsvhba.ko ] && [ -e lib/xscore.ko ]
then
	cleanup_exit
fi
#
# If all 3 modules do not exist, then it is OK too
#
if [ ! -e lib/xsvnic.ko ] && [ ! -e lib/xsvhba.ko ] && [ ! -e lib/xscore.ko ]
then
	cleanup_exit
fi
#
# Get rid of the modules now
#
for item in ${files[*]}
do
	rm -f lib/$item.ko
	grep -v $item  init > /tmp/init.$$	
	cp -f /tmp/init.$$ init
done
chmod 700 init
rm -f initrd-$kversion
#
# Repack initrd now
#
find . | cpio --quiet -c -o | gzip -9 > /tmp/initrd-$kversion.img || exit $?
mv -f /tmp/initrd-$kversion.img /boot
cleanup_exit
