#!/bin/sh
#
# ###################################################################
# Copyright (c) 2000, EMC Corporation. All Rights Reserved.
#
# Most Recent Author: Raghu Adabala
# Previous Authors: Raghu Adabala and Ed Goggin
#
# Contents:
# install interfaces for EMC PowerPath ver 3.0 on Linux
# ###################################################################
#

# @(#) $Header: /cvs/source/master/pxmobility/src/pkg/i386_linux/bin_install/.options/distribution/RedHat/.script,v 1.10.4.2 2005/06/21 10:37:43 gswamy Exp $
#

INSTALL_PATH=$RPM_INSTALL_PREFIX/etc/init.d

init_install_patches()
{
    #
    # Get rid of these (we of course assume that if they exist, they
    # were created as the result of a previous PowerPath installation)
    # so that one can be assured that they are dealing with an up-to-date
    # version of a particular reject file.
    #
    rm -rf $RPM_INSTALL_PREFIX/etc/rc.modules.rej
}

process_install_patches()
{
    #
    # install $INSTALL_PATH/PowerPath.
    #

#    if [ ! -e $INSTALL_PATH/PowerPath ]; then
	cp -f $pp_dir/.options/distribution/RedHat/PowerPath $INSTALL_PATH 2>> $PATCH_LOG
        if [ $? -ne 0 ]; then
  	    elog "$FAILURE $INSTALL_PATH/PowerPath.  Errors in $PATCH_LOG"
	    reset
        else
	    log "$SUCCESS $INSTALL_PATH/PowerPath."
        fi

	chmod 755 $INSTALL_PATH/PowerPath 2>> $PATCH_LOG 
        if [ $? -ne 0 ]; then
  	    elog "$FAILURE (chmod) of $INSTALL_PATH/PowerPath.  Errors in $PATCH_LOG"
	    reset
        else
	    log "$SUCCESS (chmod) of $INSTALL_PATH/PowerPath."
        fi

        if [ -e $RPM_INSTALL_PREFIX/etc/rc.modules ]; then
            grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/rc.modules > /dev/null 2>&1
            rc_modules_flag=$?
        else
            rc_modules_flag=1;
        fi

#        if [ "$patch" == TRUE -a $rc_modules_flag -ne 0 -o "$patch" != TRUE ]; then
        if [ $rc_modules_flag -ne 0 ]; then
             if [ ! -e $RPM_INSTALL_PREFIX/etc/rc.modules ] ; then
                 echo -n "#" > $RPM_INSTALL_PREFIX/etc/rc.modules
                 echo -ne ! >> $RPM_INSTALL_PREFIX/etc/rc.modules
                 echo -e "/bin/bash\n" >> $RPM_INSTALL_PREFIX/etc/rc.modules
                 chmod 755 $RPM_INSTALL_PREFIX/etc/rc.modules 2>> $PATCH_LOG 
             else
                 chmod 755 $RPM_INSTALL_PREFIX/etc/rc.modules 2>> $PATCH_LOG 
	         cp $RPM_INSTALL_PREFIX/etc/rc.modules $POWER_TMP_DIR/rc.modules
             fi 

#	     patch -p0 < $pp_dir/.options/distribution/RedHat/rc.modules.diff >> $PATCH_LOG 2>&1
	     #
	     ## Append lines instead of patching the file
	     #
	     echo -e "# Configure and initialize PowerPath." >> $RPM_INSTALL_PREFIX/etc/rc.modules
	     echo -e "if [ -f /etc/init.d/PowerPath ]; then" >> $RPM_INSTALL_PREFIX/etc/rc.modules
	     echo -e "\t/etc/init.d/PowerPath start" >> $RPM_INSTALL_PREFIX/etc/rc.modules
	     echo -e "fi # end PowerPath entry." >> $RPM_INSTALL_PREFIX/etc/rc.modules
	     grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/rc.modules > /dev/null 2>&1
             if [ $? -ne 0 ]; then
  	         elog "Failure to modify $RPM_INSTALL_PREFIX/etc/rc.modules.  Please modify manually"
#
#	    No longer fails the install in this case in order that PowerPath
#	    may install on non-qualified distributions even if this file needs
#	    to be manually patched.
#
#	    reset
             else
	         log "$SUCCESS $RPM_INSTALL_PREFIX/etc/rc.modules."
             fi
         fi
#

    #
    # install $INSTALL_PATH/ppconfig script that configures iSCSI LUNs during boot.
    #
        cp -f $pp_dir/.options/distribution/RedHat/ppconfig $INSTALL_PATH 2>> $PATCH_LOG
        if [ $? -ne 0 ]; then
            elog "$FAILURE $INSTALL_PATH/ppconfig.  Errors in $PATCH_LOG"
            reset
        else
            log "$SUCCESS $INSTALL_PATH/ppconfig."
        fi

        chmod 755 $INSTALL_PATH/ppconfig 2>> $PATCH_LOG
        if [ $? -ne 0 ]; then
            elog "$FAILURE (chmod) of $INSTALL_PATH/ppconfig.  Errors in $PATCH_LOG"
            reset
        else
            log "$SUCCESS (chmod) of $INSTALL_PATH/ppconfig."
        fi

        ln -s $INSTALL_PATH/ppconfig /etc/rc.d/rc3.d/S99ppconfig >/dev/null 2>&1
#    fi
}

