#!/bin/bash

#=================================================
# 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 FROM THE MANIFEST
#=================================================

domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
admin=$YNH_APP_ARG_ADMIN
password=$YNH_APP_ARG_PASSWORD

app=$YNH_APP_INSTANCE_NAME

#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..."

final_path=/var/www/$app
datadir="/home/yunohost.app/$app"
path_url=$(ynh_normalize_url_path --path_url="$path_url")
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"

# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url

## check that admin user is an existing account
ynh_user_exists --username="$admin"

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_script_progression --message="Storing installation settings..."

ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=admin_user --value=$admin
ynh_app_setting_set --app=$app --key=final_path --value="$final_path"
ynh_app_setting_set --app=$app --key=datadir --value="$datadir"

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=3

ynh_install_app_dependencies $pkg_dependencies

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..."

# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=3

## download source 
tmpdir=$(mktemp -d)
ynh_setup_source --dest_dir="$tmpdir"
## clean & copy files needed to final folder 
myynh_clean_source
mv "$tmpdir" "$final_path"

chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring PHP-FPM..."

# Create a dedicated PHP-FPM config
ynh_add_fpm_config

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..."

if [ "$path_url" != "/" ]; then
	ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="../conf/nginx.conf"
fi
ynh_add_nginx_config

#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..."

datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir

mkdir -p $datadir

chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:www-data "$datadir"

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..."

## create private & data folders
myynh_create_dir "$final_path/private"
myynh_create_dir "$datadir/uploads"
myynh_create_dir "$datadir/thumbs"
ln -s "$datadir/uploads" "$final_path/uploads"
ln -s "$datadir/thumbs" "$final_path/thumbs"
## set permissions
myynh_set_permissions

#=================================================
# SETUP APPLICATION WITH CURL
#=================================================
ynh_script_progression --message="Configuring admin user in BoZon..." --weight=2

# Set the app as temporarily public for curl call
ynh_script_progression --message="Configuring SSOwat..."
# Making the app public for curl
ynh_permission_update --permission="main" --add="visitors"

## fill the superadmin creation form (helper ynh_local_curl doesn't work due to --data vs --data-urlencode ?)
admin_url="/index.php?p=login"
admin=$(myynh_urlencode $admin)
ynh_print_OFF
	password=$(myynh_urlencode $password)
	ynh_local_curl $admin_url "creation=1" "login=$admin" "pass=$password" "confirm=$password"
ynh_print_ON

#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..."

if [ $is_public -eq 0 ]; then
	# escape magic chars in vars (lua magic chars are ().%+-*?[^$ according to https://www.lua.org/pil/20.2.html)
	domainluaregex=$(echo "$domain" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
	pathluaregex=$([ "$path_url" == "/" ] || echo "$path_url" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
	regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$"
fi

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..."

ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# END OF SCRIPT
#=================================================

ynh_script_progression --message="Installation of $app completed" --last