# /bin/bash


# --------------------------------------------------------------
#
# Uninstalls pggibs.
#
# Author:  Zlatko Michailov
# Created: 22-Jun-2004
#
# --------------------------------------------------------------



parseOptions()
{
	for opt
	do
		if [[ "$opt" == "--help" ]]
		then
			optHelp="$opt"

		elif [[ "$opt" == "--force" ]]
		then
			optForce="$opt"
		fi
	done
}



help()
{
	echo "uninstall [--help | --force]"
	echo "Uninstalls pgjobs - job scheduling and executing mechanism for PostgreSQL."
	echo "Copyright (c) 2003-2004, Zlatko Michailov"
	ext="1"
}



confirm()
{
	cfrm=""

	if [[ "$optForce" != "" ]]
	then
		cfrm="y"
	else
		read -p "$1 (Y/N)? " -n 1 ans
		echo ""

		if [[ "$ans" == "y" || "$ans" == "Y" ]]
		then
			cfrm="y"
		fi
	fi
}



initEnvironment()
{
	etcDir="/etc/init.d"
	appDir="/usr/share/pgjobs"
	tmpDir="/tmp/pgjobs"
	logDir="/var/log/pgjobs"
}



uninstall()
{
	confirm "Do you want to uninstall pgjobs"
	if [[ "$cfrm" != "" ]]
	then
		initEnvironment

		$etcDir/pgjobs stop
		$appDir/pgjobs disableall
		/sbin/chkconfig --del pgjobs
		rm -f $etcDir/pgjobs
		rm -rf $appDir
		rm -rf $tmpDir
		rm -rf $logDir

		ext="0"
	else
		ext="3"
	fi
}



# --------------------------------------------------------------

exit="0"
echo ""

parseOptions $*
if [[ "$optHelp" != "" ]]
then
	help
else
	uninstall
fi

echo ""
exit $ext


