#!/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/SuSE/.script,v 1.12.4.3 2005/06/21 10:38:31 gswamy Exp $
#

INSTALL_PATH=$RPM_INSTALL_PREFIX/etc/init.d
INSTALL_PATH_2=$RPM_INSTALL_PREFIX/etc

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/init.d/boot.localfs.rej
}

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

    # remove the old PowerPath scripts if we are upgrading from pre 4.X release or
    # remove the symbolic link to /etc/init.d/PowerPath if we are upgrading from
    # a 4.x release.
    rm -f $INSTALL_PATH_2/PowerPath 

    cp -f $pp_dir/.options/distribution/SuSE/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

    # Provide backward compatibility by linking /etc/init.d/PowerPath to /etc/PowerPath 
    ln -s $INSTALL_PATH/PowerPath $INSTALL_PATH_2/PowerPath 
    if [ $? -ne 0 ]; then
        elog "Failed to create symbolic link $INSTALL_PATH_2/PowerPath -> $INSTALL_PATH/PowerPath.  Errors in $PATCH_LOG"
        reset
    else
        log "Successfully created symbolic link $INSTALL_PATH_2/PowerPath -> $INSTALL_PATH/PowerPath."
    fi

    # Fix for the OPT 193269
    # copies the the boot.powerpath to the boot directory.
    # This is to mount or fsck powerpath pseudo devices during the boot time.
    # The boot script is to be executed before boot.localfs. That is why
    # in boot.localfs, a dependency is created through insserv.

    cp -f $pp_dir/.options/distribution/SuSE/boot.powerpath $INSTALL_PATH 2>> $PATCH_LOG

        if [ $? -ne 0 ]; then
        elog "$FAILURE $INSTALL_PATH/boot.powerpath.  Errors in $PATCH_LOG"
        reset
        else
        log "$SUCCESS $INSTALL_PATH/boot.powerpath."
        fi

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

     string=`grep "# X-UnitedLinux-Should-Start:" /etc/init.d/boot.localfs`
        if [ $? -ne 0 ]; then
        elog "$FAILURE (grep) failed.  Errors in $PATCH_LOG"
        reset
        else
        log "$SUCCESS grep succeeded."
        fi

    cat /etc/init.d/boot.localfs|sed -e "s/$string/$string boot.powerpath/" >/tmp/boot.localfs.$$
        if [ $? -ne 0 ]; then
        elog "$FAILURE (sed) failed.  Errors in $PATCH_LOG"
        reset
        else
        log "$SUCCESS grep succeeded."
        fi

    cp /tmp/boot.localfs.$$ /etc/init.d/boot.localfs
        if [ $? -ne 0 ]; then
        elog "$FAILURE (cp) failed.  Errors in $PATCH_LOG"
        rm -rf /tmp/boot.localfs.$$
        reset
        else
        log "$SUCCESS grep succeeded."
        rm -rf /tmp/boot.localfs.$$
        fi


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

    grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
    bootlocalfs_flag=$?

    grep "/etc/PowerPath start" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
    #
    ## Change "/etc/PowerPath" to "/etc/init.d/PowerPath" if entry left resident from 3.0.x
    #
    if [ $? -eq 0 ]; then
       	cp $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs $POWER_TMP_DIR/boot.localfs
	cat $POWER_TMP_DIR/boot.localfs |sed 's/\/etc\/PowerPath/\/etc\/init.d\/PowerPath/' > $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs
	grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
       	if [ $? -ne 0 ]; then
        	elog "Failure to modify $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs.  Please modify manually"
	else
       		log "$SUCCESS $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs."
       	fi
    #
    ## Otherwise use sed(1) to insert entry instead of using patch(1)
    ## Insert before entry that mounts local filesystems
    #
    #elif [ "$patch" == TRUE -a $bootlocalfs_flag -ne 0 -o "$patch" != TRUE ]; then
    elif [ $bootlocalfs_flag -ne 0 ]; then
       	cp $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs $POWER_TMP_DIR/boot.localfs

	# patch -p0 < $pp_dir/.options/distribution/SuSE/boot.localfs.diff >> $PATCH_LOG 2>&1
	grep "Mount local filesystems in" $POWER_TMP_DIR/boot.localfs > /dev/null 2>&1
	if [ $? = 0 ]; then
       		ins_loc=`grep -n "Mount local filesystems in" $POWER_TMP_DIR/boot.localfs |sed 's/:.*//'`
       		grep -B 1 "Mount local filesystems in" $POWER_TMP_DIR/boot.localfs |grep "#$" > /dev/null 2>&1
		if [ $? = 0 ]; then
        		ins_loc=`expr $ins_loc - 1`
		fi
        	cat $POWER_TMP_DIR/boot.localfs | sed ''$ins_loc'i \
        # Configure and initialize PowerPath \
        if [ -f /etc/init.d/PowerPath ]; then \
        	/etc/init.d/PowerPath start \
        fi # end PowerPath entry. \
' > $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs 
		grep "/etc/init.d/PowerPath start" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
       		if [ $? -ne 0 ]; then
           		elog "Failure to modify $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs.  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/init.d/boot.localfs."
       		fi
	else
		elog "Failure to modify $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs.  Please modify manually"
	fi
    fi
    #
    # install $INSTALL_PATH/ppconfig script that configures iSCSI LUNs during boot.
    #
        cp -f $pp_dir/.options/distribution/SuSE/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

}

