#!/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/UnitedLinux/.script,v 1.7.2.1 2004/07/23 21:22:33 mwah 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()
{
    #echo -e "dist:: process_install_patches:\n"

    #
    # 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 

#    if [ ! -e $INSTALL_PATH/PowerPath ]; then

	#echo -e "dist:: process_install_patches: cp'ing  PowerPath ...\n"

#	cp -f $pp_dir/scripts/PowerPath $INSTALL_PATH 2>> $PATCH_LOG 
        cp -f $pp_dir/.options/distribution/UnitedLinux/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

        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
}

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

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
}

#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

    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
}