reset_install_patches()
{
    rm -f $INSTALL_PATH/PowerPath
    if [ -f $POWER_TMP_DIR/rc.modules ]; then
	 mv $POWER_TMP_DIR/rc.modules $RPM_INSTALL_PREFIX/etc/rc.modules
    else
         rm -f $RPM_INSTALL_PREFIX/etc/rc.modules
    fi

    rm -f $INSTALL_PATH/ppconfig
    rm -f /etc/rc.d/rc3.d/S99ppconfig
}

finish_install_patches()
{
    rm -f $POWER_TMP_DIR/rc.modules
}

#
# uninstall interfaces
#

init_uninstall_patches()
{
    if [ -e $INSTALL_PATH/PowerPath ]; then
	mv $INSTALL_PATH/PowerPath $POWER_TMP_DIR/PowerPath
    fi
    cp $RPM_INSTALL_PREFIX/etc/rc.modules $POWER_TMP_DIR/rc.modules
    #
    # Get rid of these (we of course assume that if they exist, they
    # were created as the result of a previous PowerPath installation)
    # so that one can be assured that they are dealing with an up-to-date
    # version of a particular reject file.
    #
    rm -rf $RPM_INSTALL_PREFIX/etc/rc.modules.rej
    if [ -e $INSTALL_PATH/ppconfig ]; then
        mv $INSTALL_PATH/ppconfig $POWER_TMP_DIR/ppconfig
        rm -f /etc/rc.d/rc3.d/S99ppconfig
    fi
}

#process_uninstall_patches()
#{
#dummy
#}

reset_uninstall_patches()
{
    if [ -e $POWER_TMP_DIR/PowerPath ]; then
    	if [ -f $POWER_TMP_DIR/PowerPath ]; then
	    mv $POWER_TMP_DIR/PowerPath $INSTALL_PATH/PowerPath
	fi
    	if [ -f $POWER_TMP_DIR/rc.modules ]; then
	    mv $POWER_TMP_DIR/rc.modules /etc/rc.modules
	fi
        if [ -f $POWER_TMP_DIR/ppconfig ]; then
            mv $POWER_TMP_DIR/ppconfig $INSTALL_PATH/ppconfig 
            ln -s $INSTALL_PATH/ppconfig /etc/rc.d/rc3.d/S99ppconfig >/dev/null 2>&1
        fi
    fi
}

finish_uninstall_patches()
{
    cp $RPM_INSTALL_PREFIX/etc/rc.modules $POWER_TMP_DIR/rc.modules

    #patch -p0 -R < $pp_dir/.options/distribution/RedHat/rc.modules.diff >> $PATCH_LOG 2>&1
    #
    ## Remove all lines that contain "PowerPath"
    #
    grep "Configure and initialize PowerPath" $RPM_INSTALL_PREFIX/etc/rc.modules > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	#
	## Insert "PowerPath" string to closing 'fi' line so it gets removed as well
	#
	start_loc=`grep -n "Configure and initialize PowerPath" $RPM_INSTALL_PREFIX/etc/rc.modules |sed 's/:.*//'`
	for i in $start_loc
	do
		cat $RPM_INSTALL_PREFIX/etc/rc.modules | sed ''`expr $i + 3`' s/fi/fi # end PowerPath entry./' > $POWER_TMP_DIR/rc.modules
	done
    fi
    cat $POWER_TMP_DIR/rc.modules | sed '/PowerPath/ d' > $RPM_INSTALL_PREFIX/etc/rc.modules
    grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/rc.modules > /dev/null 2>&1
    if [ $? -ne 1 ]; then
	elog "Failure to modify $RPM_INSTALL_PREFIX/etc/rc.modules.  Please remove PowerPath entry manually"
    fi

    rm -f $POWER_TMP_DIR/PowerPath
    rm -f $POWER_TMP_DIR/rc.modules
    rm -f $POWER_TMP_DIR/ppconfig
}
