#!/bin/bash

# Exit on command errors and treat unset variables as an error
set -eu

if [ ! -e .fonctions ]; then
	# Get file fonction if not been to the current directory
	sudo cp ../settings/scripts/.fonctions ./.fonctions
	sudo chmod a+rx .fonctions
fi
# Loads the generic functions usually used in the script
source .fonctions
# Source app helpers
source /usr/share/yunohost/helpers

# This is a multi-instance app, meaning it can be installed several times independently
# The id of the app as stated in the manifest is available as $YNH_APP_ID
# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...)
# The app instance name is available as $YNH_APP_INSTANCE_NAME
#    - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample
#    - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2
#    - ynhexample__{N} for the subsequent installations, with N=3,4, ...
# The app instance name is probably what you are interested the most, since this is
# guaranteed to be unique. This is a good unique identifier to define installation path,
# db names, ...
app=$YNH_APP_INSTANCE_NAME

# Retrieve app settings
domain=$(ynh_app_setting_get $app domain)
path=$(ynh_app_setting_get $app path)
admin=$(ynh_app_setting_get $app admin)
is_public=$(ynh_app_setting_get $app is_public)
multisite=$(ynh_app_setting_get $app multisite)

# Remove trailing slash to path
path=${path%/}
#force location to be / or /foo
location=${path:-/}

# admin default value, if not set
if [ -z "$admin" ];
then
	admin=$(sudo yunohost user list | grep 'username' -m1 | awk '{print $2}')
	sudo ynh_app_setting_set $app is_public -v "$is_public"
fi

# Create system user dedicace for this app
ynh_system_user_create $app

# Modify dokuwiki conf
sed -i "s@YNH_ADMIN_USER@$admin@g" ../conf/dokuwiki.php

# Copy files to the right place
final_path=/var/www/$app
sudo mkdir -p $final_path

# Get source
SETUP_SOURCE

sudo cp ../conf/dokuwiki.php $final_path/conf

# Do not override ACL configuration file
if [ ! -f "$final_path/conf/acl.auth.php" ]; then
	sudo cp ../conf/acl.auth.php $final_path/conf
fi

# Remove upgrade notification
# See https://www.dokuwiki.org/update_check
sudo touch $final_path/doku.php

# Remove deleted files
# See https://www.dokuwiki.org/install:unused_files
if [ -f "../sources/data/deleted.files" ]; then
	grep -Ev '^($|#)' ../sources/data/deleted.files | xargs -I {} sudo rm -vrf $final_path/{}
fi

# Change owner for all plugins
sudo chmod -R 755 $final_path/lib/plugins

# Update all plugins
for name_plugin in $(sudo -s cat $final_path/lib/plugins/*/plugin.info.txt | grep url | awk -F':' '{print $3}');
do
		# Get a official plugin for dokuwiki, not update a no-official
		sudo wget -nv --quiet "https://github.com/splitbrain/dokuwiki-plugin-${name_plugin}/zipball/master" -O "${name_plugin}.zip" -o /dev/null || true
		if [ -s "${name_plugin}.zip" ]; then
			sudo unzip ${name_plugin}.zip
			sudo cp -a splitbrain-dokuwiki-plugin-${name_plugin}*/. "${final_path}/lib/plugins/${name_plugin}/"
		fi
done

# Files owned by www-data can just read
sudo chown -R root: $final_path

# except for conf, data, some data subfolders, and lib/plugin, where dokuwiki must have write permissions
if [ -d "${final_path}/data/media" ]; then
	sudo chown -R $app:root $final_path/{data/attic,data/cache,data/index,data/locks,data/media*,data/meta,data/pages,data/tmp}
fi
sudo chown -R $app:root $final_path/{conf,data,lib/plugins,lib/tpl}
sudo chmod -R 700 $final_path/conf
sudo chmod -R 700 $final_path/data
sudo chmod -R 755 $final_path/lib/plugins
sudo chmod 755 $final_path/lib/tpl/{dokuwiki,dokuwiki/images}

# Modify Nginx configuration file and copy it to Nginx conf directory
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
sudo sed -i "s@fastcgi_pass unix:/var/run/php5-fpm.sock;@fastcgi_pass unix:/var/run/php5-fpm-${app}.sock;@g" /etc/nginx/conf.d/$domain.d/$app.conf

if [ "$is_public" = "Yes" ];
then
	sudo sed -i "s@#--PRIVATE--@@g" /etc/nginx/conf.d/$domain.d/$app.conf
fi

# Create the php-fpm pool config
POOL_FPM

# Set ssowat config
if [ "$is_public" = "Yes" ]; then
	ynh_app_setting_set $app is_public 1 # Fixe is_public en booléen
	is_public=1
else
	ynh_app_setting_set $app is_public 0
 	is_public=0
fi

sudo systemctl reload nginx
sudo yunohost app ssowatconf