#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source _common.sh
source /usr/share/yunohost/helpers

#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================

password=$YNH_APP_ARG_PASSWORD
app_nb=$YNH_APP_INSTANCE_NUMBER
ssh_port=$(grep "^Port" /etc/ssh/sshd_config | awk '{print $2}')

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

[ $with_sftp -eq 0 ] || [ "$password" != "" ] || ynh_die --message="You need to set a password to enable SFTP"

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

ynh_app_setting_set --app=$app --key=password --value=$password

#=================================================
# CREATE A MYSQL DATABASE
#=================================================

if [ $with_mysql -eq 1 ]
then
    ynh_script_progression --message="Creating a MySQL database..." --weight=2

    db_name=$(ynh_sanitize_dbid --db_name=$app)
    db_user=$db_name
    db_pwd=$(ynh_string_random --length=30)
    ynh_app_setting_set --app=$app --key=db_name --value=$db_name
    ynh_app_setting_set --app=$app --key=db_user --value=$db_user
    ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
    ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
fi

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

nginx_extra_conf_dir="/etc/nginx/conf.d/$domain.d/$app.d"
mkdir -p "$nginx_extra_conf_dir"

# Prepare nginx extra conf
if [ $phpversion != "none" ]
then
    ynh_add_config --template="nginx-php.conf" --destination="$nginx_extra_conf_dir/php.conf"
    YNH_PHP_VERSION="$phpversion"
fi

# Add the custom error build with the application
ynh_add_config --template="nginx-code-error.conf" --destination="$nginx_extra_conf_dir/error-code.conf"
CUSTOM_ERROR_FILE=1

# Create a dedicated NGINX config
ynh_add_nginx_config
ynh_add_config --template="example-custom-nginx-config.conf" --destination="$nginx_extra_conf_dir/sample.conf"

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

if [ $with_sftp -eq 1 ]
then
    groups="sftp.app"
else
    groups=""
fi

    ynh_system_user_create --username=$app --home_dir="$install_dir" --groups="$groups"

if [ $with_sftp -eq 1 ]
then
    # Add the password to this user
    chpasswd <<< "${app}:${password}"
fi

#=================================================
# SPECIFIC SETUP
#=================================================
# MODIFY A CONFIG FILE
#=================================================

mkdir -p "$install_dir/www"

if [ $with_sftp -eq 1 ]
then
    ynh_add_config --template="../sources/www/index.html" --destination="$install_dir/www/index.html"
else
    ynh_add_config --template="../sources/www/index_no_sftp.html" --destination="$install_dir/www/index.html"
fi

chown -R $app:www-data "$install_dir"
# Home directory of the user needs to be owned by root to allow
# SFTP connections
chown root:root "$install_dir"
setfacl -m g:$app:r-x "$install_dir"
setfacl -m g:www-data:r-x "$install_dir"
chmod 750 "$install_dir"

#=================================================
# PHP-FPM CONFIGURATION
#=================================================

fpm_footprint="low"
fpm_free_footprint=0
fpm_usage="low"

ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
ynh_app_setting_set --app=$app --key=fpm_free_footprint --value=$fpm_free_footprint
ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage

if [ $phpversion != "none" ]
then
    ynh_script_progression --message="Configuring PHP-FPM..." --weight=2

    # Create a dedicatedPHP-FPM config
    ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --phpversion=$phpversion
fi

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

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