#!/bin/bash
# info: Install Roundcube webmail client
# options: [MODE]
#
# This function installs the Roundcube webmail client.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
source $HESTIA/func/db.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
# upgrade config file
source "$HESTIA/install/upgrade/upgrade.conf"

MODE=$2
UPDATE="no"
# Version and Download paths
RC_FILE="roundcubemail-$rc_v-complete.tar.gz"
RC_EXTRACT="roundcubemail-$rc_v"
# Downloading full version
RC_URL="https://github.com/roundcube/roundcubemail/releases/download/$rc_v/roundcubemail-$rc_v-complete.tar.gz"

# Folder paths
RC_INSTALL_DIR="/var/lib/roundcube"
RC_CONFIG_DIR="/etc/roundcube"
RC_LOG="/var/log/roundcube"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

# Checking root permissions
if [ "x$(id -u)" != 'x0' ]; then
	echo "ERROR: v-add-sys-roundcube can be run executed only by root user"
	exit 10
fi

# Ensure that $HESTIA (/usr/local/hestia/) and other variables are valid.
if [ -z "$HESTIA" ]; then
	HESTIA="/usr/local/hestia"
fi

if [ -z "$HOMEDIR" ] || [ -z "$HESTIA_COMMON_DIR" ]; then
	echo "ERROR: Environment variables not present, installation aborted."
	exit 2
fi

if [ -z "$(echo "$DB_SYSTEM" | grep -w 'mysql')" ]; then
	echo "ERROR: Mysql not available. Installation aborted"
	exit 2
fi

if [ -d "/usr/share/roundcube" ]; then
	echo "ERROR: Install done from apt source, unable to continue"
	exit 2
fi

# Get current version
if [ -f "/var/lib/roundcube/index.php" ]; then
	version=$(cat $RC_INSTALL_DIR/index.php | grep -o -E '[0-9].[0-9].[0-9]+' | head -1)
	if [ "$version" == "$rc_v" ]; then
		echo "Error: Installed version ($version) is equal to the available version ($rc_v)"
		exit 2
	else
		UPDATE="yes"
	fi
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

