#!/bin/bash
#
# Univention
#  listener module: creating mailboxes and sieve scripts
#
# Copyright 2008-2012 Univention GmbH (http://www.univention.de/)
#
# and 
#     iKu Systems & Services GmbH & Co. KG
#
# http://www.iku-systems.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

. /usr/share/univention-lib/all.sh

# OX install-script for UCS 2

eval `univention-config-registry shell`

OXADMINMASTER="oxadminmaster"
OXDBUSER="openexchange"

OXSECRETPATH="/etc/ox-secrets"
mkdir -m 700 "$OXSECRETPATH" 2>/dev/null
OXPATH="/opt/open-xchange"
OXADMINBIN="$OXPATH/sbin"
OXMASTERSECRETFILE="$OXSECRETPATH/master.secret"
OXDBSECRETFILE="$OXSECRETPATH/dbuser.secret"

## Tests: Verbindungen zu mysql, join-status
if ! test -e "/usr/share/univention-join/.joined"; then
	echo "The server has not joined yet"
	exit 1
fi

CONTEXTNUM=10
OXCONTEXTNAME=context$CONTEXTNUM

if univention-ldapsearch cn="$OXCONTEXTNAME" | grep -q "^cn: $OXCONTEXTNAME"; then
	echo "Context10 already exsists. Not installing."
	exit 0
fi

if [ "$OXDB" -o "$OXDBPW" ]; then
	if ! echo | mysql -h "$OXDB" -u "$OXDBUSER" --password="$OXDBPW" >/dev/null 2>&1; then
		echo "Connection to MySQL server failed."
		echo "Please check env. variables 'OXDB' and 'OXDBPW'."
		exit 1
	fi
else
	if [ -e /etc/mysql.secret ]; then
		if ! echo | mysql -u "root" --password="$(cat /etc/mysql.secret)" >/dev/null 2>&1; then
			echo "Connection to MySQL server failed. Aborting"
			exit 1
		fi
	else
		if ! echo | mysql; then
			echo "Connection to MySQL server failed. Aborting"
			exit 1
		fi
	fi
fi

if [ ! -f "$OXMASTERSECRETFILE" ]; then
	SERVERNAME="localhost"
	OXFILESTORE="/var/oxfilestore"

	mkdir -p "$OXFILESTORE" 2>/dev/null
	chmod 700 "$OXFILESTORE"
	chown open-xchange "$OXFILESTORE"
	OXFILESTORESIZE=$( expr $(df -P -k "$OXFILESTORE" | tail -1 | awk '{ print $4 }') / 1126)

	OXADMINMASTERPW=$("$OXPATH/sbin/genpw.sh")
	if [ "$OXDB" -a "$OXDBPW" ]; then
		OXDBUSERPW="$OXDBPW"
		CONFIGDBHOST="$OXDB"
	else
		OXDBUSERPW=$("$OXPATH/sbin/genpw.sh")
		CONFIGDBHOST=localhost
	fi
	
	##########################
	### Baseconf-Variablen ###
	##########################
	
	touch "$OXMASTERSECRETFILE"
	chmod 600 "$OXMASTERSECRETFILE"
	echo "$OXADMINMASTERPW" > "$OXMASTERSECRETFILE"
	
	touch "$OXDBSECRETFILE"
	chmod 600 "$OXDBSECRETFILE"
	echo "$OXDBUSERPW" > "$OXDBSECRETFILE"
	
	##########################
	### MySQL - Bootstrap  ###
	##########################
	if ! [ "$OXDB" ]; then
		mysql $MYSQLOPTIONS << EOF
