#!/usr/bin/env bash

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

VERSION=7.1.9
RELEASE=44333
BASEDIR=`dirname $0`
INSTALL=1
if [ ! -z "$1" -a "$1" == "-config" ]; then
	INSTALL=0
fi

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

echo 
echo "Zarafa Collaboration Platform, version $VERSION-$RELEASE"
echo "(C) Copyright 2004-2012 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
if [ $INSTALL == 1 ]; then

#
# MySQL client version tests
#
	mysql -V >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_version
	fi

	MYSQLVER=`mysql -V | sed -e 's/.*Distrib \([0-9]\+\.[0-9]\+\.\?[0-9]*\).*/\1/'`
	MYSQLMAJOR=`echo $MYSQLVER | sed -e 's/^\([^.]\+\)\.[^.]\+.*/\1/'`
	MYSQLMINOR=`echo $MYSQLVER | sed -e 's/^[^.]\+\.\([^.]\+\).*/\1/'`
	MYSQLREVISION=`echo $MYSQLVER | sed -e 's/^[^.]\+\.[^.]\+\.\(.*\)/\1/'`

	if [ -z "$MYSQLMAJOR" -o -z "$MYSQLMINOR" ]; then
		echo "Unable to determine MySQL version. Please make sure you are running"
		echo "MySQL 4.1 or higher."
		exit 1
	fi

	if [ $MYSQLMAJOR -lt 4 ]; then
		mysql_version
	fi

	if [ $MYSQLMAJOR -eq 4 -a $MYSQLMINOR -lt 1 ]; then
		mysql_version
	fi

	if [ $MYSQLMAJOR -eq 5 -a $MYSQLMINOR -eq 0 -a $MYSQLREVISION -eq 18 ]; then
		mysql_reconnect_bug
	fi

	echo "About to install for $DISTRO $DVERSION $ARCH"
	echo
	echo "Found MySQL version $MYSQLMAJOR.$MYSQLMINOR"
	echo 
	echo "We will now install Zarafa onto your system. You may be"
	echo "required to install dependencies if the installation fails."
	echo
	echo "Press <enter> to continue"
	read dummy

	echo "Installing packages ..."

	if [ $DISTRO == "Debian" -o $DISTRO == "Ubuntu" ]; then
#
# DEB packages
#
		# block upgrade of libical and libvmime by normal distribution packages, which are newer but incompatible
		dpkg --set-selections <<EOF
libical0 hold
libvmime0 hold
EOF

		if ! dpkg -s zarafa &>/dev/null || dpkg -s zarafa | grep -qE 'deinstall|not-installed'; then
			LICENSED=`ls zarafa-licensed_$VERSION-${RELEASE}_$ARCH.deb 2> /dev/null`
			WEBAPP=`ls -1 zarafa-webapp_*deb 2> /dev/null`
			if ! dpkg -i libvmime0_0.9.2*-*_$ARCH.deb libical0_0.44-*_$ARCH.deb \
				libkyotocabinet16_1.2.72*_$ARCH.deb \
				zarafa_$VERSION-${RELEASE}_all.deb \
				php5-mapi_$VERSION-${RELEASE}_$ARCH.deb \
				python-mapi_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-client_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-common_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-dagent_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-gateway_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-ical_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-search_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-libarchiver_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-libs_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-monitor_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-server_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-spooler_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-utils_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-contacts_$VERSION-${RELEASE}_$ARCH.deb \
				zarafa-webaccess_$VERSION-${RELEASE}_all.deb \
				$LICENSED $WEBAPP; \
			then

				echo
				echo "Will now try to automatically resolve above dependency problems."
				echo

				apt-get -f install
				if [ $? -ne 0 ]; then
					echo "Please resolve above errors from apt-get / dpkg, and restart the installer."
					exit 1
				fi
			fi
			if [ -x /etc/init.d/apache2 ]; then
				echo "Restarting Apache to load the Zarafa PHP-extension."
				/etc/init.d/apache2 restart
			else
				echo "Unable to restart Apache."
				echo "Please restart your webserver to load the Zarafa PHP-extension."
			fi
		else
			echo "Already installed, skipping packages."
		fi
	else
