#!/usr/bin/bash
KERNEL_VERSION=`uname -r`
INSTALL_PATH="/boot/"

if [ ! -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.qssave -a ! -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.xz.qssave -a ! -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.zst.qssave ]; then
	echo "Looks like builditf was either not executed or executed without '-fc' option"
	echo "/lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.qssave is not present"
	echo "Assuming that nothing needs to be done to uninstall the qlogic target driver"
	exit 0
fi

rm -f /lib/modules/$KERNEL_VERSION/updates/virt/qla2xxx.* 2> /dev/null
rmdir /lib/modules/$KERNEL_VERSION/updates/virt 2> /dev/null

if [ -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.qssave ]; then
	echo "Restoring original qla2xxx driver from/lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.qssave"
	mv -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.qssave /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko
elif [ -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.zst.qssave ]; then
	echo "Restoring original qla2xxx driver from/lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.zst.qssave"
	mv -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.zst.qssave /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.zst
elif [ -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.xz.qssave ]; then
	echo "Restoring original qla2xxx driver from/lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.xz.qssave"
	mv -f /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.xz.qssave /lib/modules/$KERNEL_VERSION/kernel/drivers/scsi/qla2xxx/qla2xxx.ko.xz
fi


#each system has its own way, based on the presence of a few executables, we can assume certain things 
echo "Recreating initrd image"
which dracut > /dev/null 2>&1
if [ "$?" = "0" -a -f /etc/redhat-release ]; then
	dracut -f /boot/initramfs-${KERNEL_VERSION}.img $KERNEL_VERSION
elif [ -x /sbin/installkernel -a -r /boot/vmlinuz-$KERNEL_VERSION -a -r /boot/System.map-$KERNEL_VERSION ]; then
	cp /boot/vmlinuz-$KERNEL_VERSION /boot/.vmlinuz-$KERNEL_VERSION-qstor
	cp /boot/System.map-$KERNEL_VERSION /boot/.System.map-$KERNEL_VERSION-qstor
	rm -f /boot/vmlinuz-$KERNEL_VERSION /boot/System.map-$KERNEL_VERSION
	/sbin/installkernel $KERNEL_VERSION /boot/.vmlinuz-$KERNEL_VERSION-qstor /boot/.System.map-$KERNEL_VERSION-qstor
	rm -f /boot/.vmlinuz-$KERNEL_VERSION-qstor /boot/.System.map-$KERNEL_VERSION-qstor
elif [ -x /sbin/new-kernel-pkg ]; then
	if [ -x /sbin/dracut ]; then
		/sbin/new-kernel-pkg --mkinitrd --dracut --host-only --depmod --install $KERNEL_VERSION
	else
		/sbin/new-kernel-pkg --mkinitrd --depmod --install $KERNEL_VERSION
	fi
	/sbin/new-kernel-pkg --rpmposttrans $KERNEL_VERSION
elif [ -x /usr/sbin/update-initramfs ]; then
	/usr/sbin/update-initramfs -c -t -k $KERNEL_VERSION
elif [ -x /sbin/mkinitrd -a -d /lib/modules/$KERNEL_VERSION ]; then
	#Assume SLES For now
	/sbin/mkinitrd -k vmlinuz-$KERNEL_VERSION \
		-i initrd-$KERNEL_VERSION \
		-b $INSTALL_PATH
elif [ -f /etc/SUSE-brand ]; then
	dracut -f --regenerate-all
else
	echo "Dont know how to create initrd image"
fi
