#!/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
phpversion=$YNH_APP_ARG_PHPVERSION
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..."

ynh_app_setting_set --app=$app --key=password --value=$password
ynh_app_setting_set --app=$app --key=ssh_port --value=$ssh_port
ynh_app_setting_set --app=$app --key=with_mysql --value=$with_mysql
ynh_app_setting_set --app=$app --key=with_sftp --value=$with_sftp
ynh_app_setting_set --app=$app --key=phpversion --value=$phpversion
ynh_app_setting_set --app=$app --key=admin_mail_html --value=1

#=================================================
# 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

# Prepare nginx.conf
if [ $phpversion != "none" ]
then
    cp ../conf/nginx{_with_php,}.conf
    YNH_PHP_VERSION="$phpversion"
else
    cp ../conf/nginx{_no_php,}.conf
fi

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# 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
    # Copy files to the right place
    cp "../sources/www/index_no_sftp.html" "$install_dir/www/index.html"
fi

#if [ $with_mysql -eq 1 ]; then
#    # Store the database access
#    echo -e "# MySQL Database
#name: ${db_name}\nuser: ${db_name}\npass: ${db_pwd}" > ../sources/db_access.txt
#
#    # Copy files to the right place
#    cp -r "../sources/db_access.txt" "$install_dir/db_access.txt"
#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