#
# RPM packages
#
		if ! rpm -q zarafa &> /dev/null; then
			LICENSED=`ls zarafa-licensed-$VERSION-$RELEASE.$ARCH.rpm 2> /dev/null`
			WEBAPP=`ls -1 zarafa-webapp-*rpm 2> /dev/null`
			if ! rpm -U -h --replacepkgs libvmime-0.9.2*-*.$ARCH.rpm libical-0.44-*.$ARCH.rpm \
				kyotocabinet-lib-1.2.72-*.$ARCH.rpm \
				zarafa-$VERSION-$RELEASE.$ARCH.rpm \
				php*-mapi-$VERSION-$RELEASE.$ARCH.rpm \
				python-mapi-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-client-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-common-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-dagent-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-gateway-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-ical-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-search-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-libarchiver-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-libs-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-monitor-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-server-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-spooler-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-utils-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-contacts-$VERSION-$RELEASE.$ARCH.rpm \
				zarafa-webaccess-$VERSION-$RELEASE.noarch.rpm \
				$LICENSED $WEBAPP; \
			then
				echo
				echo "Please resolve above errors from rpm, and restart the installer."
				exit 1
			else
				if [ -x /etc/init.d/apache2 ]; then
					echo "Restarting Apache to load the Zarafa PHP-extension."
					/etc/init.d/apache2 restart
				elif [ -x /etc/init.d/httpd ]; then
					echo "Restarting Apache to load the Zarafa PHP-extension."
					/etc/init.d/httpd restart
				else
					echo "Unable to restart Apache."
					echo "Please restart your webserver to load the Zarafa PHP-extension."
				fi
			fi
		else
			echo "Already installed, skipping packages."
		fi
	fi

	echo
	echo "Installation of the software is complete."
fi

#--- configuration ---

echo "We will now configure Zarafa services."
echo
echo "Please answer the following questions:"
echo

