#!/bin/bash

#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

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

ynh_abort_if_errors

#=================================================
# RETRIEVE ARGUMENTS
#=================================================

install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
domain=$(ynh_app_setting_get --app=$app --key=domain)
path=$(ynh_app_setting_get --app=$app --key=path)

current_fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage)

#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================

get__fpm_footprint() {
    # Free footprint value for php-fpm
    # Check if current_fpm_footprint is an integer
    if [ "$current_fpm_footprint" -eq "$current_fpm_footprint" ] 2> /dev/null
    then
        echo "specific"
    else
        echo "$current_fpm_footprint"
    fi
}

get__free_footprint() {
    # Free footprint value for php-fpm
    # Check if current_fpm_footprint is an integer
    if [ "$current_fpm_footprint" -eq "$current_fpm_footprint" ] 2> /dev/null
    then
        # If current_fpm_footprint is an integer, that's a numeric value for the footprint
        echo "$current_fpm_footprint"
    else
        echo "0"
    fi
}

#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================

#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================

set__password() {
    if [ "$password" == "" ]
    then
        ynh_app_setting_set --app=$app --key=password --value="$password"
    fi
}

set__fpm_footprint() {
    if [ "$fpm_footprint" != "specific" ]
    then
        ynh_app_setting_set --app=$app --key=fpm_footprint --value="$fpm_footprint"
    fi
}

set__fpm_free_footprint() {
    if [ "$fpm_footprint" = "specific" ]
    then
        ynh_app_setting_set --app=$app --key=fpm_footprint --value="$fpm_free_footprint"
    fi
}

#=================================================
# GENERIC FINALIZATION
#=================================================

ynh_app_config_validate() {
    _ynh_app_config_validate

    if [ "${changed[with_sftp]}" == "true" ] && [ $with_sftp -eq 1 ] && [ "$password" == "" ]
    then
        ynh_die --message="You need to set a password to enable SFTP"
    fi

    if [ "${changed[fpm_usage]}" == "true" ] || [ "${changed[fpm_footprint]}" == "true" ] || [ "${changed[fpm_free_footprint]}" == "true" ]; then
        # If fpm_footprint is set to 'specific', use $fpm_free_footprint value.
        if [ "$fpm_footprint" = "specific" ]
        then
            fpm_footprint=$fpm_free_footprint
        fi

        if [ "$fpm_footprint" == "0" ]
        then
            ynh_print_err --message="When selecting 'specific', you have to set a footprint value into the field below."

            exit 0
        fi
    fi
}

ynh_app_config_apply() {
    _ynh_app_config_apply

    if [ "${changed[phpversion]}" == "true" ]
    then
        ynh_app_setting_set --app=$app --key=phpversion --value="${old[phpversion]}"
        ynh_remove_fpm_config
        # ^ the helper includes ynh_remove_app_dependencies
        YNH_PHP_VERSION=$phpversion
        # ^ ynh_add_config replaces __PHPVERSION__ by __PHP_YNH_VERSION__...
        ynh_app_setting_set --app=$app --key=phpversion --value="$phpversion"

        nginx_extra_conf_dir="/etc/nginx/conf.d/$domain.d/$app.d"
        mkdir -p "$nginx_extra_conf_dir"
        if [ "$phpversion" == "none" ]
        then
            ynh_delete_file_checksum --file="$nginx_extra_conf_dir/php.conf"
            ynh_secure_remove --file="$nginx_extra_conf_dir/php.conf"
        else
            ynh_add_config --template="nginx-php.conf" --destination="$nginx_extra_conf_dir/php.conf"
            ynh_install_app_dependencies "php${phpversion}-fpm"
            ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --phpversion=$phpversion
            # ^ the helper takes care of ynh_app_setting_set the phpversion
        fi

        ynh_add_nginx_config
    fi

    if [ "${changed[with_sftp]}" == "true" ] && [ $with_sftp -eq 1 ]
    then
        ynh_system_user_add_group --username=$app --groups="sftp.app"

        if [ ! "$password" == "" ]
        then
            chpasswd <<< "${app}:${password}"
        fi
    elif [ "${changed[with_sftp]}" == "true" ] && [ $with_sftp -eq 0 ]
    then
        ynh_system_user_del_group --username=$app --groups="sftp.app"
    fi

    if [ "${changed[custom_error_file]}" == "true" ]
    then
      CUSTOM_ERROR_FILE=$custom_error_file
      nginx_extra_conf_dir="/etc/nginx/conf.d/$domain.d/$app.d"

      if [ $custom_error_file -eq 1 ]
      then
        ynh_add_config --template="nginx-code-error.conf" --destination="$nginx_extra_conf_dir/error-code.conf"
      elif [ $custom_error_file -eq 0 ]
      then
        ynh_secure_remove --file="$nginx_extra_conf_dir/error-code.conf"
      fi
      ynh_systemd_action --service_name=nginx --action=reload
    fi

    if [ "$phpversion" != "none" ]
    then
        ynh_add_fpm_config --phpversion=$phpversion --usage=$fpm_usage --footprint=$fpm_footprint
    fi

}

ynh_app_config_run $1