#!/bin/sh
######################################################################
# 
# PowerChute Network Shutdown uninstall script
# Copyright (c) 1999-2014 Schneider Electric, All Rights Reserved. 
#
# History:
#   09Aug2007 Initial revision
#   21Oct2008 Supported VIMA
#
######################################################################

######################################################################
# Grobal Constants
######################################################################
LINUX="Linux"
SOLARIS="Solaris"
HPUX="HP-UX"
AIX="AIX"
UNKNOWN="UNKNOWN"
VIMA="VIMA"
TRUE="true"
# PCNS-1512: Make sure we have /sbin on the path
PATH=/sbin:$PATH
GROUP1="group1"

######################################################################
# Grobal Variables
######################################################################
INSTALL_PATH=GSUB_INSTALL_PATH
OS=$UNKNOWN

######################################################################
#  Functions
######################################################################

CheckOS() {
    OS=`uname | grep -i Linux`
    if [ ! -z "$OS" ]; then
        if [ -f /etc/vima-release ] || [ -f /etc/vma-release ]; then
            OS=$VIMA
        else
            OS=$LINUX
        fi
    else
        OS=`uname | grep -i HP-UX`
        if [ ! -z "$OS" ] 
        then
            OS=$HPUX
        else
            OS=`uname | grep -i AIX`
            if [ ! -z "$OS" ] 
            then
                OS=$AIX
            else
                OS=`uname | grep -i SOLARIS`
                if [ ! -z "$OS" ]
                then
                    OS=$SOLARIS
                else 
                    OS=`uname | grep -i SUNOS`
                    if [ ! -z "$OS" ]
                    then
                        OS=$SOLARIS
                    else
                        echo "Unknown OS, Cancel uninstallation."
                        exit 1
                    fi
                fi
            fi
        fi
    fi
 	if [ "$OS" = "$VIMA" ]; then
		if [ -f /etc/vma-release ]; then
			echo "OS=vMA"
		fi
	else
		echo "OS=$OS"
	fi
   #echo OS=$OS
}

UninstallVCenterPluginAndAlarm() {
	echo "Uninstalling vCenter plugin and alarm"
	COMP="$INSTALL_PATH/$GROUP1/comp"
	LIB="$INSTALL_PATH/$GROUP1/lib"
	javaParams="-Xms32m -Xmx64m -cp .:$COMP/pcns.jar:$LIB/m11.jar:$LIB/jasypt-1.9.0.jar:$LIB/vijava5120121125.jar:$LIB/dom4j-1.6.1.jar:$LIB/commons-codec-1.4.jar:$LIB/commons-collections-3.2.1.jar:$LIB/commons-configuration-1.6.jar:$LIB/commons-lang-2.5.jar:$LIB/commons-logging-1.1.1.jar:$LIB/log4j-core-2.2.jar:$LIB/log4j-api-2.2.jar:$LIB/bcprov-jdk15on-151.jar:$LIB/bcpkix-jdk15on-151.jar:$LIB/json_simple-1.1.jar:$INSTALL_PATH/$GROUP1 -DapplicationDirectory=\"$INSTALL_PATH\" -Dgroup=1 com.apcc.m11.components.Shutdowner.VCenterPluginAndAlarmUninstall"
	JAVA_EXE="`awk '{ print }' $INSTALL_PATH/$GROUP1/java.cfg`"
    echo ${JAVA_EXE}/java  $javaParams 2>/dev/null > $INSTALL_PATH/$GROUP1/run.sh
	chmod +x $INSTALL_PATH/$GROUP1/run.sh
	$INSTALL_PATH/$GROUP1/run.sh
    
    code=$?
	rm -f $INSTALL_PATH/$GROUP1/run.sh
    if [ $code -ne 0 ]; then
        echo "Error uninstalling VCenter plugin and alarm  Error code is: $code"
    fi
}

ConfigureESXFirewall(){
    if [ -f /usr/sbin/esxcfg-firewall ]; then
        esxcfg-firewall -c 80,tcp,out
        esxcfg-firewall -c 3052,tcp,out
        esxcfg-firewall -c 3052,tcp,in
        esxcfg-firewall -c 3052,udp,out
        esxcfg-firewall -c 3052,udp,in
        esxcfg-firewall -c 6547,tcp,in
    else
        if [ -f /usr/sbin/iptables ] || [ -f /sbin/iptables ]; then
            iptables -D OUTPUT -p tcp --sport 80 -j ACCEPT
            iptables -D OUTPUT -p tcp --sport 3052 -j ACCEPT
            iptables -D INPUT -p tcp --dport 3052 -j ACCEPT
            iptables -D OUTPUT -p udp --sport 3052 -j ACCEPT
            iptables -D INPUT -p udp --dport 3052 -j ACCEPT
            iptables -D INPUT -p tcp --dport 6547 -j ACCEPT
            
            if [ -f /etc/SuSE-release ]; then
                	iptables-save 1> /dev/null
                	OUT=$?
                	if [ $OUT -eq 0 ]; then
                		echo "Saving firewall rules to iptables"
                	else 
                		echo "Error occurred: Cannot save firewall rules to iptables"
                	fi
            else 
            		service iptables save
            fi
        fi        
    fi
}


######################################################################
# Main routine
######################################################################

