#! /bin/sh

#export DEBCONF_DEBUG=developer
set -e
# das hier muss ganz oben stehen (man debconf-devel)
. /usr/share/debconf/confmodule
db_version 2.0


webserver_soft_reload() {
	if command -v invoke-rc.d >/dev/null 2>&1; then
		invoke-rc.d "$1" force-reload || :
	else
		[ -x /etc/init.d/"$1" ] && /etc/init.d/"$1" force-reload || :
	fi
}


if [ "$1" = configure -o "$1" = upgrade ]
then
	mkdir -p /var/lib/egroupware/default/files
	mkdir -p /var/lib/egroupware/default/backup

	config="/var/lib/egroupware/header.inc.php"
	template="/usr/share/egroupware/header.inc.php.template"

	install_log="/root/egroupware-epl-install.log"
	date >> $install_log

	# If upgrading, $2 contains the release currently installed, otherwise it's empty
	if [ "$2" ]
	then
		/usr/share/egroupware/doc/rpm-build/post_install.php 2>&1 | /usr/bin/tee -a $install_log
        # fix ownership of files created during update
        chown -R www-data /var/lib/egroupware
		chmod 700 /var/lib/egroupware/
	else
		apache_conf=apache.conf
		# Debian 9 has no more /etc/lsb-release and gives an error
		if test -f /etc/lsb-release && grep -q '^DISTRIB_ID="*Univention"*$' /etc/lsb-release
		then
			apache_conf=apache-univention.conf
			if [ -n "$DOCKER_HOST_NAME" -a -n "$DB_HOST" ]
			then
				# docker UCS setup, use appcenter join script
				:
			else
				# normal UCS setup
				/usr/share/egroupware/doc/rpm-build/post_install.php \
					--distro univention \
					--admin_user "Administrator" \
					--admin_passwd "*unchanged*" 2>&1 | /usr/bin/tee -a $install_log
			fi
		else
			# for non-interactive install let post_install generate passwords
			if [ "$DEBIAN_FRONTEND" = "noninteractive" ]
			then
				/usr/share/egroupware/doc/rpm-build/post_install.php 2>&1 | /usr/bin/tee -a $install_log
			else
				# Get configuration passwords
				db_get "egroupware/header/user"
				header_user="$RET"
				db_get "egroupware/header/password"
				header_password="$RET"

				# Get first egroupware user / pw
				db_get "egroupware/admin/user"
				admin_user="$RET"
				db_get "egroupware/admin/password"
				admin_password="$RET"

				# Get egroupware language
				db_get "egroupware/lang"
				lang="$RET"

				# Get mysql root / pw
				db_get "egroupware/db_root"
				db_root="$RET"
				db_get "egroupware/db_root_pw"
				db_root_pw="$RET"

				/usr/share/egroupware/doc/rpm-build/post_install.php \
					--config_user "$header_user" \
					--config_passwd "$header_password" \
					--admin_user "$admin_user" \
					--admin_passwd "$admin_password" \
					--lang "$lang" \
					--db_type mysql \
					--db_root "$db_root" \
					--db_root_pw "$db_root_pw" 2>&1 | /usr/bin/tee -a $install_log
			fi
		fi
	fi
	echo "EGroupware install log saved to $install_log"

	# Only change permissions during initial installation, in case
	# someone wants a different setup.
	if [ -z "$2" ]; then
		chown -R www-data:www-data /var/lib/egroupware/
		chmod 700 /var/lib/egroupware/
		chmod 600 $config
	fi

	# Set up web server and reload it.
	if [ -d /etc/nginx ]
	then
		ln -fs ../../egroupware/nginx.conf /etc/nginx/sites-available/egroupware.conf
		# initial install: enable egroupware and disable default site
		if [ -z "$2" ]
		then
			ln -fs /etc/nginx/sites-available/egroupware.conf /etc/nginx/sites-enabled/
			rm -f /etc/nginx/sites-enabled/default
		fi
		nginx -s reload
	else
		# use either apache.conf or apache-univention.conf for extra open_basedir
		apache_conf=apache.conf
		test -f /etc/lsb-release && grep -q '^DISTRIB_ID="*Univention"*$' /etc/lsb-release && apache_conf=apache-univention.conf

		if [ -d /etc/apache2/conf.d ] ; then
			ln -fs ../../egroupware/$apache_conf /etc/apache2/conf.d/egroupware
		else
			if [ -d /etc/apache2/sites-available ] ; then
				ln -fs ../../egroupware/$apache_conf /etc/apache2/sites-available/egroupware.conf
				a2ensite egroupware
			fi
		fi
		a2enmod actions >/dev/null
		webserver_soft_reload apache2
	fi
fi

#DEBHELPER#

db_stop
