#!/bin/bash

# SLES 9 only
# This script is written in bash (instead of ksh) because it invokes SLES scripts

# Check for pre-requisites (files)
prereq() {
    for file in $*; do
        if [ ! -f $file ]; then
            echo "Cannot find $file. See /usr/lpp/mmfs/samples/nfscluster/README for uninstall instructions."
            exit 0
        fi
    done
}

unconfig() {
    . /etc/rc.status

    rc_reset

    # Check for SUSE distribution
    dist=$( cat /etc/issue | grep "SUSE LINUX Enterprise Server 9" )
    if [ "$dist" == "" ]; then
	die "$0 will work only for SUSE LINUX Enterprise Server 9"
    fi

    # Check the GPFS is not running
    gpfsIp=$(myGPFSIP)
    isRunning=$(mmfsadm dump version 2> /dev/null | grep "Current daemon version")
    if [ -n "$isRunning" ]; then
	echo "Error: GPFS has to be shutdown on $gpfsIp before uninstall can continue."
        return 1
    fi

    echo -n "Removing GPFS system init script "
    if [ -f /etc/init.d/gpfs ]; then
        log insserv -d gpfs
        _unlink /etc/init.d/gpfs
    fi
    # Redo GPFS autoload in inittab
    sed -i 's/^# gpfs:\(.*\)# Disabled by HA-NFS/gpfs:\1/g' /etc/inittab
    rc_status -v

    # Reset default start mode of interfaces used for HA-NFS
    nfsIPs=$(getNfsIPs $gpfsIp)
    for ip in $nfsIPs; do
        # Find its ifcfg file
        ifcfg=$(getifcfg $ip)
	[ -z "$ifcfg" ] && continue
	iface=$(getifname $ifcfg)
        if grep -q "^STARTMODE.*='manual'" $ifcfg; then
	    . $ifcfg
	    for ipval in ${!IPADDR*}; do
		index=${ipval#IPADDR}
		ipaddr=${!ipval}
		if [ "$ip" == "$ipaddr" ]; then
			mode=STARTMODE$index
			break
		fi
	    done
            # SuSE
            sed -i "s/^$mode='manual'	# added for HA-NFS/$mode='onboot'/g" $ifcfg
            echo "Uninstallation has changed the start mode of interface $iface to onboot"
        elif grep -q "^ONBOOT=no" $ifcfg; then
            # RedHat
            sed -i "s/^ONBOOT=no	# added for HA-NFS/^ONBOOT=yes/g" $ifcfg
            echo "Uninstallation has changed the start mode of interface $iface to onboot"
        fi
    done

    # Restore backup 
    [ -d /var/mmfs/etc.bak ] && log rm -rf /var/mmfs/etc && log mv /var/mmfs/etc.bak /var/mmfs/etc

    # Reset files in /var/lib/nfs
    [ -L /var/lib/nfs/rmtab ] && _unlink /var/lib/nfs/rmtab && touch /var/lib/nfs/rmtab
    [ -L /var/lib/nfs/sm ] && _unlink /var/lib/nfs/sm && _mkdir /var/lib/nfs/sm
    [ -L /var/lib/nfs/sm.bak ] && _unlink /var/lib/nfs/sm.bak && _mkdir /var/lib/nfs/sm.bak
    rc_reset
}

defs=/var/mmfs/etc/nfsdefs
funcs=/var/mmfs/etc/nfsfuncs
uninstall_script=/var/mmfs/etc/uninstall_ha-nfs.sh

prereq $defs $funcs $uninstall_script

. $defs
. $funcs
. /etc/rc.status

if [ -n "$GPFS_RSH" ]; then
    GPFS_rshPath=$(which $GPFS_RSH)
else
    GPFS_rshPath=$(which rsh)
fi
if [ -n "$GPFS_RCP" ]; then
    GPFS_rcpPath=$(which $GPFS_RCP)
else
    GPFS_rcpPath=$(which rcp)
fi

LOGFILE=/tmp/uninstall-ha-nfs.log
> $LOGFILE

allflag=""
case $1 in
    -a)
        allflag=yes
	;;
    -u)
        unconfig
        exit $?
	;;
    *)
	;;
esac
	
if [ -z "$allflag" ]; then
    unconfig
else
    # Run configuration on all GPFS nodes
    gpfsIPs=$(getAllGPFSIPs)

    echo -n "Uninstalling HA-NFS on all nodes in the cluster "
    gpfsIPs=$(echo $gpfsIPs | sed 's/ /,/g')
    log mmdshcmdRC $gpfsIPs $uninstall_script -u
    if [ $? -ne 0 ]; then
        rc_failed 1
    else
        rc_reset
    fi
    rc_status -v
fi

RC=$?
grep "Error:" $LOGFILE
[ -s $LOGFILE ] && echo -n "See $LOGFILE for details."
echo ""
exit $RC