if [ -x /usr/bin/zarafa-licensed ]; then
	if [ ! -f /etc/zarafa/license/base ]; then
		mkdir -p /etc/zarafa/license
		SERIAL="0"
		while [ ${#SERIAL} -ne 25 -a ${#SERIAL} -ne 0 ]; do
			if [ -n "$SERIAL" ]; then
				echo "Serial number must be 25 characters or empty"
			fi
			echo -ne "Server serial number (leave empty for none) []\t\t: "
			read SERIAL
		done
		SERIAL=`echo $SERIAL | tr [a-z] [A-Z]`
		echo $SERIAL > /etc/zarafa/license/base
		echo
		echo "The correctness of the serial will be checked when the server starts."
		echo
	fi
fi


#
#--- server config ---
#
if [ ! -f /etc/zarafa/server.cfg ]; then
	echo "Installing new configation file for the server."
	if [ -f /usr/share/doc/zarafa/example-config/server.cfg.gz ]; then
		zcat /usr/share/doc/zarafa/example-config/server.cfg.gz | sed -e 's/\r$//g' > /etc/zarafa/server.cfg
	else
		doscopy /usr/share/doc/zarafa/example-config/server.cfg /etc/zarafa/server.cfg
	fi
fi

if [ -x /usr/bin/zarafa-licensed ]; then
	if [ ! -f /etc/zarafa/licensed.cfg ]; then
		if [ -f /usr/share/doc/zarafa-licensed/example-config/licensed.cfg.gz ]; then
			zcat /usr/share/doc/zarafa-licensed/example-config/licensed.cfg.gz | sed -e 's/\r$//g' > /etc/zarafa/licensed.cfg
		else
			doscopy /usr/share/doc/zarafa-licensed/example-config/licensed.cfg /etc/zarafa/licensed.cfg
		fi
	fi
fi

import /etc/zarafa/server.cfg server_tcp_port SERVERPORT
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_database DATABASENAME
while true; do
	echo -ne "MySQL Server [$MYSQLSERVER]\t: "
	read MYSQLSERVER_NEW
	if [ ! -z "$MYSQLSERVER_NEW" ]; then MYSQLSERVER=$MYSQLSERVER_NEW; fi

	echo -ne "MySQL Port [$MYSQLPORT]\t\t: "
	read MYSQLPORT_NEW
	if [ ! -z "$MYSQLPORT_NEW" ]; then MYSQLPORT=$MYSQLPORT_NEW; fi

	echo -ne "MySQL User [$MYSQLUSER]\t\t: "
	read MYSQLUSER_NEW
	if [ ! -z "$MYSQLUSER_NEW" ]; then MYSQLUSER=$MYSQLUSER_NEW; fi

	echo -ne "MySQL Password (won't be shown while typing) []\t\t: "
	read -s MYSQLPASS
	echo

	echo -ne "Database name [$DATABASENAME]\t\t: "
	read DATABASENAME_NEW
	if [ ! -z "$DATABASENAME_NEW" ]; then DATABASENAME=$DATABASENAME_NEW; fi

	mysql -h "$MYSQLSERVER" -P "$MYSQLPORT" --password="$MYSQLPASS" -u "$MYSQLUSER" -e 'SET SESSION group_concat_max_len = 32768'
	if [ $? -ne 0 ]; then
		echo
		echo "Your MySQL settings are incorrect or the server is not version 4.1 or higher."
		echo "Press <enter> to try again, or <ctrl-c> to stop."
		read dummy
	else
		break
	fi
done


import /etc/zarafa/server.cfg log_method SERVER_LOGMETHOD
import /etc/zarafa/server.cfg log_file SERVER_LOGFILE
echo -ne "Log method (file or syslog) [$SERVER_LOGMETHOD]\t\t: "
read SERVER_LOGMETHOD_NEW
if [ ! -z "$SERVER_LOGMETHOD_NEW" ]; then SERVER_LOGMETHOD=$SERVER_LOGMETHOD_NEW; fi
if [ "$SERVER_LOGMETHOD" == "file" ]; then
	echo -ne "Log filename [$SERVER_LOGFILE]\t: "
	read SERVER_LOGFILE_NEW
	if [ ! -z "$SERVER_LOGFILE_NEW" ]; then SERVER_LOGFILE=$SERVER_LOGFILE_NEW; fi
	logdir=`dirname $SERVER_LOGFILE`
	if [ ! -d $logdir ]; then
		mkdir -p $logdir
	fi
fi

replace /etc/zarafa/server.cfg mysql_host $MYSQLSERVER
replace /etc/zarafa/server.cfg mysql_port $MYSQLPORT
replace /etc/zarafa/server.cfg mysql_user $MYSQLUSER
replace /etc/zarafa/server.cfg mysql_password $MYSQLPASS
replace /etc/zarafa/server.cfg mysql_database $DATABASENAME
replace /etc/zarafa/server.cfg log_method $SERVER_LOGMETHOD
if [ $SERVER_LOGMETHOD == "file" ]; then
	replace /etc/zarafa/server.cfg log_file $SERVER_LOGFILE
fi
if [ $ARCH == "x86_64" -a -d /usr/lib64/zarafa ]; then
	replace /etc/zarafa/server.cfg plugin_path /usr/lib64/zarafa
fi

chmod 600 /etc/zarafa/server.cfg

if mysql -h "$MYSQLSERVER" -P "$MYSQLPORT" -u "$MYSQLUSER" --password="$MYSQLPASS" mysql -e "use $DATABASENAME" 2>/dev/null; then
	echo
	echo "WARNING: Database name is already in use!"
	echo "         You may not be able to start the server correctly"
	echo "         if this is a new installation."
	echo
	echo "Press <enter> to continue, or <ctrl-c> to stop."
	read dummy
fi

#
#--- spooler config ---
#
echo
echo "Creating spooler configuration ..."
echo "The spooler program sends outgoing e-mail using an SMTP server"
echo "Please answer the follow questions:"
echo

if [ ! -f /etc/zarafa/spooler.cfg ]; then
	if [ -f /usr/share/doc/zarafa-spooler/example-config/spooler.cfg.gz ]; then
		zcat /usr/share/doc/zarafa-spooler/example-config/spooler.cfg.gz | sed -e 's/\r$//g' >  /etc/zarafa/spooler.cfg
	else
		doscopy /usr/share/doc/zarafa-spooler/example-config/spooler.cfg /etc/zarafa/spooler.cfg
	fi
fi

import /etc/zarafa/spooler.cfg smtp_server SMTPSERVER
import /etc/zarafa/spooler.cfg log_method LOGMETHOD
import /etc/zarafa/spooler.cfg log_file LOGFILE
echo -en "SMTP server [$SMTPSERVER]\t\t: "
read SMTPSERVER_NEW
if [ ! -z "$SMTPSERVER_NEW" ]; then SMTPSERVER=$SMTPSERVER_NEW; fi

echo -ne "Log method (file or syslog) [$LOGMETHOD]\t\t: "
read LOGMETHOD_NEW
if [ ! -z "$LOGMETHOD_NEW" ]; then LOGMETHOD=$LOGMETHOD_NEW; fi
if [ "$LOGMETHOD" == "file" ]; then
	echo -ne "Log filename [$LOGFILE]\t: "
	read LOGFILE_NEW
	if [ ! -z "$LOGFILE_NEW" ]; then LOGFILE=$LOGFILE_NEW; fi
	logdir=`dirname $LOGFILE`
	if [ ! -d $logdir ]; then
		mkdir -p $logdir
	fi
fi

replace /etc/zarafa/spooler.cfg smtp_server $SMTPSERVER
replace /etc/zarafa/spooler.cfg log_method $LOGMETHOD
replace /etc/zarafa/spooler.cfg log_file $LOGFILE


#
#--- monitor config ---
#
echo
echo "Creating monitor configuration ..."
echo "The monitor program sends quota warning mails to users who are over quota."
echo "Please answer the follow questions:"
echo

if [ ! -f /etc/zarafa/monitor.cfg ]; then
	if [ -f /usr/share/doc/zarafa-monitor/example-config/monitor.cfg.gz ]; then
		cat /usr/share/doc/zarafa-monitor/example-config/monitor.cfg.gz | sed -e 's/\r$//g' > /etc/zarafa/monitor.cfg
	else
		doscopy /usr/share/doc/zarafa-monitor/example-config/monitor.cfg /etc/zarafa/monitor.cfg
	fi
fi

import /etc/zarafa/monitor.cfg mailquota_resend_interval INTERVAL
import /etc/zarafa/monitor.cfg log_method LOGMETHOD
import /etc/zarafa/monitor.cfg log_file LOGFILE
echo -en "Warning e-mail resend interval (in days) [$INTERVAL]\t: "
read INTERVAL
if [ -z "$INTERVAL" ]; then INTERVAL=1; fi

echo -ne "Log method (file or syslog) [$LOGMETHOD]\t\t: "
read LOGMETHOD_NEW
if [ ! -z "$LOGMETHOD_NEW" ]; then LOGMETHOD=$LOGMETHOD_NEW; fi
if [ "$LOGMETHOD" == "file" ]; then
	echo -ne "Log filename [$LOGFILE]\t: "
	read LOGFILE_NEW
	if [ ! -z "$LOGFILE_NEW" ]; then LOGFILE=$LOGFILE_NEW; fi
	logdir=`dirname $LOGFILE`
	if [ ! -d $logdir ]; then
		mkdir -p $logdir
	fi
fi

replace /etc/zarafa/monitor.cfg mailquota_resend_interval $INTERVAL
replace /etc/zarafa/monitor.cfg log_method $LOGMETHOD
replace /etc/zarafa/monitor.cfg log_file $LOGFILE


#
#--- gateway config ---
#
if [ ! -f /etc/zarafa/gateway.cfg ]; then
	if [ -f /usr/share/doc/zarafa-gateway/example-config/gateway.cfg.gz ]; then
		zcat /usr/share/doc/zarafa-gateway/example-config/gateway.cfg.gz | sed -e 's/\r$//g' > /etc/zarafa/gateway.cfg
	else
		doscopy /usr/share/doc/zarafa-gateway/example-config/gateway.cfg /etc/zarafa/gateway.cfg
	fi
fi

echo
echo    "Do you wish to run the Zarafa gateway (POP3/IMAP)"
echo -n " services as well? [y] "
read RUNGATEWAY
if [ -z "$RUNGATEWAY" ]; then RUNGATEWAY="y"; fi
if [ "${RUNGATEWAY:0:1}" == "y" ]; then

	import /etc/zarafa/gateway.cfg pop3_port POP3PORT
	import /etc/zarafa/gateway.cfg imap_port IMAPPORT
	import /etc/zarafa/gateway.cfg log_method LOGMETHOD
	import /etc/zarafa/gateway.cfg log_file LOGFILE

	echo -ne "Log method (file or syslog) [$LOGMETHOD]\t\t: "
	read LOGMETHOD_NEW
	if [ ! -z "$LOGMETHOD_NEW" ]; then LOGMETHOD=$LOGMETHOD_NEW; fi
	if [ "$LOGMETHOD" == "file" ]; then
		echo -ne "Log filename [$LOGFILE]\t: "
		read LOGFILE_NEW
		if [ ! -z "$LOGFILE_NEW" ]; then LOGFILE=$LOGFILE_NEW; fi
		logdir=`dirname $LOGFILE`
		if [ ! -d $logdir ]; then
			mkdir -p $logdir
		fi
	fi

	echo -en 'POP3 port [110]\t\t: '
	read POP3PORT_NEW
	if [ ! -z "$POP3PORT_NEW" ]; then POP3PORT=$POP3PORT_NEW; fi
	if [ -z "$POP3PORT" -o "$POP3PORT" == "110" ]; then
		POP3PORT="110";
		echo
		echo '**Warning: When another service is currently providing POP3 support,'
		echo '           the gateway may not start because the port is already in use.'
		echo
	fi
	echo -en 'IMAP port [143]\t\t: '
	read IMAPPORT_NEW
	if [ ! -z "$IMAPPORT_NEW" ]; then IMAPPORT=$IMAPPORT_NEW; fi
	if [ -z "$IMAPPORT" -o "$IMAPPORT" == "143" ]; then
		IMAPPORT="143";
		echo
		echo '**Warning: When another service is currently providing IMAP support,'
		echo '           the gateway may not start because the port is already in use.'
		echo
	fi

	replace /etc/zarafa/gateway.cfg log_method $LOGMETHOD
	replace /etc/zarafa/gateway.cfg log_file $LOGFILE
	replace /etc/zarafa/gateway.cfg pop3_port $POP3PORT
	replace /etc/zarafa/gateway.cfg imap_port $IMAPPORT
else
	replace /etc/zarafa/server.cfg disabled_features imap
fi

#
#--- ical config ---
#
if [ ! -f /etc/zarafa/ical.cfg ]; then
	if [ -f /usr/share/doc/zarafa-ical/example-config/ical.cfg.gz ]; then
		zcat /usr/share/doc/zarafa-ical/example-config/ical.cfg.gz | sed -e 's/\r$//g' > /etc/zarafa/ical.cfg
	else
		doscopy /usr/share/doc/zarafa-ical/example-config/ical.cfg /etc/zarafa/ical.cfg
	fi
fi

echo
echo    "Do you wish to run the Zarafa Ical gateway (Ical/CalDAV)"
echo -n " services as well? [y] "
read RUNICAL
if [ -z "$RUNICAL" ]; then RUNGATEWAY="y"; fi
if [ "${RUNICAL:0:1}" == "y" ]; then

	import /etc/zarafa/ical.cfg ical_port POP3PORT
	import /etc/zarafa/ical.cfg log_method LOGMETHOD
	import /etc/zarafa/ical.cfg log_file LOGFILE

	echo -ne "Log method (file or syslog) [$LOGMETHOD]\t\t: "
	read LOGMETHOD_NEW
	if [ ! -z "$LOGMETHOD_NEW" ]; then LOGMETHOD=$LOGMETHOD_NEW; fi
	if [ "$LOGMETHOD" == "file" ]; then
		echo -ne "Log filename [$LOGFILE]\t: "
		read LOGFILE_NEW
		if [ ! -z "$LOGFILE_NEW" ]; then LOGFILE=$LOGFILE_NEW; fi
		logdir=`dirname $LOGFILE`
		if [ ! -d $logdir ]; then
			mkdir -p $logdir
		fi
	fi

	echo -en 'ICal port [8080]\t\t: '
	read ICALPORT_NEW
	if [ ! -z "$ICALPORT_NEW" ]; then ICALPORT=$ICALPORT_NEW; fi
	if [ -z "$ICALPORT" -o "$ICALPORT" == "8080" ]; then
		ICALPORT="8080";
		echo
		echo '**Warning: When another service is currently providing ICal support,'
		echo '           the gateway may not start because the port is already in use.'
		echo
	fi

	replace /etc/zarafa/ical.cfg log_method $LOGMETHOD
	replace /etc/zarafa/ical.cfg log_file $LOGFILE
	replace /etc/zarafa/ical.cfg ical_port $ICALPORT
fi

echo
echo -n "Do you wish to run the Zarafa search service? [y] "
read RUNSEARCH
if [ -z "$RUNSEARCH" ]; then RUNSEARCH="y"; fi
if [ "${RUNSEARCH:0:1}" == "y" ]; then
	if [ $DISTRO == "Debian" -o $DISTRO == "Ubuntu" ]; then
		if ! dpkg -s zarafa-search &>/dev/null || dpkg -s zarafa-search | grep -qE 'deinstall|not-installed'; then
			if ! dpkg -i zarafa-search_$VERSION-${RELEASE}_$ARCH.deb; then
				echo
				echo "Will now try to automatically resolve above dependency problems."
				echo

				apt-get -f install
				if [ $? -ne 0 ]; then
					echo "Please resolve above errors from apt-get / dpkg, and restart the installer."
					exit 1
				fi
			else
				if [ $? -ne 0 ]; then
					echo "Please resolve above errors from dpkg after the installation."
					RUNSEARCH='n'
				fi
			fi
		else
			echo "Already installed, skipping packages."
		fi
	else
		# rpms, also install catdoc
		if ! rpm -q zarafa-search &> /dev/null; then
			if ! rpm -U -h --replacepkgs catdoc-0.94.2-*.$ARCH.rpm zarafa-search-$VERSION-$RELEASE.$ARCH.rpm; then
				echo
				echo "Please resolve above errors from rpm, and restart the installer."
				exit 1
			fi
		else
			echo "Already installed, skipping packages."
		fi
	fi
fi

echo
echo
echo "Done configuring services"
echo
echo "Press <enter> to continue..."
read dummy

#
#---------------------
#
if [ $DISTRO == "Debian" -o $DISTRO == "Ubuntu" ]; then
	# deb packages start services, so use restart
	STARTPARAM=restart
else
	STARTPARAM=start
fi


echo
echo
echo "Your server has been configured with the following parameters:"
echo -e "\tMySQL server:\t\t$MYSQLSERVER"
echo -e "\tMySQL port:\t\t$MYSQLPORT"
echo -e "\tMySQL user:\t\t$MYSQLUSER"
echo -en "\tMySQL password:\t\t"
echo $MYSQLPASS | sed -e 's/./*/g'
echo -e "\tLog method:\t\t$SERVER_LOGMETHOD"
if [ $SERVER_LOGMETHOD == "file" ]; then
    echo -e "\tLog file:\t\t$SERVER_LOGFILE"
fi
echo
echo "The server is running on port $SERVERPORT"
echo
echo "If you wish to make changes, please edit the file /etc/zarafa/server.cfg"
echo "or /etc/zarafa/license/base for the serial number."
echo
echo "If you wish to make changes to the spooler or monitor configuration,"
echo "please edit the file spooler.cfg or monitor.cfg in the directory"
echo "/etc/zarafa/."
echo
echo "To start Zarafa, you must run"
echo
echo "/etc/init.d/zarafa-server start"
echo
echo -ne "Do you wish to start the server now? [y]"
read START_SERVER
BOOT=no
if [ -z "$START_SERVER" ]; then START_SERVER="y"; fi
if [ "${START_SERVER:0:1}" == "y" ]; then
	/etc/init.d/zarafa-server $STARTPARAM
	if [ $? -ne 0 ]; then
		echo 'The Zarafa server did not start correctly!'
		START_SERVER=no
	else
		if [ ! -z `which chkconfig` ]; then
			BOOT=yes
			echo 'Enabling Zarafa server on reboot'
			if [ "$MYSQLSERVER" == "localhost" ]; then
				chkconfig --add zarafa-server
			else
			# force enable, removes depend on mysql service
				echo 'Please make sure your MySQL server is running before Zarafa is started'
				chkconfig --add --force zarafa-server
			fi
		fi
	fi
fi
echo
if [ "${START_SERVER:0:1}" == "y" ]; then
	echo
	echo "We will now create the public store with the 'zarafa-admin -s' command"
	zarafa-admin -s
	echo
	echo "Use the 'zarafa-admin -c' command to create users."
else
	echo "After the Zarafa server is started, use 'zarafa-admin -s' to create"
	echo "the public store. And 'zarafa-admin -c' to create users."
fi
echo "Read the help or manual page for details, by typing:"
echo
echo "man zarafa-admin"
echo
echo "Press <enter> to continue..."
read dummy

echo "For message delivery, 'zarafa-dagent' is available. Please read the"
echo "Installation documentation (PDF) on how to configure your mailserver."
echo
echo "To start the dagent, spooler, monitor and gateway services, you must run"
echo
echo "/etc/init.d/zarafa-dagent start"
echo "/etc/init.d/zarafa-spooler start"
echo "/etc/init.d/zarafa-monitor start"
echo "/etc/init.d/zarafa-gateway start"
echo "/etc/init.d/zarafa-ical start"
echo
if [ "${START_SERVER:0:1}" == "y" ]; then
	echo -ne "Do you wish to start the configured services now? [y]"
	read START_SERVICES
	if [ -z "$START_SERVICES" ]; then START_SERVICES="y"; fi
	rc=0
	if [ "${START_SERVICES:0:1}" == "y" ]; then
		/etc/init.d/zarafa-dagent $STARTPARAM
		rc=$[$rc+$?]
		if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-dagent; fi
		/etc/init.d/zarafa-spooler $STARTPARAM
		rc=$[$rc+$?]
		if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-spooler; fi
		/etc/init.d/zarafa-monitor $STARTPARAM
		rc=$[$rc+$?]
		if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-monitor; fi
		if [ "${RUNGATEWAY:0:1}" == "y" ]; then
			/etc/init.d/zarafa-gateway $STARTPARAM
			rc=$[$rc+$?]
			if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-gateway; fi
		fi
		if [ "${RUNICAL:0:1}" == "y" ]; then
			/etc/init.d/zarafa-ical $STARTPARAM
			rc=$[$rc+$?]
			if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-ical; fi
		fi
		if [ "${RUNSEARCH:0:1}" == "y" ]; then
			/etc/init.d/zarafa-search $STARTPARAM
			rc=$[$rc+$?]
			if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-search; fi
		fi
		if [ -x /etc/init.d/zarafa-licensed ]; then
			/etc/init.d/zarafa-licensed $STARTPARAM
			rc=$[$rc+$?]
			if [ "$BOOT" == "yes" ]; then chkconfig --add zarafa-licensed; fi
		fi
	fi
	if [ $rc -ne 0 ]; then
		echo "One or more services could not be started."
	fi
fi
echo "Press <enter> to continue"
read dummy

if [ $SERVERPORT -ne 80 ]; then
	echo
	echo "Currently the server is configured to accept SOAP connections on port"
	echo "$SERVERPORT. If you wish to connect Outlook clients on port 80,"
	echo "please setup your web server to proxy HTTP requests with"
	echo "URI=/zarafa to $SERVERPORT. This can be done in Apache2 with mod_proxy"
	echo "with the ProxyPass configuration directive:"
	echo
	echo "ProxyPass        /zarafa http://localhost:$SERVERPORT"
	echo "ProxyPassReverse /zarafa http://localhost:$SERVERPORT"
	echo
	echo "The modules 'proxy' and 'proxy_http' need to be enabled."
	echo

	case "$DISTRO" in
	"SuSE"|"SLES")
		echo "Edit /etc/sysconfig/apache2 to enable these modules."
		echo "Add 'proxy' and 'proxy_http' to the APACHE_MODULES=\"...\" line."
		echo "Run '/sbin/SuSEconfig --module apache2' after editing the file."
		;;
	"RedHat")
		echo "Edit /etc/httpd/conf/httpd.conf and remove comments"
		echo "from following lines:"
		echo
		echo "LoadModule proxy modules/mod_proxy.so"
		echo "LoadModule proxy_http modules/mod_proxy_http.so"
		;;
	"Debian"|"Ubuntu")
		echo "Enter 'a2enmod proxy' to enable the mod_proxy module."
		;;	
	esac
	echo "Do not forget to reload apache when you have altered the configuration."
	echo
fi

echo "The webaccess may already be available from http://server/webaccess."
echo "The new WebApp may already be available from http://server/webapp."
echo

if [ $DISTRO == "RedHat" -a -x zarafa-config-users/install.sh ]; then
	echo "Press <enter> to continue"
	read dummy

	echo "Do you want to start the zarafa-config-users installation too?"
	echo "This installation will configure your LDAP service for use with Zarafa"
	echo -ne "Install? [y] : "
	read answer
	if [ ${#answer} -eq 0 ]; then
		answer=y
	fi
	if [ "${answer:0:1}" == "y" ]; then
		echo "Starting zarafa-config-users installation"
		cd zarafa-config-users && ./install.sh
	fi
fi
