#!/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
password=$YNH_APP_ARG_PASSWORD

app=$YNH_APP_INSTANCE_NAME

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

final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"

# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)

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

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_print_info "Storing installation settings..."

ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app path $path_url

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
ynh_print_info "Creating a MySQL database..."

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_print_info "Setting up source files..."

ynh_app_setting_set $app final_path $final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$final_path"

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_print_info "Configuring nginx web server..."

# Create a dedicated nginx config
ynh_add_nginx_config

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_print_info "Configuring system user..."

# Create a system user
ynh_system_user_create $app

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_print_info "Configuring php-fpm..."

# Create a dedicated php-fpm config
ynh_add_fpm_config

#=================================================
# SPECIFIC SETUP
#=================================================
# INITIALIZE DATABASE
#=================================================
ynh_print_info "Configuring baikal..."

ynh_mysql_connect_as "$db_name" "$db_pwd" "$db_name" \
    < "${final_path}/Core/Resources/Db/MySQL/db.sql"

#=================================================
# CONFIGURE BAIKAL
#=================================================

bk_conf="${final_path}/Specific/config.php"
cp ../conf/config.php "$bk_conf"

ynh_replace_string "__TIMEZONE__" "$(cat /etc/timezone)" "$bk_conf"
# Create admin password hash
password_hash=$(echo -n admin:BaikalDAV:$password | md5sum | cut -d ' ' -f 1)
ynh_replace_string "__PASSWORDHASH__" "${password_hash}" "$bk_conf"
ynh_app_setting_set $app password_hash $password_hash

# Store the config file checksum into the app settings
ynh_store_file_checksum "$bk_conf"

bk_conf="${final_path}/Specific/config.system.php"
cp ../conf/config.system.php "$bk_conf"

ynh_replace_string "__PATH__" "$path_url" "$bk_conf"
ynh_replace_string "__DBNAME__" "$db_name" "$bk_conf"
ynh_replace_string "__DBUSER__" "$db_name" "$bk_conf"
ynh_replace_string "__DBPASS__" "$db_pwd" "$bk_conf"

deskey=$(ynh_string_random 24)
ynh_app_setting_set $app encrypt_key "$deskey"
ynh_replace_string "__DESKEY__" "$deskey" "$bk_conf"

# Store the config file checksum into the app settings
ynh_store_file_checksum "$bk_conf"

# Disable installation
touch "${final_path}/Specific/INSTALL_DISABLED"

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================

# Set permissions
chown -R root: "$final_path"
chown $app "$final_path/Specific/"{config.php,config.system.php}
chmod 640 "$final_path/Specific/"{config.php,config.system.php}

#=================================================
# SETUP SSOWAT
#=================================================
ynh_print_info "Configuring SSOwat..."

# Allow public access on /
ynh_app_setting_set $app skipped_uris "/"
# But restrain on /admin
ynh_app_setting_set $app protected_uris "/admin/"

#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info "Reloading nginx web server..."

systemctl reload nginx

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

ynh_print_info "Installation of $app completed"