if [ -z "$1" -o "$1" != "-q" ]
then
    echo
    echo "Are you sure you want to uninstall PowerChute Network Shutdown [Yes|No]?"
    read val
    case "$val" in
    [Yy]*)
        echo "Uninstalling PowerChute Network Shutdown ..."
        ;;
    [Nn]*)
        echo "Uninstallation canceled."
        exit 1
        ;;
    *)
        echo "Invalid response. Uninstallation canceled."
        exit 1
        ;;
    esac
fi

# Check OS
CheckOS
case "$OS" in
$VIMA)
cd $INSTALL_PATH
	# Fix for PCNS-3383. Perform removal of plugin and alarm if it is enabled else not
	enable_plugin=`sed '/^\#/d' ./group1/pcnsconfig.ini | grep "enable_plugin"  | tail -n 1 | sed 's/^.*=//' | sed 's/^[ ]//'`
	if [ "$enable_plugin" = $TRUE ]; then
		UninstallVCenterPluginAndAlarm
	fi
    # Stop daemon
	if [ -f /etc/SuSE-release ]; then
        /etc/init.d/PowerChute stop
    else
        /etc/rc.d/init.d/PowerChute stop
    fi

    # Remove link
	if [ -f /etc/SuSE-release ]; then
        insserv -r PowerChute 1>/dev/null 2>/dev/null
        rm -f /etc/init.d/PowerChute
    elif [ -f /sbin/chkconfig ]; then
        chkconfig PowerChute off
        chkconfig --del PowerChute
        rm -f /etc/rc.d/init.d/PowerChute
    else
        rm -f /etc/rc.d/rc1.d/K99PowerChute
        rm -f /etc/rc.d/rc2.d/S99PowerChute
        rm -f /etc/rc.d/rc3.d/S99PowerChute
        rm -f /etc/rc.d/rc4.d/S99PowerChute
        rm -f /etc/rc.d/rc5.d/S99PowerChute
        rm -f /etc/rc.d/rc6.d/K99PowerChute
	rm -f /etc/rc.d/init.d/PowerChute
    fi
    if [ -f /etc/SuSE-release ]; then
        rm -f /etc/init.d/PowerChute
    else
        rm -f /etc/rc.d/init.d/PowerChute
    fi
    ;;
$LINUX)
cd $INSTALL_PATH
	# Fix for PCNS-3693. Perform removal of plugin and alarm if it is enabled else not
	enable_plugin=`sed '/^\#/d' ./group1/pcnsconfig.ini | grep "enable_plugin"  | tail -n 1 | sed 's/^.*=//' | sed 's/^[ ]//'`
	if [ "$enable_plugin" = $TRUE ]; then
		UninstallVCenterPluginAndAlarm
	fi
    if [ -f /etc/rc.d/init.d/PowerChute ]; then
        # Stop daemon
        /etc/rc.d/init.d/PowerChute stop
    fi
    
    if [ -f /etc/init.d/PowerChute ]; then
        # Stop daemon
        /etc/init.d/PowerChute stop
    fi

    # Remove link
    if [ -f /etc/SuSE-release ]; then
        insserv -r PowerChute 1>/dev/null 2>/dev/null
    elif [ -f /sbin/chkconfig ]; then
        chkconfig PowerChute off
        chkconfig --del PowerChute
    else
        rm -f /etc/rc0.d/K99PowerChute
		rm -f /etc/rc1.d/K99PowerChute
		rm -f /etc/rc2.d/S99PowerChute
		rm -f /etc/rc3.d/S99PowerChute
		rm -f /etc/rc4.d/S99PowerChute
		rm -f /etc/rc5.d/S99PowerChute
		rm -f /etc/rc6.d/K99PowerChute
		rm -f /etc/init.d/PowerChute
    fi

    if [ -f /etc/SuSE-release ]; then
        rm -f /etc/rc.d/init.d
        rm -f /etc/init.d/PowerChute
    else
    	rm -f /etc/init.d/PowerChute
        rm -f /etc/rc.d/init.d/PowerChute
    fi
    ;;
$SOLARIS)
    /etc/rc2.d/S99PowerChute stop
    rm -f /etc/rc2.d/S99PowerChute
    rm -f /etc/rc0.d/K99PowerChute
    ;;
$HPUX)
    /sbin/init.d/pcns stop
    rm -f /sbin/rc1.d/K990pcns
    rm -f /sbin/rc2.d/S990pcns
    rm -f /sbin/init.d/pcns
    ;;
$AIX)
    /etc/rc.APCpcns stop
    rmitab PCNS
    rm -f /etc/rc.APCpcns
    ;;
esac

cd $INSTALL_PATH

# Close firewall ports

# Fix for PCNS-2417. Firewall should not be configured in case of vMA5.0 

if [ $OS = $VIMA ] || [ $OS = $LINUX ]; then
service iptables status 1>/dev/null 2>/dev/null
OUT=$?
if [ $OUT -eq 0 ]; then
   ConfigureESXFirewall
fi
else
ConfigureESXFirewall
fi

rm -rf ./group* 1>/dev/null 2>/dev/null
rm -rf ./jre 1>/dev/null 2>/dev/null
cd ..
rm -rf $INSTALL_PATH
cd ..
buf=`ls APC 2>/dev/null`
if [ -z "$buf" ]; then
    # Remove empty APC directory
    rm -rf APC 1>/dev/null 2>/dev/null
fi

echo "Uninstallation completed."
echo
exit 0
