#!/bin/bash
set -e
app=rainloop

# Retrieve arguments
	domain=$1
	path=$2
	is_public=$3
	password=$4
	ldap=$5

# Removal of trailing /
	if [ $path = "/" ]
	then
		echo "Installation on the root of the domain"
	else
		path=${path%/}
	fi

# Check domain/path availability
	sudo yunohost app checkurl $domain$path -a rainloop
	if [[ ! $? -eq 0 ]]; then
		exit 1
	fi

# Generate random password
	db_pwd=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d 'A-Za-z0-9' | sed -n 's/\(.\{24\}\).*/\1/p')

# Use 'rainloop' as database name and user
	db_user=$app

# Initialize database and store mysql password for upgrade
	sudo yunohost app initdb $db_user -p $db_pwd
	sudo yunohost app setting $app mysqlpwd -v $db_pwd

# Create the final path and copy sources
	final_path=/var/www/$app
	rainloop_path=${final_path}/app
	#rainloop_path=${final_path}

	sudo rm -rf $final_path
	sudo mkdir -p $final_path
	sudo mkdir -p $rainloop_path

	# Use of latest community edition
	sudo unzip -q ../sources/rainloop-community.zip -d $rainloop_path/
	
	# Install plugins
	sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
	sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
	sudo cp -rf ../sources/plugins/ynh-ldap-suggestions $rainloop_path/data/_data_/_default_/plugins/.

# Autoconfig
	sudo mkdir -p $rainloop_path/data/_data_/_default_/configs/
	application_file=$rainloop_path/data/_data_/_default_/configs/application.ini

	# Set lang
	if [ $(echo $LANG | cut -c1-2) == "fr" ]
	then
		lang=fr
	else
		lang=en
	fi
	
	# Set plugins
	plugins="ynh-login-mapping"
	if [ "$ldap" = "Yes" ];
	then
		plugins="$plugins,ynh-ldap-suggestions"
	fi
	sudo yunohost app setting $app plugins -v $plugins
	
	sudo cp ../conf/data/configs/application.ini $application_file
	sudo sed -i "s@domain.tld@$domain@g"         $application_file
	sudo sed -i "s@MYSQLUSER@$db_user@g"         $application_file
	sudo sed -i "s@MYSQLPASSWORD@$db_pwd@g"      $application_file
	sudo sed -i "s@LANGTOCHANGE@$lang@g"         $application_file
	sudo sed -i "s@PLUGINSTOENABLE@$plugins@g"   $application_file

# Set admin password
	sudo php ../conf/config.php --index="$rainloop_path/index.php" --password="$password"

# Add default domain configs by looping through all the domains already added
	sudo mkdir -p $rainloop_path/data/_data_/_default_/domains/

	# get list of ldap domains
	alldomains=`ldapsearch -LLL -x -b ou=domains,dc=yunohost,dc=org -s one "objectclass=top" virtualdomain | grep -v "dn:" | sed "s/virtualdomain://" `
	for domain in $alldomains ; do
		sudo cp ../conf/data/domains/domain.tld.ini $rainloop_path/data/_data_/_default_/domains/$domain.ini
	done
	sudo cp ../conf/data/domains/disabled $rainloop_path/data/_data_/_default_/domains/disabled

# Hooks for domains are not implemented yet, so new domains will not be added automatically

# install SSO and auto version - at the moment the index is the SSO and rainloop is installed in /app
	if [ $final_path == $rainloop_path ]
	then
		# use modified version of master index.php that implement sso
		sudo cp ../sources/sso/index.php      $final_path/index.php
	else
		# use only sso on master
		sudo cp ../sources/sso/sso.php                  $final_path/index.php
		sudo cp ../sources/patch/index_auto_version.php $rainloop_path/index.php
	fi
	sudo sed -i "s@domain.tld@$domain@g"  $final_path/index.php
	sudo sed -i "s@PATHTOCHANGE@$path@g"  $final_path/index.php

# Set permissions to rainloop directory
	sudo chown -R www-data:www-data $final_path

# Install Nginx configuration file
	nginx_conf_file=/etc/nginx/conf.d/$domain.d/$app.conf
	sudo cp ../conf/nginx.conf $nginx_conf_file
	sudo sed -i "s@PATHTOCHANGE@$path@g"         $nginx_conf_file
	sudo sed -i "s@ALIASTOCHANGE@$final_path/@g" $nginx_conf_file
	sudo sed -i "s@NAMETOCHANGE@$app@g"          $nginx_conf_file
	sudo chown root:                             $nginx_conf_file
	sudo chmod 644                               $nginx_conf_file

	finalphpconf=/etc/php5/fpm/pool.d/$app.conf
	sudo cp ../conf/php-fpm.conf        $finalphpconf
	sudo sed -i "s@NAMETOCHANGE@$app@g" $finalphpconf
	sudo chown root:                    $finalphpconf
	sudo chmod 644                      $finalphpconf

# Make app public if necessary
	sudo yunohost app setting $app is_public -v "$is_public"
	if [ "$is_public" = "Yes" ];
	then
		sudo yunohost app setting $app skipped_uris -v "/"
	fi

# Reload Nginx and regenerate SSOwat conf
	sudo service php5-fpm reload
	sudo service nginx reload
	sudo yunohost app ssowatconf