reset_install_patches()
{
    rm -f $INSTALL_PATH/PowerPath
    if [ -f $POWER_TMP_DIR/boot.localfs ]; then
        mv $POWER_TMP_DIR/boot.localfs $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs
    fi

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

}

finish_install_patches()
{
    rm -f $POWER_TMP_DIR/boot.localfs
}

#
# uninstall interfaces
#

init_uninstall_patches()
{
    if [ -e $INSTALL_PATH/PowerPath ]; then
        mv $INSTALL_PATH/PowerPath $POWER_TMP_DIR/.PowerPath
    fi
    #
    # Remove symbolic link to /etc/PowerPath
    #
    rm -f $INSTALL_PATH_2/PowerPath
    cp $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs $POWER_TMP_DIR/boot.localfs
    #
    # 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/init.d/boot.localfs.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()
{
    #echo -e "dist:: reset_UNINSTALL_patches:\n"
    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/boot.localfs ]; then
            mv $POWER_TMP_DIR/boot.localfs $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs
        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()
{
    #echo -e "dist:: finish_UNINSTALL_patches:\n"
     
    cp $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs $POWER_TMP_DIR/boot.localfs

    #
    ## Remove all lines that "PowerPath"
    #
    #patch -p0 -R < $pp_dir/.options/distribution/SuSE/boot.localfs.diff >> $PATCH_LOG 2>&1
    grep "Mount local filesystems in" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
    cond1=$?
    grep "Configure and initialize PowerPath" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
    cond2=$?
    if [ $cond1 -eq 0 -a $cond2 -eq 0 ]; then
   	#
	## Insert "PowerPath" string to closing 'fi' statement so it gets removed as well
	## Insert "PowerPath" string to blank lines between PowerPath entry and Mount entry so they get removed as well
	#
	start_loc=`grep -n "Configure and initialize PowerPath" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs |sed 's/:.*//'`
	end_loc=`grep -n "Mount local filesystems in" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs |sed 's/:.*//'`
	for i in $start_loc
	do
		cat $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs | sed ''$i','$end_loc' s/^$/\t# PowerPath/; '`expr $i + 3`' s/fi/\tfi # end PowerPath entry./' > $POWER_TMP_DIR/boot.localfs
	done
    fi
    cat $POWER_TMP_DIR/boot.localfs | sed '/PowerPath/ d' > $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs 
    grep "/etc/init.d/PowerPath" $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs > /dev/null 2>&1
    if [ $? -ne 1 ]; then
        elog "Failed to modify $RPM_INSTALL_PREFIX/etc/init.d/boot.localfs.  Please remove PowerPath entry manually"
    fi

    rm -f $POWER_TMP_DIR/.PowerPath
    rm -f $POWER_TMP_DIR/boot.localfs 

    # Fix for the OPT 193269
    # removes the the boot.powerpath from  the boot directory.
    insserv -r $INSTALL_PATH/boot.powerpath 2>/dev/null
    rm -rf $INSTALL_PATH/boot.powerpath

    cat /etc/init.d/boot.localfs|sed "s/boot.powerpath//g" >/tmp/boot.localfs.$$
    cp /tmp/boot.localfs.$$ /etc/init.d/boot.localfs
    rm -rf /tmp/boot.localfs.$$
    rm -f $POWER_TMP_DIR/ppconfig

}
