#!/bin/bash

# Récupère les infos de l'application.
app=$YNH_APP_INSTANCE_NAME
domain=$(sudo yunohost app setting $app domain)
path=$(sudo yunohost app setting $app path)
admin_wordpress=$(sudo yunohost app setting $app admin)
language=$(sudo yunohost app setting $app language)
is_public=$(sudo yunohost app setting $app is_public)
multisite=$(sudo yunohost app setting $app multisite)
final_path=$(sudo yunohost app setting $app final_path)

root_pwd=$(sudo cat /etc/yunohost/mysql)
db_name=$app
if [[ "$admin_wordpress" = "" ]];
then
    mysql -u root -p$root_pwd $db_name -e "select MAX(user_login) from wp_users where user_status=0 INTO OUTFILE '/tmp/wordpressuser';"
    admin_wordpress=$(cat /tmp/wordpressuser)
    sudo rm -f /tmp/wordpressuser
	sudo yunohost app setting $app admin -v $admin_wordpress
fi

if [[ "$final_path" = "" ]];
then
    final_path=/var/www/$app
	sudo yunohost app setting $app final_path -v $final_path
fi

if [[ "$language" = "" ]];
then
    language=$(sudo grep WPLANG $final_path/wp-config.php | cut -d"'" -f4)
	sudo yunohost app setting $app language -v $language
fi

# Check if admin is not null
if [[ "$admin_wordpress" = "" || "$is_public" = "" || "$language" = "" ]]; then
    echo "Unable to upgrade, please contact support"
    exit 1
fi

# Copie le fichier de config nginx
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
# Modifie les variables dans le fichier de configuration nginx
sudo sed -i "s@__PATHTOCHANGE__@$path@g" /etc/nginx/conf.d/$domain.d/$app.conf
sudo sed -i "s@__FINALPATH__@$final_path@g" /etc/nginx/conf.d/$domain.d/$app.conf
sudo sed -i "s@__NAMETOCHANGE__@$app@g" /etc/nginx/conf.d/$domain.d/$app.conf

# Copie le fichier de configuration du pool php-fpm et le configure.
sed -i "s@__NAMETOCHANGE__@$app@g" ../conf/php-fpm.conf
sed -i "s@__FINALPATH__@$final_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
finalphpini=/etc/php5/fpm/conf.d/20-$app.ini
sudo cp ../conf/php-fpm.ini $finalphpini
sudo chown root: $finalphpini
sudo service php5-fpm reload


config_file="$final_path/wp-config.php"
if [ "$(sudo yunohost app setting $app wp-config.php_file_md5)" != $(md5sum "$config_file" | cut -d' ' -f1) ]; then
	sudo cp -a "$config_file" "$config_file.backup.$(date '+%d.%m.%y_%Hh%M,%Ss')"	# Si le fichier de config a été modifié, créer un backup.
fi
if [ "$multisite" = "Yes" ];
then
    sudo sed -i "s@#--MULTISITE--@@g" /etc/nginx/conf.d/$domain.d/$app.conf
else
	multisite="No"
	if [ "$is_public" = "No" ];
	then
		sudo sed -i "s@#--PRIVATE--@@g" /etc/nginx/conf.d/$domain.d/$app.conf
	else
		sudo sed -i "s@//--PUBLIC--define@define@g" $final_path/wp-config.php
	fi
fi
sudo yunohost app setting $app multisite -v $multisite
# Réenregistre la somme de contrôle du fichier de config.
sudo yunohost app setting $app wp-config.php_file_md5 -v $(md5sum "$final_path/wp-config.php" | cut -d' ' -f1)

# Configure les droits d'accès au fichiers
# Les fichiers appartiennent à www-data, pour permettre les mises à jour.
sudo chown -R www-data: $final_path
# Sauf le fichier de config wp-config.php qui appartient à root
sudo chown root: $final_path/wp-config.php


sudo yunohost app setting $app skipped_uris -d	# Retire le skipped_uris si il existe encore.
if [ "$is_public" = "No" ]; then	# Retire l'accès public
	sudo yunohost app setting $app unprotected_uris -d
else	# Ou remplace le skipped_uris par unprotected_uris le cas échéant.
	sudo yunohost app setting $app unprotected_uris -v "/"
fi
sudo yunohost app ssowatconf


# Recharge la configuration Nginx
sudo service nginx reload
# Régénère la configuration de SSOwat
sudo yunohost app ssowatconf