GRANT ALL PRIVILEGES ON *.* TO '$OXDBUSER'@'localhost' IDENTIFIED BY '$OXDBUSERPW' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF

	fi

	if [ ! -e /etc/mysql.secret ]; then
		tmp=$(mktemp)
		makepasswd --nocrypt > $tmp
		passwd=$(cat $tmp)
		hash=$(mkpasswd --hash=md5 -s < $tmp)
		touch /etc/mysql.secret
		chmod 600 /etc/mysql.secret
		echo $passwd > /etc/mysql.secret
		chown root /etc/mysql.secret
		rm -f $tmp
		# set passwd for database user reservation
		/usr/bin/mysqladmin -u root password "$passwd"
		unset passwd
	fi

	$OXADMINBIN/initconfigdb \
		--configdb-user="$OXDBUSER" \
		--configdb-pass="$OXDBUSERPW" \
		--configdb-host="$CONFIGDBHOST" 
	
	##########################
	### Vorbereitung OX    ###
	##########################
	
	$OXADMINBIN/oxinstaller \
		--servername=$SERVERNAME \
		--configdb-pass="$OXDBUSERPW" \
		--master-pass="$OXADMINMASTERPW" \
		--configdb-readhost="$CONFIGDBHOST" \
		--configdb-writehost="$CONFIGDBHOST" \
		--master-user=$OXADMINMASTER \
		--object-link-hostname="$(hostname -f)/ox6" \
		--no-license 
	
	##########################
	### Konfig. LDAPauth   ###
	##########################


	cp "$OXPATH/etc/admindaemon/User.properties" \
		"$OXPATH/etc/admindaemon/User.properties.bak"
	sed 's/^USERNAME_CHANGEABLE=.*$/USERNAME_CHANGEABLE=true/' \
		"$OXPATH/etc/admindaemon/User.properties.bak" \
		| sed 's/^CHECK_USER_UID_FOR_NOT_ALLOWED_CHARS=.*$/CHECK_USER_UID_FOR_NOT_ALLOWED_CHARS=false/' \
		| sed 's/^PRIMARY_MAIL_UNCHANGEABLE=.*$/PRIMARY_MAIL_UNCHANGEABLE=false/' \
		> "$OXPATH/etc/admindaemon/User.properties"

	cp "$OXPATH/etc/admindaemon/Group.properties" \
		"$OXPATH/etc/admindaemon/Group.properties.bak"
	sed 's/^CHECK_GROUP_UID_FOR_NOT_ALLOWED_CHARS=.*$/CHECK_GROUP_UID_FOR_NOT_ALLOWED_CHARS=false/' \
		"$OXPATH/etc/admindaemon/Group.properties.bak" \
		> "$OXPATH/etc/admindaemon/Group.properties"

	cp "$OXPATH/etc/groupware/mailfilter.properties" \
		"$OXPATH/etc/groupware/mailfilter.properties.bak"
	sed 's/^SIEVE_LOGIN_TYPE=.*$/SIEVE_LOGIN_TYPE=user/' \
		"$OXPATH/etc/groupware/mailfilter.properties.bak" \
		| sed 's/^SIEVE_CREDSRC=.*$/SIEVE_CREDSRC=imapLogin/' \
		> "$OXPATH/etc/groupware/mailfilter.properties"
	
	cp "$OXPATH/etc/common/foldercache.properties" \
		"$OXPATH/etc/common/foldercache.properties.bak"
	sed s/"^ENABLE_INTERNAL_USER_EDIT=.*$"/"ENABLE_INTERNAL_USER_EDIT=false"/ \
		"$OXPATH/etc/common/foldercache.properties.bak" \
		> "$OXPATH/etc/common/foldercache.properties"
	
	cp "$OXPATH/etc/groupware/mail.properties" \
		"$OXPATH/etc/groupware/mail.properties.bak"
	sed s/"^com.openexchange.mail.adminMailLoginEnabled=.*$"/"com.openexchange.mail.adminMailLoginEnabled=true"/ \
		"$OXPATH/etc/groupware/mail.properties.bak" \
		> "$OXPATH/etc/groupware/mail.properties"
	
	if [ -e "$OXPATH/etc/groupware/configjump.properties" ]; then
		cp "$OXPATH/etc/groupware/configjump.properties" \
			"$OXPATH/etc/groupware/configjump.properties.bak"
		grep -v "^URL=" \
			"$OXPATH/etc/groupware/configjump.properties.bak" \
			> "$OXPATH/etc/groupware/configjump.properties"
	fi
	echo "URL=https://$ldap_master/umc" \
		>> "$OXPATH/etc/groupware/configjump.properties"
	
	cp "$OXPATH/etc/groupware/sessioncache.ccf" \
		"$OXPATH/etc/groupware/sessioncache.ccf.bak"
	sed s/"^jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryPort=.*$"/"jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryPort=6790"/ \
		"$OXPATH/etc/groupware/sessioncache.ccf.bak" \
		> "$OXPATH/etc/groupware/sessioncache.ccf"

	cp "$OXPATH/etc/groupware/sessiond.properties" \
		"$OXPATH/etc/groupware/sessiond.properties.bak"
	sed s/"^com.openexchange.sessiond.maxSessionPerUser:.*$"/"com.openexchange.sessiond.maxSessionPerUser:1000"/ \
		"$OXPATH/etc/groupware/sessiond.properties.bak" \
		> "$OXPATH/etc/groupware/sessiond.properties"

	sed -i s/"^com.openexchange.sessiond.maxSessionPerUser:.*$"/"com.openexchange.sessiond.maxSessionPerUser:1000"/ \
		"$OXPATH/etc/groupware/sessiond.properties"
	
	for DIR in groupware admindaemon; do
		F="$OXPATH/etc/$DIR/cache.ccf"
		[ -f $F ] && sed -i 's/^jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled=true/jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled=false/' $F
	done
	
	F="$OXPATH/etc/groupware/sessioncache.ccf"
	[ -f $F ] && sed -i 's/^jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryEnabled=true/jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryEnabled=false/' $F
	
	sed -i 's/^.*jcs.auxiliary.LTCP.attributes.TcpServers=.*$/jcs.auxiliary.LTCP.attributes.TcpServers=127.0.0.1:57462/' \
		"$OXPATH/etc/admindaemon/cache.ccf"
	sed -i 's/^.*jcs.auxiliary.LTCP.attributes.TcpServers=.*$/jcs.auxiliary.LTCP.attributes.TcpServers=127.0.0.1:57461/' \
		"$OXPATH/etc/groupware/cache.ccf"
	sed -i 's/^com.openexchange.imap.spamHandler=.*/com.openexchange.imap.spamHandler=SpamAssassin/' \
		"$OXPATH/etc/groupware/imap.properties"


	for DIR in groupware admindaemon; do
		F="$OXPATH/etc/$DIR/cache.ccf"
		[ -f $F ] && sed -i 's/^jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled=true/jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled=false/' $F || true
	done

	sed -i 's/^.*jcs.auxiliary.LTCP.attributes.TcpServers=.*$/jcs.auxiliary.LTCP.attributes.TcpServers=127.0.0.1:57462/' \
		"$OXPATH/etc/admindaemon/cache.ccf"
	sed -i 's/^.*jcs.auxiliary.LTCP.attributes.TcpServers=.*$/jcs.auxiliary.LTCP.attributes.TcpServers=127.0.0.1:57461/' \
		"$OXPATH/etc/groupware/cache.ccf"
	sed -i 's/^com.openexchange.imap.spamHandler=.*/com.openexchange.imap.spamHandler=SpamAssassin/' \
		"$OXPATH/etc/groupware/imap.properties"
	F="$OXPATH/etc/groupware/sessioncache.ccf"
	[ -f $F ] && sed -i 's/^jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryEnabled=true/jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryEnabled=false/' $F || true

	 sed -i "s/^com.openexchange.push.udp.senderAddress:.*$/# com.openexchange.push.udp.senderAddress:$hostname.$domainname/" \
	 	"$OXPATH/etc/groupware/push.properties"
	
	
	##########################
	### (Re-)Start Dienste ###
	##########################

	
	/etc/init.d/open-xchange-groupware restart
	/etc/init.d/open-xchange-admin restart

	echo -n "Waiting for Open-Xchange daemons to be started ."
	while ! "$OXPATH/sbin/allpluginsloaded" >/dev/null 2>&1; do
		sleep 5
		echo -n "."
	done
	echo
	
	/etc/init.d/apache2 restart

	##########################
	### Erzeugen OX-env.   ###
	##########################
	
	$OXADMINBIN/registerdatabase \
		--name="$SERVERNAME" \
		--dbpasswd="$OXDBUSERPW" \
		--master=true \
		--hostname="$CONFIGDBHOST" \
		-A "$OXADMINMASTER" \
		-P "$OXADMINMASTERPW"
	
	$OXADMINBIN/registerserver \
		--name="$SERVERNAME" \
		-A "$OXADMINMASTER" \
		-P "$OXADMINMASTERPW"
	
	$OXADMINBIN/registerfilestore \
		--storepath=file://$OXFILESTORE \
		--storesize="$OXFILESTORESIZE" \
		-A "$OXADMINMASTER" \
		-P "$OXADMINMASTERPW"
fi
/usr/sbin/ox-register-to-ucs-domain $@ || exit 1
	
	## for debug
	# univention-config-registry set listener/debug/level=3
