#!/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
with_sftp=$YNH_APP_ARG_WITH_SFTP
password=$YNH_APP_ARG_PASSWORD
is_public=$YNH_APP_ARG_IS_PUBLIC

with_mysql=$YNH_APP_ARG_WITH_MYSQL

app=$YNH_APP_INSTANCE_NAME
app_nb=$YNH_APP_INSTANCE_NUMBER

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

final_path=/var/www/$app
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

#=================================================
# 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=with_mysql --value=$with_mysql
ynh_app_setting_set --app=$app --key=with_sftp --value=$with_sftp
ynh_app_setting_set --app=$app --key=final_path --value=$final_path

ynh_app_setting_set --app=$app --key=overwrite_nginx --value=0
ynh_app_setting_set --app=$app --key=overwrite_phpfpm --value=1
ynh_app_setting_set --app=$app --key=admin_mail_html --value=1

#=================================================
# STANDARD MODIFICATIONS
#=================================================
# 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)
    ynh_app_setting_set --app=$app --key=db_name --value=$db_name
    ynh_mysql_setup_db --db_user=$db_name --db_name=$db_name
fi

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

# 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="$final_path" --groups="$groups"

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

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

mkdir -p "$final_path/www"

if [ $with_sftp -eq 1 ]
then

    ynh_add_config --template="../sources/www/index.html" --destination="$final_path/www/index.html"

else
    # Copy files to the right place
    cp "../sources/www/index_no_sftp.html" "$final_path/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" "$final_path/db_access.txt"
fi

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

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

# Create a dedicatedPHP-FPM config
ynh_add_fpm_config --usage=low --footprint=low
phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)

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

# Make app public if necessary
if [ $is_public -eq 1 ]
then
    ynh_permission_update --permission="main" --add="visitors"
fi

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

ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# SEND A README FOR THE ADMIN
#=================================================

# Get main domain and buid the url of the admin panel of the app.
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"

if [ $with_mysql -eq 1 ]
then
    sql_infos="
You've asked for a database, please find here the information about this SQL database.
$(cat "$final_path/db_access.txt")
"
else
    sql_infos=""
fi

if [ $with_sftp -eq 1 ]
then
    sftp_infos="You can connect to this repository by using SFTP with the following credentials.
Domain: $domain
Port: $(grep "^Port" /etc/ssh/sshd_config | awk '{print $2}')
User: $app
Password: The one you set at installation."
else
    sftp_infos=""
fi

echo "This app is simply a blank web app container. You have to put your own app inside.
$sql_infos
Please put your files into this directory: $final_path/www/
$sftp_infos

You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.

If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/my_webapp_ynh__URL_TAG3__." > mail_to_send

ynh_send_readme_to_admin --app_message="mail_to_send" --recipients=root --type=install

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

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