#!/bin/bash

# Ask a question that requires a yes or no answer.
# Sets REPLY to either "y" or "n" depending on the user's answer.
# Returns a value of 0 (success) if the user says yes, or a value of 1 (failure) if the user says no.
function askYesNo() {
	while [ true ] ; do
		echo -n "$1 [y/n] "
		read -n 1
		case $REPLY in
			Y|y)
			REPLY="y"
			echo
			return 0
			;;
			N|n)
			REPLY="n"
			echo
			return 1
			;;
			*)
			echo
			;;
		esac
	done
}

echo "MacOS X uberSVN Uninstall Script"
echo "Copyright WANdisco Inc 2011"
askYesNo "This will forcibly remove your uberSVN installation, continue?"
if [ $? -eq 1 ] ; then
	echo "Cancelled"
	exit 0
fi
if [ "`uname -s`" = "Darwin" ] ; then
	echo "Stopping uberSVN..."
	if test -z "`sudo launchctl stop com.wandisco.ubersvn_tomcat 2>&1`" ; then
		sleep 30
	fi
	if test -z "`sudo su - ubersvn -c 'launchctl stop com.wandisco.ubersvn_tomcat' 2>&1`" ; then
		sleep 30
	fi
	sudo /opt/ubersvn/bin/httpdserverctl stop > /dev/null 2>&1
	echo "Removing files..."
	sudo rm -rf /Library/LaunchDaemons/com.wandisco.ubersvn_*
	sudo rm -rf /opt/ubersvn
	echo "Removing user and forgetting package..."
	sudo dscl . delete /Users/ubersvn > /dev/null 2>&1
	sudo dscl . delete /Groups/ubersvn > /dev/null 2>&1
	sudo pkgutil --forget com.wandisco.ubersvn > /dev/null 2>&1
fi
echo "Done, thanks for trying uberSVN!"


