#!/usr/bin/env bash

#
# Zarafa Collaboration Platform install script for supported distributions
# (c) 2004-2014 Zarafa, The Netherlands
#

VERSION=7.1.9
RELEASE=44333
BASEDIR=`dirname $0`

if [ ! -f helpers.inc ]; then
	echo "File missing: helpers.inc"
	echo "Please download Zarafa again to get this file."
	echo
	exit 1
fi
. helpers.inc

echo 
echo "Zarafa Collaboration Platform, version $VERSION-$RELEASE"
echo "(C) Copyright 2004-2010 Zarafa, The Netherlands"
echo "Please visit http://www.zarafa.com/ for more information."
echo

if [ `whoami` != "root" ]; then
	echo "This installation must be run as user root."
	echo "Use the 'su' or 'sudo' command to become root, and run this script again."
	echo
	exit 1
fi

distro_check

echo "Are you sure you wish to remove all zarafa related software? [n]"
read answer
if [ -z "$answer" ]; then answer="n"; fi
if [ "${answer:0:1}" != "y" ]; then
	echo "Not uninstalling."
	exit 0
fi

echo
echo "Stopping services ..."
for service in spooler dagent monitor search gateway ical server licensed; do
	if [ -x /etc/init.d/zarafa-$service ]; then
		/etc/init.d/zarafa-$service stop
	else
		echo $service 'init script not found anymore!'
	fi
done

# import settings before we remove configurations
import /etc/zarafa/server.cfg mysql_host MYSQLSERVER
import /etc/zarafa/server.cfg mysql_port MYSQLPORT
import /etc/zarafa/server.cfg mysql_user MYSQLUSER
import /etc/zarafa/server.cfg mysql_pass MYSQLPASS
import /etc/zarafa/server.cfg mysql_database DATABASENAME

echo
echo "Removing packages ..."
if [ $DISTRO == "Debian" -o $DISTRO == "Ubuntu" ]; then
	apt-get remove zarafa\* p\*mapi libical0 libvmime0 libkyotocabinet16
	ret=$?
else
	packages=`rpm -qa | grep ^zarafa`
	mapi=`rpm -qa | grep mapi`
	rpm -ev $packages $mapi libical libvmime kyotocabinet-lib
	ret=$?
fi
echo
if [ $ret -ne 0 ]; then
	echo 'Some packages may not have been uninstalled correctly!'
else
	echo 'All software was removed successfully!'
fi

echo
echo "The following MySQL settings for Zarafa were found:"
echo
echo "mysql_host	= $MYSQLSERVER"
echo "mysql_port	= $MYSQLPORT"
echo "mysql_user	= $MYSQLUSER"
echo "mysql_pass	= $MYSQLPASS"
echo "mysql_database	= $DATABASENAME"
echo
echo "Do you wish to remove the database using these settings too? [y]"
read answer
if [ -z "$answer" ]; then answer="y"; fi
if [ "${answer:0:1}" == "y" ]; then
	echo "Dropping database $DATABASENAME on $MYSQLSERVER:$MYSQLPORT"
	mysql -h "$MYSQLSERVER" -P "$MYSQLPORT" -u "$MYSQLUSER" --password="$MYSQLPASS" -e "DROP DATABASE $DATABASENAME"
	if [ $? -eq 0 ]; then
		echo 'Database successfully removed'
	else
		echo 'Could not remove database!'
	fi
else
	echo "Leaving database present"
fi

echo
echo "Do you wish to remove the configuration and log files too? [y]"
read answer
if [ -z "$answer" ]; then answer="y"; fi
if [ "${answer:0:1}" == "y" ]; then
	if [ $DISTRO == "Debian" -o $DISTRO == "Ubuntu" ]; then
		dpkg -l zarafa\* | grep ^rc | awk {'print $2'} | xargs -r dpkg --purge libical0 libvmime0
	fi

	if [ -d /etc/zarafa ]; then
		rm -r /etc/zarafa
	else
		echo "Directory '/etc/zarafa' not found."
	fi
	if [ -d /var/log/zarafa ]; then
		rm -r /var/log/zarafa
	else
		echo "Directory '/var/log/zarafa' not found."
	fi
fi

echo "Uninstall finished."
