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

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

######################################################################
# Global Variables
######################################################################
INSTALL_PATH=GSUB_INSTALL_PATH
OS=$UNKNOWN
SYSTEMCTL=$(which systemctl 2>/dev/null)
CHKCONFIG=$(which chkconfig 2>/dev/null)

######################################################################
#  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 -Dfile.encoding=UTF-8 -cp .:$LIB/*:$COMP/pcns.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(){
	echo "Uninstall Firewall Rule"
    if [ -f /usr/sbin/esxcfg-firewall ]; then
        /usr/sbin/esxcfg-firewall -c 80,tcp,out
        /usr/sbin/esxcfg-firewall -c 3052,tcp,out
        /usr/sbin/esxcfg-firewall -c 3052,tcp,in
        /usr/sbin/esxcfg-firewall -c 3052,udp,out
        /usr/sbin/esxcfg-firewall -c 3052,udp,in
        /usr/sbin/esxcfg-firewall -c 6547,tcp,in
    else
		
		if [ -n "$SYSTEMCTL" ]; then
          $SYSTEMCTL is-enabled firewalld 1>/dev/null 2>/dev/null
          OUT=$?
        else
          OUT=1
        fi
		
		if [ $OUT -eq 0 ]; then
			echo "    firewalld"
			firewall-cmd --remove-service pcns 1>/dev/null 2>/dev/null
			firewall-cmd --reload 1>/dev/null 2>/dev/null
			rm -fr /usr/lib/firewalld/services/pcns.xml
			firewall-cmd --reload 1>/dev/null 2>/dev/null
		else
		
			if [ $OS = $VIMA ] || [ $OS = $LINUX ]; then
				service iptables status 1>/dev/null 2>/dev/null
				OUT=$?
			else
				OUT=0
			fi
			
			if [ $OUT -eq 0 ]; then
				echo "    iptables"
				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
		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 cancelled."
        exit 1
        ;;
    *)
        echo "Invalid response. Uninstallation cancelled."
        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 [ -n "$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
		
		if [ -n "$SYSTEMCTL" ]; then		
			$SYSTEMCTL stop PowerChute
			$SYSTEMCTL disable PowerChute
			rm -f /etc/systemd/system/PowerChute.service
			rm -f /usr/bin/PowerChute
			$SYSTEMCTL daemon-reload
		else
			if [ -n "$CHKCONFIG" ]; then
        		$CHKCONFIG PowerChute off
        		$CHKCONFIG --del PowerChute
        		rm -f /etc/init.d/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
		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
ConfigureESXFirewall

rm -rf ./group* 1>/dev/null 2>/dev/null
rm -rf ./jre 1>/dev/null 2>/dev/null
rm -rf ./user_files 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
