#!/bin/sh
# dkmspost moves binaries so that they are preserved by the rpm
#          determines the install state using /opt/mpp/installed
ker=$1
base=$2
cmd=$3
src=$4
prc=`uname -m`
OS_DIST=""

#echo "post $*" >> /tmp/dkmspost.log

cleanupModulesFiles () {

	# For SLES: Delete the modules from INITRD_MODULES list from /etc/sysconfig/kernel
	# For RHEL: Delete mppUpper and mppVhba from /sbin/mkinitrd. Also remove aliases for
	# mppUpper and mppVhba
	MODLIST="mppUpper mppVhba"
	PARAMNAME=INITRD_MODULES
	FILE_TMP=/tmp/kernel_dellmpp_new.txt
	FILE_TMP2=/tmp/kernel_dellmpp_new2.txt
	if [ -f /etc/redhat-release ]; then
		FILE=/sbin/mkinitrd
		PARAMNAME=PRESCSIMODS
	else
		FILE=/etc/sysconfig/kernel
		PARAMNAME=INITRD_MODULES
	fi

	if [ ! -f $FILE ]; then
		echo kernel config file not found !
		exit 1
	fi
	cp -f $FILE $FILE_TMP
	for mod in $MODLIST
	do
		sed '/^'$PARAMNAME'/s/'$mod'//g' $FILE_TMP > $FILE_TMP2
		cp -f $FILE_TMP2 $FILE_TMP
	done
	cp -f $FILE_TMP $FILE
	rm -f $FILE_TMP $FILE_TMP2

	# Delete the aliases for mppVhba and mppUpper from /etc/modprobe.conf
	FILE_MODPROBE=/etc/modprobe.conf
	FILE_MODPROBE_TMP=/tmp/modprobe_dellmpp_new.txt
	if [ -f /etc/redhat-release ]; then
		rm -f $FILE_MODPROBE_TMP
		while read myLine
		do
			echo "$myLine" | grep "^alias" > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				echo "$myLine" | egrep " mppUpper| mppVhba" > /dev/null 2>&1
				if [ ! $? -eq 0 ]
				then
					echo "$myLine" >> $FILE_MODPROBE_TMP
				fi
			else
				echo "$myLine" >> $FILE_MODPROBE_TMP
			fi
			
		done < $FILE_MODPROBE

		cp -f $FILE_MODPROBE_TMP $FILE_MODPROBE
		rm -f $FILE_MODPROBE_TMP
	fi
}

# module is where the binaries are stored to get moved to the rpm
module=`find $base -name module |grep $ker |grep $prc`
if [ -z $module ]; then # just a guess (i.e. i586 on i686)
    module=`find $base -name module |grep $ker | tail -1`
fi

if [ build == $cmd ]; then
	# dkms copies the source to build to make the modules
    build=$base/build
    # make and move the extra utilities to the binary location
    make -C $build genuniqueid mppUtil
    cp $build/genuniqueid $module
    cp $build/mppUtil $module
    

elif [ install == $cmd ]; then
	# pull the binaries from the dkms binary location
	# to where they can be installed
    if [ ! -d $src/$ker ]; then
		mkdir $src/$ker;
    fi
    cp -f $module/genuniqueid $src/$ker
    cp -f $module/mppUtil $src/$ker
    cp -f $module/mppUpper.ko $src/$ker;
    cp -f $module/mppVhba.ko $src/$ker;

    make -C $src install OS_VER=$ker
    dkms status -m linuxrdac -k $ker |grep installed > /opt/mpp/installed
    rm -Rf $src/$ker

elif [ remove == $cmd ]; then
    # do not uninstall if dkms is being removed before an install
    if ! dkms status -m linuxrdac | grep installed ; then
        # uninstall when the installed version is removed
        if [ -f /opt/mpp/installed ]; then
			rm -f /opt/mpp/installed
			make -C $src uninstall
			cleanupModulesFiles

			# rebuild ramdisk
			echo "rebuilding ramdisk now"
			if [ -f /etc/redhat-release ]; then
				mkinitrd -f /boot/initrd-$ker.img $ker
       		else
				mkinitrd -i /boot/initrd-$ker -k vmlinuz-$ker
			fi
		fi
	fi
fi