if [ "$UPDATE" == "no" ]; then
	rm -f -r $RC_INSTALL_DIR
	rm -f -r $RC_CONFIG_DIR

	mkdir -p $RC_INSTALL_DIR/
	mkdir -p $RC_CONFIG_DIR/

	cd "$RC_INSTALL_DIR"
	[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$RC_URL" --retry-connrefused --quiet -O "${RC_INSTALL_DIR}/${RC_FILE}"

	tar xzf $RC_FILE
	cp -rT $RC_EXTRACT $RC_INSTALL_DIR

	# Delete old config folder
	cp $RC_INSTALL_DIR/config/defaults.inc.php $RC_CONFIG_DIR/defaults.inc.php
	rm -f -r $RC_INSTALL_DIR/config/
	ln -s $RC_CONFIG_DIR/ ./config
	# Replace with Hestia config
	cp -f $HESTIA_COMMON_DIR/roundcube/main.inc.php $RC_CONFIG_DIR/config.inc.php
	cp -f $HESTIA_COMMON_DIR/roundcube/mimetypes.php $RC_CONFIG_DIR/mimetypes.php
	chmod 644 $RC_CONFIG_DIR/*.php

	cp -f $HESTIA_COMMON_DIR/roundcube/hestia.php $RC_INSTALL_DIR/plugins/password/drivers/
	mkdir -p $RC_CONFIG_DIR/plugins/password
	mkdir -p $RC_CONFIG_DIR/plugins/newmail_notifier
	mkdir -p $RC_CONFIG_DIR/plugins/zipdownload

	# Allow changes to the respective config / Create symlinks to /etc/roundcube/
	cp -f $HESTIA_COMMON_DIR/roundcube/config.inc.php $RC_CONFIG_DIR/plugins/password/config.inc.php
	ln -s $RC_CONFIG_DIR/plugins/password/config.inc.php ./plugins/password/config.inc.php
	cp -f $HESTIA_COMMON_DIR/roundcube/plugins/config_newmail_notifier.inc.php $RC_CONFIG_DIR/plugins/newmail_notifier/config.inc.php
	ln -s $RC_CONFIG_DIR/plugins/newmail_notifier/config.inc.php ./plugins/newmail_notifier/config.inc.php
	cp -f $HESTIA_COMMON_DIR/roundcube/plugins/config_zipdownload.inc.php $RC_CONFIG_DIR/plugins/zipdownload/config.inc.php
	ln -s $RC_CONFIG_DIR/plugins/zipdownload/config.inc.php ./plugins/zipdownload/config.inc.php

	# Set up correct permissions roundcube
	chown -R hestiamail:www-data $RC_CONFIG_DIR/
	chmod 751 -R $RC_CONFIG_DIR
	chmod 640 $RC_CONFIG_DIR/config.inc.php
	chmod 644 $RC_CONFIG_DIR/plugins/password/config.inc.php
	chmod 644 $RC_CONFIG_DIR/plugins/newmail_notifier/config.inc.php
	chmod 644 $RC_CONFIG_DIR/plugins/zipdownload/config.inc.php

	# Add robots.txt
	echo "User-agent: *" > /var/lib/roundcube/robots.txt
	echo "Disallow: /" >> /var/lib/roundcube/robots.txt

	chown -R hestiamail:www-data $RC_INSTALL_DIR

	# Log file
	if [ ! -d $RC_LOG ]; then
		mkdir $RC_LOG
	fi
	chown hestiamail:www-data $RC_LOG
	chmod 751 $RC_LOG

	if [ ! -z "$(echo "$DB_SYSTEM" | grep -E 'mysql|pgsql')" ]; then
		host='localhost'
		database='roundcube'
		dbuser="$database"
		dbpass=$(generate_password)
		charset='UTF8'
		sed -i "s/%password%/$dbpass/g" $RC_CONFIG_DIR/config.inc.php

		if [ ! -z "$(echo "$DB_SYSTEM" | grep -w 'mysql')" ]; then
			add_mysql_database
			mysql_query "USE $database; $(< /var/lib/roundcube/SQL/mysql.initial.sql)"
		else
			add_pgsql_database
			psql_query "USE $database; $(< /var/lib/roundcube/SQL/postgres.initial.sql)"
		fi
	fi

	# TODO: Add support for PostgreSQL

	rcDesKey="$(openssl rand -base64 30 | tr -d "/" | cut -c1-24)"
	sed -i "s/%des_key%/$rcDesKey/g" $RC_CONFIG_DIR/config.inc.php
	# Update server hostname in password change plugin
	sed -i "s/localhost/$(hostname)/g" $RC_CONFIG_DIR/plugins/password/config.inc.php

	# Clean up
	rm -f -r $RC_INSTALL_DIR/installer
	rm -f -r $RC_INSTALL_DIR/$RC_FILE
	rm -f -r $RC_INSTALL_DIR/$RC_EXTRACT

	# Updating hestia.conf
	if [ -z "$(grep WEBMAIL_SYSTEM $HESTIA/conf/hestia.conf)" ]; then
		$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' 'roundcube'
	else
		if [ -z "$(echo "$WEBMAIL_SYSTEM" | grep -w 'roundcube')" ]; then
			if [ ! -z "$WEBMAIL_SYSTEM" ]; then
				$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "roundcube,$WEBMAIL_SYSTEM"
			else
				$BIN/v-change-sys-config-value 'WEBMAIL_SYSTEM' "roundcube"
			fi
		fi
	fi

	phpenmod mcrypt > /dev/null 2>&1
else
	cd "$RC_INSTALL_DIR"
	[ ! -f "${RC_INSTALL_DIR}/${RC_FILE}" ] && wget "$RC_URL" --quiet -O "${RC_INSTALL_DIR}/${RC_FILE}"

	tar xzf $RC_FILE

	# Run Roundcube upgrade script
	$RC_INSTALL_DIR/$RC_EXTRACT/bin/installto.sh -y $RC_INSTALL_DIR > /dev/null 2>&1
	# Use COMPOSER_ALLOW_SUPERUSER=1 to prevent update.sh script from freezing trying
	# to execute composer as root to update roundcube dependencies
	export COMPOSER_ALLOW_SUPERUSER=1
	$RC_INSTALL_DIR/bin/update.sh --version "$version" > /dev/null 2>&1
	$RC_INSTALL_DIR/bin/indexcontacts.sh > /dev/null 2>&1
	chown -R hestiamail:www-data $RC_INSTALL_DIR

	#clean up the mess
	if [ -d "$RC_INSTALL_DIR/installer" ]; then
		rm -f -r $RC_INSTALL_DIR/installer
	fi
	rm -f -r $RC_INSTALL_DIR/$RC_FILE
	rm -f -r $RC_INSTALL_DIR/$RC_EXTRACT
fi

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

if [ "$UPDATE" = "yes" ]; then
	$BIN/v-log-action "system" "Info" "Plugins" "Roundcube updated (Version: $version)."
else
	$BIN/v-log-action "system" "Info" "Plugins" "Roundcube enabled (Version: $version)."
fi
log_event "$OK" "$ARGUMENTS"
