#!/bin/sh
# Script Name: uninstall
# Platform: Linux
# Description: Uninstallation program for ShutdownAgent

service_unlink() {
	if [ -f /sbin/chkconfig ]; then
		/sbin/chkconfig shutdownagent off 2>/dev/null
		/sbin/chkconfig --del shutdownagent 2>/dev/null
	else
		if [ -f /usr/sbin/update-rc.d ]; then
			/usr/sbin/update-rc.d -f shutdownagent remove 2>/dev/null
		else
			if [ -d /etc/rc.d/rc3.d/ ]; then
				rm -f /etc/rc.d/rc3.d/S99shutdownagent
				rm -f /etc/rc.d/rc3.d/K99shutdownagent
				rm -f /etc/rc.d/rc5.d/S99shutdownagent
				rm -f /etc/rc.d/rc5.d/K99shutdownagent
			else
				if [ -d /etc/rc3.d/ ]; then
					rm -f /etc/rc3.d/S99shutdownagent
					rm -f /etc/rc3.d/K99shutdownagent
					rm -f /etc/rc5.d/S99shutdownagent
					rm -f /etc/rc5.d/K99shutdownagent
				fi
			fi
		fi
	fi
}

set `/usr/bin/whoami`
if [ $1 != "root" ]
then
	echo "You must login under root to execute this program."
	echo "Bye !"
	exit
fi
echo
echo -n "Do you really want to remove the ShutdownAgent ?[y|n] "
read answer
if [ $answer = "y" ]
then
	pid=`/bin/cat /usr/local/upsagent/agent.pid 2> /dev/null`
	if [ "${pid}" != "" ]
	then
		kill -9 $pid
	fi
	service_unlink
	rm -rf /usr/local/upsagent/command
	rm -rf /usr/local/upsagent/httpd
	rm -rf /usr/local/upsagent/language
	rm -f /usr/local/upsagent/upsagentd
	rm -f /usr/local/upsagent/agent.pid
	rm -f /usr/local/upsagent/configure
	if [ -d /etc/init.d/ ]; then
		rm -f /etc/init.d/shutdownagent
	else
		rm -f /etc/rc.d/shutdownagent
	fi
	rm -f /usr/local/upsagent/shutdownagent
	rm -f /usr/local/upsagent/uninstall
	echo
	echo "Uninstall the ShutdownAgent completely."
	echo
else
	echo "Bye !"
fi
