#!/bin/bash

set -eu
app=$YNH_APP_INSTANCE_NAME
version=$(cat ../sources/version)

## Source YunoHost helpers - old
#	source /usr/share/yunohost/helpers
#	source ./_extrahelpers

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source _common.sh
source /usr/share/yunohost/helpers

#=================================================
# MANAGE SCRIPT FAILURE
#=================================================

# Exit if an error occurs during the execution of the script
ynh_abort_if_errors


################

# Retrieve arguments
	domain=$YNH_APP_ARG_DOMAIN
	path=$YNH_APP_ARG_PATH
	admin=$YNH_APP_ARG_ADMIN
	member=$YNH_APP_ARG_MEMBER

# Correct path: puts a / at the start and nothing at the end
	if [ "${path:0:1}" != "/" ]; then
		path="/$path"
	fi
	if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
		path="${path:0:${#path}-1}"
	fi

# Check domain/path availability
#	sudo yunohost app checkurl "${domain}${path}" -a "$app" \
#		|| ynh_die "path not available: ${domain}${path}"

	sudo ynh_webpath_register $app $domain $path \
		|| ynh_die "path not available and may be already used : ${domain}${path}"

# Check user parameter
	ynh_user_exists "$admin" \
	  || ynh_die "The chosen admin user does not exist."

# Store settings
	ynh_app_setting_set $app domain $domain
	ynh_app_setting_set $app admin $admin
	ynh_app_setting_set $app member $member
	ynh_app_setting_set $app version $version

# Copy source files
	src_path=/var/www/$app
	sudo mkdir -p $src_path

	# Download, unzip and copy source
	sudo wget -q https://github.com/Dolibarr/dolibarr/archive/${version}.zip -O dolibarr-${version}.zip
	sudo unzip -qq dolibarr-${version}.zip

	sudo cp -a dolibarr-${version}/. $src_path

	# Create necessary files
	sudo touch $src_path/htdocs/conf/conf.php
	sudo mkdir -p $src_path/documents

# MySQL
	dbuser=$app
	dbname=$app
	dbpass=$(ynh_string_random 12)
	ynh_app_setting_set "$app" mysqlpwd "$dbpass"
	ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"

# Modify Nginx configuration file and copy it to Nginx conf directory
	nginx_conf=../conf/nginx.conf
	sed -i "s@YNH_WWW_PATH@${path%/}@g" $nginx_conf
	sed -i "s@YNH_WWW_ALIAS@$src_path/htdocs/@g" $nginx_conf
	sed -i "s@YNH_WWW_APP@$app@g" $nginx_conf
	sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf

# PHP
	sed -i "s@YNH_WWW_APP@$app@g" ../conf/php-fpm.conf
	sed -i "s@YNH_WWW_ALIAS@$src_path@g" ../conf/php-fpm.conf
	finalphpconf=/etc/php5/fpm/pool.d/$app.conf
	sudo cp ../conf/php-fpm.conf $finalphpconf
	sudo chown root: $finalphpconf
	sudo chmod 644 $finalphpconf
	sudo service php5-fpm reload

# Install parameters
	sed -i "s@YNH_WWW_ALIAS@$src_path@g" ../sources/install.forced.php
	sed -i "s@YNH_DBNAME@$dbname@g" ../sources/install.forced.php
	sed -i "s@YNH_DBUSER@$dbuser@g" ../sources/install.forced.php
	sed -i "s@YNH_DBPASS@$dbpass@g" ../sources/install.forced.php
	sed -i "s@YNH_ADMIN@$admin@g" ../sources/install.forced.php
	if [ $member = 1 ];
	then
		# If YNH users are members, we must activate the members module
		sed -i "s@modLdap@modLdap,modAdherent@g" ../sources/install.forced.php
	fi
	forcedinstall=$src_path/htdocs/install/install.forced.php
	sudo cp ../sources/install.forced.php $forcedinstall

# Set permissions to app files
	sudo chmod -R 755 $src_path
	sudo chown -R www-data: $src_path

# Reload Nginx
	sudo service nginx reload

# Install
	# Disable SSO
	ynh_app_setting_set "$app" unprotected_uris "/"
	sudo yunohost app ssowatconf

	# Generate a random password for the admin user (will be ignored because of LDAP)
	password=$(ynh_string_random 8)

	# Install with CURL
	ynh_local_curl "/install/fileconf.php"

	ynh_local_curl "/install/step1.php" \
		"testpost=ok" \
		"action=set" \
		"selectlang=fr_FR"

	ynh_local_curl "/install/step2.php" \
		"testpost=ok" \
		"action=set" \
		"dolibarr_main_db_character_set=latin1" \
		"dolibarr_main_db_collation=latin1_swedish_ci" \
		"selectlang=fr_FR"

	ynh_local_curl "/install/step4.php" \
		"testpost=ok" \
		"action=set" \
		"selectlang=fr_FR"

	ynh_local_curl "/install/step5.php" \
		"testpost=ok" \
		"action=set" \
		"selectlang=fr_FR" \
		"pass=$password" \
		"pass_verif=$password"

	# Populate the LDAP parameters
	mysql -u ${dbuser} -p${dbpass} ${dbname} < ../conf/ldap.sql

	# Populate the database with YNH users.
	mysql -u ${dbuser} -p${dbpass} ${dbname} < ../conf/ldap_user.sql
	sudo sudo -u www-data php $src_path/scripts/user/sync_users_ldap2dolibarr.php commitiferror --server=localhost -y

	# If YNH users should be members, populate the database accordingly, create the member list, and sync members
	if [ $member = 1 ];
	then
		mysql -u ${dbuser} -p${dbpass} ${dbname} < ../conf/ldap_member.sql
		sudo sudo -u www-data php $src_path/scripts/members/sync_members_ldap2dolibarr.php commitiferror 1 --server=localhost -y
	fi

	# Re-enable SSO if chosen by the user
	ynh_app_setting_delete $app unprotected_uris
	sudo yunohost app ssowatconf

# Setup HTTP auth in conf
	sudo sed -i "s@\$dolibarr_main_authentication='dolibarr';@\$dolibarr_main_authentication='http';@g" $src_path/htdocs/conf/conf.php

# Setup hooks
	sed -i "s@YNH_APP@$app@g" ../hooks/post_user_create
	sed -i "s@YNH_MEMBER@$member@g" ../hooks/post_user_create