#!/bin/bash

#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

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

#=================================================
# MANAGE FAILURE OF THE SCRIPT
#=================================================

ynh_abort_if_errors	# Active trap pour arrêter le script si une erreur est détectée.

#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================

domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
admin_wordpress=$YNH_APP_ARG_ADMIN
language=$YNH_APP_ARG_LANGUAGE
multisite=$YNH_APP_ARG_MULTISITE
is_public=$YNH_APP_ARG_IS_PUBLIC

app=$YNH_APP_INSTANCE_NAME

#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
#=================================================

path_url=$(ynh_normalize_url_path $path_url)	# Vérifie et corrige la syntaxe du path.
CHECK_DOMAINPATH	# Vérifie la disponibilité du path et du domaine.
CHECK_FINALPATH	# Vérifie que le dossier de destination n'est pas déjà utilisé.

if [ "$path_url" == "/" ] && [ $multisite -eq 1 ]; then
	ynh_die "Multisite option of wordpress doesn't work at root of domain."
fi

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================

ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app path $path_url
ynh_app_setting_set $app admin $admin_wordpress
ynh_app_setting_set $app is_public $is_public
ynh_app_setting_set $app language $language
ynh_app_setting_set $app multisite $multisite

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================

ynh_install_app_dependencies php5-cli

#=================================================
# CREATE A SQL BDD
#=================================================

db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set $app db_name $db_name
ynh_mysql_setup_db $db_name $db_name

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

ynh_app_setting_set $app final_path $final_path
ynh_setup_source "$final_path"	# Télécharge la source, décompresse et copie dans $final_path

#=================================================
# NGINX CONFIGURATION
#=================================================

ynh_nginx_config

#=================================================
# CREATE DEDICATED USER
#=================================================

ynh_system_user_create $app	# Créer un utilisateur système dédié à l'app

#=================================================
# PHP-FPM CONFIGURATION
#=================================================

ynh_fpm_config	# Créer le fichier de configuration du pool php-fpm et le configure.

#=================================================
# SPECIFIC SETUP
#=================================================
# CONFIGURE WP-CONFIG
#=================================================

sudo cp ../conf/wp-config.php $final_path/wp-config.php
# Change variables in Wordpress configuration
ynh_replace_string "__DB_USER__" "$db_name" $final_path/wp-config.php
ynh_replace_string "__DB_PWD__" "$db_pwd" $final_path/wp-config.php
ynh_replace_string "__DOMAIN__" "$domain" $final_path/wp-config.php
ynh_replace_string "__PATH__" "$path_url" $final_path/wp-config.php

for i in 1 2 3 4 5 6 7 8
do
	j=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{40\}\).*/\1/p')
	if [ "$j" = "" ];
	then
		# For obscure reasons, the loop is too fast at execution
		sleep 1
		j=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{40\}\).*/\1/p')
	fi
	ynh_replace_string "KEY$i" "$j" $final_path/wp-config.php
done

#=================================================
# SETTING UP WITH CURL
#=================================================

# Set right permissions for curl install
sudo chown -R $app: $final_path

# Rend la page d'install publique pour curl
ynh_app_setting_set $app unprotected_uris "/"
sudo yunohost app ssowatconf	# Régénère la configuration de SSOwat

# Reload Nginx
sudo systemctl reload nginx

# Wordpress installation
ynh_local_curl "/wp-admin/install.php?step=2" "&weblog_title=YunoBlog" "user_name=$admin_wordpress" "admin_password=$db_pwd" "admin_password2=$db_pwd" "admin_email=$admin_wordpress@$domain" "language=$language" "Submit=Install+WordPress"

WARNING echo -n "Please wait during Wordpress installation"
for i in `seq 1 300`
do	# La boucle attend la fin de l'installation de wordpress Ou 5 minutes.
	if ynh_mysql_connect_as $db_name $db_pwd $db_name <<< "show tables" | grep -q "wp_options"; then
		break	# Si la table wp_options est trouvée, l'installation de wordpress est terminée. Quitte la boucle.
	fi
	WARNING echo -n "."
	sleep 1
done

#=================================================
# LOAD SQL CONFIG
#=================================================

# Replace variables in sql scripts
ynh_replace_string "__DOMAIN_PATH__" "$domain$path_url" ../conf/sql/*.sql
ynh_replace_string "__LANGUAGE__" "$language" ../conf/sql/*.sql
ynh_replace_string "__DATE__" "$(date +%s)" ../conf/sql/*.sql

# Charge les commandes sql communes à tous les scripts.
# mysql --debug-check -u $db_user -p$db_pwd $db_user < ../conf/sql/common.sql
ynh_mysql_connect_as $db_name $db_pwd $db_name < ../conf/sql/common.sql

#=================================================
# CONFIGURE MULTISITE
#=================================================

if [ $multisite -eq 1 ]
then
	ynh_replace_string "#--MULTISITE--" "" /etc/nginx/conf.d/$domain.d/$app.conf
	# Autorise le multisite wordpress
	ynh_replace_string "//--MULTISITE1--define" "define " $final_path/wp-config.php

	# Active le multisite via wp-cli.
	sudo wget -nv https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O $final_path/wp-cli.phar
	ALL_QUIET php $final_path/wp-cli.phar core multisite-convert --allow-root --path=$final_path --base=$path_url/

	# Active le multisite wordpress
	ynh_replace_string "//--MULTISITE2--define" "define" $final_path/wp-config.php

	# Charge les commandes sql pour activer les plugins
	if [ $is_public -eq 0 ]
	then
		ynh_replace_string "#--PRIVATE--" "" ../conf/sql/multisite.sql
	else
		ynh_replace_string "#--PUBLIC--" "" ../conf/sql/multisite.sql
	fi
	ynh_mysql_connect_as $db_name $db_pwd $db_name < ../conf/sql/multisite.sql
else
	if [ $is_public -eq 0 ]
	then
		ynh_replace_string "#--PRIVATE--" "" /etc/nginx/conf.d/$domain.d/$app.conf
		ynh_replace_string "#--PRIVATE--" "" ../conf/sql/single.sql
	else
		ynh_replace_string "//--PUBLIC--define" "define" $final_path/wp-config.php
		ynh_replace_string "#--PRIVATE--" "#" /etc/nginx/conf.d/$domain.d/$app.conf
		ynh_replace_string "#--PUBLIC--" "" ../conf/sql/single.sql
	fi
	# Charge les commandes sql pour activer les plugins
	ynh_mysql_connect_as $db_name $db_pwd $db_name < ../conf/sql/single.sql
fi

# Décommente les add_filter, qui auraient provoqué une erreur avec wp-cli
ynh_replace_string "//add_filter" "add_filter" $final_path/wp-config.php

#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================

ynh_store_file_checksum "$final_path/wp-config.php"	# Enregistre la somme de contrôle du fichier de config

#=================================================
# GENERIC FINALISATION
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================

# Les fichiers appartiennent à l'user wordpress, pour permettre les mises à jour.
sudo chown -R $app: $final_path
# Sauf le fichier de config wp-config.php qui appartient à root
sudo chown root: $final_path/wp-config.php

#=================================================
# SETUP SSOWAT
#=================================================

if [ $is_public -eq 0 ];
then
	# Retire l'accès public
	ynh_app_setting_delete $app unprotected_uris
fi

#=================================================
# RELOAD NGINX
#=================================================

sudo systemctl reload nginx

#=================================================
# REMOVE WP-CLI.PHAR
#=================================================

# wp-cli me semble un peu trop permissif... Il a terminé son travail...
ynh_secure_remove $final_path/wp-cli.phar