#!/bin/bash
	app=$YNH_APP_INSTANCE_NAME
	rainloop_version='1.10.5.192'

# Backup the current version of the app, restore it if the upgrade fails
# Check if old backup exists
        if sudo yunohost backup list | grep -q $app-before-upgrade > /dev/null 2>&1;
        then
                sudo yunohost backup delete $app-before-upgrade
        else
                echo "no old backup to delete"
        fi
	sudo yunohost backup create --ignore-hooks --apps $app --name $app-before-upgrade --quiet
	EXIT_PROPERLY () {
		trap '' ERR
		set +eu
		sudo yunohost backup restore --ignore-hooks $app-before-upgrade --apps $app --force --quiet	# Restore the backup if upgrade failed
		ynh_die "Upgrade failed. The app was restored to the way it was before the failed upgrade."
	}
	set -eu
	trap EXIT_PROPERLY ERR

# Source app helpers
	source /usr/share/yunohost/helpers

# Retrieve arguments
	domain=$(ynh_app_setting_get "$app" domain)
	path=$(ynh_app_setting_get "$app" path)
	is_public=$(ynh_app_setting_get "$app" is_public)
	password=$(ynh_app_setting_get "$app" password)
	ldap=$(ynh_app_setting_get "$app" ldap)
	lang=$(ynh_app_setting_get "$app" lang)
	dp_pwd=$(ynh_app_setting_get "$app" mysqlpwd)
	db_user=$app
	plugins=$(ynh_app_setting_get "$app" plugins)

# Correct path
	if [ "${path:0:1}" != "/" ]; then
		path="/$path"
	fi
	if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then
		path="${path:0:${#path}-1}"
	fi

# no update for db now...

# Copy the new sources
	final_path=/var/www/$app
	rainloop_path=${final_path}/app
	sudo rm -rf $rainloop_path/rainloop		# Remove the previous Rainloop files except data

	# Download sources and keys
	sudo wget -q https://github.com/RainLoop/rainloop-webmail/releases/download/v${rainloop_version}/rainloop-community-${rainloop_version}.zip
	sudo wget -q https://github.com/RainLoop/rainloop-webmail/releases/download/v${rainloop_version}/rainloop-community-${rainloop_version}.zip.asc
	sudo wget -q https://repository.rainloop.net/RainLoop.asc
	# Verify the integrity of sources
	sudo gpg --import --quiet RainLoop.asc
	sudo gpg --verify --quiet rainloop-community-${rainloop_version}.zip.asc rainloop-community-${rainloop_version}.zip
	sudo gpg --batch --delete-key --yes Rainloop
	# Unzip and overwrite
	sudo unzip -qq -o rainloop-community-${rainloop_version}.zip -d $rainloop_path/

	# Update ynh plugins:
	sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
	sudo cp -rf ../sources/plugins/auto-domain-grab $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/.

# update SSO
	sudo cp ../sources/sso/sso.php        $final_path/index.php
	sudo sed -i "s@domain.tld@$domain@g"  $final_path/index.php
	sudo sed -i "s@PATHTOCHANGE@$path@g"  $final_path/index.php

# Install PGPback by chtixof to allow users to backup/restore their PGP private keys on the server
	sudo cp -rf ../sources/pgpback        	  $final_path/.

# Set permissions to rainloop directory
	sudo find $final_path/. -type d -exec chmod 755 {} \;
	sudo find $final_path/. -type f -exec chmod 644 {} \;
	sudo chown -R www-data:www-data $final_path

# Update Nginx configuration file
	nginx_conf_file=/etc/nginx/conf.d/$domain.d/$app.conf
	sudo cp ../conf/nginx.conf $nginx_conf_file
        if [ $path = "/" ]; then
            sudo sed -i "s@ROOTTOCHANGE@@g"          $nginx_conf_file
        else
            sudo sed -i "s@ROOTTOCHANGE@$path@g"     $nginx_conf_file
        fi
        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

# Reload services
	sudo service php5-fpm reload
	sudo service nginx reload