#!/bin/bash

# (this is used in the apt helpers, big meh ...)
readonly YNH_DEFAULT_PHP_VERSION=7.4

# Legacy: auto-convert phpversion to php_version (for consistency with nodejs_version, ruby_version, ...)
if [[ -n "${app:-}" ]] && [[ -n "${phpversion:-}" ]]
then
    if [[ -z "${php_version:-}" ]]
    then
        php_version=$phpversion
        ynh_app_setting_set --key=php_version --value=$php_version
    fi
    ynh_app_setting_delete --key=phpversion
    unset phpversion
fi

# Create a dedicated PHP-FPM config
#
# usage: ynh_config_add_phpfpm
#
# This helper assumes the app has an conf/extra_php-fpm.conf snippet
#
# The actual PHP configuration will be automatically generated, 
# and your extra_php-fpm.conf will be appended (typically contains PHP upload limits)
#
# The resulting configuration will be deployed to the appropriate place, /etc/php/$php_version/fpm/pool.d/$app.conf
#
# Performance-related options in the PHP conf, such as : 
# pm.max_children, pm.start_servers, pm.min_spare_servers pm.max_spare_servers
# are computed from two parameters called "usage" and "footprint" which can be set to low/medium/high. (cf details below)
#
# If you wish to tweak those, please initialize the settings `fpm_usage` and `fpm_footprint` 
# *prior* to calling this helper. Otherwise, "low" will be used as a default for both values.
#
# Otherwise, if you want the user to have control over these, we encourage to create a config panel 
# (which should ultimately be standardized by the core ...)
#
# The footprint of the service will be used to defined the maximum footprint we can allow, which is half the maximum RAM.
# So it will be used to defined 'pm.max_children'
# A lower value for the footprint will allow more children for 'pm.max_children'. And so for
#    'pm.start_servers', 'pm.min_spare_servers' and 'pm.max_spare_servers' which are defined from the
#    value of 'pm.max_children'
# NOTE: 'pm.max_children' can't exceed 4 times the number of processor's cores.
#
# The usage value will defined the way php will handle the children for the pool.
# A value set as 'low' will set the process manager to 'ondemand'. Children will start only if the
#   service is used, otherwise no child will stay alive. This config gives the lower footprint when the
#   service is idle. But will use more proc since it has to start a child as soon it's used.
# Set as 'medium', the process manager will be at dynamic. If the service is idle, a number of children
#   equal to pm.min_spare_servers will stay alive. So the service can be quick to answer to any request.
#   The number of children can grow if needed. The footprint can stay low if the service is idle, but
#   not null. The impact on the proc is a little bit less than 'ondemand' as there's always a few
#   children already available.
# Set as 'high', the process manager will be set at 'static'. There will be always as many children as
#   'pm.max_children', the footprint is important (but will be set as maximum a quarter of the maximum
#   RAM) but the impact on the proc is lower. The service will be quick to answer as there's always many
#   children ready to answer.
#
# Requires YunoHost version 4.1.0 or higher.
ynh_config_add_phpfpm() {
    # ============ Argument parsing =============
    local -A args_array=([g]=group=)
    local group
    ynh_handle_getopts_args "$@"
    group=${group:-}
    # ===========================================

    # If the PHP version changed, remove the old fpm conf
    # (NB: This stuff is also handled by the apt helper, which is usually triggered before this helper)
    # FIXME: so is this still needed @_@
    local old_php_version=$(ynh_app_setting_get --key=php_version)
    if [ -n "$old_php_version" ] && [ "$old_php_version" != "$php_version" ]; then
        local old_php_fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir)
        local old_php_finalphpconf="$old_php_fpm_config_dir/pool.d/$app.conf"

        if [[ -f "$old_php_finalphpconf" ]]
        then
            ynh_backup_if_checksum_is_different "$old_php_finalphpconf"
            ynh_remove_fpm_config
        fi
    fi

    local fpm_service="php${php_version}-fpm"
    local fpm_config_dir="/etc/php/$php_version/fpm"

    # Create the directory for FPM pools
    mkdir --parents "$fpm_config_dir/pool.d"

    # FIXME: zzzz do we really need those ...
    ynh_app_setting_set --key=fpm_config_dir --value="$fpm_config_dir"
    ynh_app_setting_set --key=fpm_service --value="$fpm_service"
    ynh_app_setting_set --key=php_version --value=$php_version

    # Define the values to use for the configuration of PHP.
    _ynh_get_scalable_phpfpm

    local phpfpm_group=$([[ -n "$group" ]] && echo "$group" || echo "$app")
    local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
    echo "
[__APP__]

user = __APP__
group = __PHPFPM_GROUP__

chdir = __INSTALL_DIR__

listen = /var/run/php/php__PHP_VERSION__-fpm-__APP__.sock
listen.owner = www-data
listen.group = www-data

pm = __PHP_PM__
pm.max_children = __PHP_MAX_CHILDREN__
pm.max_requests = 500
request_terminate_timeout = 1d
" >"$phpfpm_path"

    if [ "$php_pm" = "dynamic" ]; then
        echo "
pm.start_servers = __PHP_START_SERVERS__
pm.min_spare_servers = __PHP_MIN_SPARE_SERVERS__
pm.max_spare_servers = __PHP_MAX_SPARE_SERVERS__
" >>"$phpfpm_path"

    elif [ "$php_pm" = "ondemand" ]; then
        echo "
pm.process_idle_timeout = 10s
" >>"$phpfpm_path"
    fi

    # Concatene the extra config.
    if [ -e $YNH_APP_BASEDIR/conf/extra_php-fpm.conf ]; then
        cat $YNH_APP_BASEDIR/conf/extra_php-fpm.conf >>"$phpfpm_path"
    fi

    ynh_config_add --template="$phpfpm_path" --destination="$fpm_config_dir/pool.d/$app.conf"

    # Validate that the new php conf doesn't break php-fpm entirely
    if ! php-fpm${php_version} --test 2>/dev/null; then
        php-fpm${php_version} --test || true
        ynh_safe_rm "$fpm_config_dir/pool.d/$app.conf"
        ynh_die "The new configuration broke php-fpm?"
    fi
    ynh_systemctl --service=$fpm_service --action=reload
}

# Remove the dedicated PHP-FPM config
#
# usage: ynh_config_remove_phpfpm
#
# Requires YunoHost version 2.7.2 or higher.
ynh_config_remove_phpfpm() {
    local fpm_config_dir=$(ynh_app_setting_get --key=fpm_config_dir)

    ynh_safe_rm "$fpm_config_dir/pool.d/$app.conf"
    ynh_systemctl --service="php${php_version}-fpm" --action=reload
}

# Define the values to configure PHP-FPM
#
# [internal]
#
# usage: _ynh_get_scalable_phpfpm
# Footprint can be defined via the "fpm_footprint", to be set prior to calling this helper
# low    - Less than 20 MB of RAM by pool.
# medium - Between 20 MB and 40 MB of RAM by pool.
# high   - More than 40 MB of RAM by pool.
# Or specify exactly the footprint, the load of the service as MB by pool instead of having a standard value.
# To have this value, use the following command and stress the service.
# watch -n0.5 ps -o user,cmd,%cpu,rss -u APP
#
# Usage can be defined via the "fpm_usage", to be set prior to calling this helper
# low    - Personal usage, behind the SSO.
# medium - Low usage, few people or/and publicly accessible.
# high   - High usage, frequently visited website.
#
_ynh_get_scalable_phpfpm() {

    # If no usage provided, default to the value existing in setting ... or to low
    local fpm_usage_in_setting=$(ynh_app_setting_get --key=fpm_usage)
    local usage=${fpm_usage_in_setting:-low}
    ynh_app_setting_set --key=fpm_usage --value=$usage

    # If no footprint provided, default to the value existing in setting ... or to low
    local fpm_footprint_in_setting=$(ynh_app_setting_get --key=fpm_footprint)
    local footprint=${fpm_footprint_in_setting:-low}
    ynh_app_setting_set --key=fpm_footprint --value=$footprint

    # Set all characters as lowercase
    if [ "$footprint" = "low" ]; then
        footprint=20
    elif [ "$footprint" = "medium" ]; then
        footprint=35
    elif [ "$footprint" = "high" ]; then
        footprint=50
    fi

    # Define the factor to determine min_spare_servers
    # to avoid having too few children ready to start for heavy apps
    if [ $footprint -le 20 ]; then
        min_spare_servers_factor=8
    elif [ $footprint -le 35 ]; then
        min_spare_servers_factor=5
    else
        min_spare_servers_factor=3
    fi

    # Define the way the process manager handle child processes.
    if [ "$usage" = "low" ]; then
        php_pm=ondemand
    elif [ "$usage" = "medium" ]; then
        php_pm=dynamic
    elif [ "$usage" = "high" ]; then
        php_pm=static
    else
        ynh_die "Does not recognize '$usage' as an usage value."
    fi

    # Get the total of RAM available, except swap.
    local max_ram=$(ynh_get_ram --total)

    at_least_one() {
        # Do not allow value below 1
        if [ $1 -le 0 ]; then
            echo 1
        else
            echo $1
        fi
    }

    # Define pm.max_children
    # The value of pm.max_children is the total amount of ram divide by 2 and divide again by the footprint of a pool for this app.
    # So if PHP-FPM start the maximum of children, it won't exceed half of the ram.
    php_max_children=$(($max_ram / 2 / $footprint))
    # If process manager is set as static, use half less children.
    # Used as static, there's always as many children as the value of pm.max_children
    if [ "$php_pm" = "static" ]; then
        php_max_children=$(($php_max_children / 2))
    fi
    php_max_children=$(at_least_one $php_max_children)

    # To not overload the proc, limit the number of children to 4 times the number of cores.
    local core_number=$(nproc)
    local max_proc=$(($core_number * 4))
    if [ $php_max_children -gt $max_proc ]; then
        php_max_children=$max_proc
    fi

    # Get a potential forced value for php_max_children
    local php_forced_max_children=$(ynh_app_setting_get --key=php_forced_max_children)
    if [ -n "$php_forced_max_children" ]; then
        php_max_children=$php_forced_max_children
    fi

    if [ "$php_pm" = "dynamic" ]; then
        # Define pm.start_servers, pm.min_spare_servers and pm.max_spare_servers for a dynamic process manager
        php_min_spare_servers=$(($php_max_children / $min_spare_servers_factor))
        php_min_spare_servers=$(at_least_one $php_min_spare_servers)

        php_max_spare_servers=$(($php_max_children / 2))
        php_max_spare_servers=$(at_least_one $php_max_spare_servers)

        php_start_servers=$(($php_min_spare_servers + ($php_max_spare_servers - $php_min_spare_servers) / 2))
        php_start_servers=$(at_least_one $php_start_servers)
    else
        php_min_spare_servers=0
        php_max_spare_servers=0
        php_start_servers=0
    fi
}

# Execute a command with Composer
#
# Will use $install_dir as workdir unless $composer_workdir exists (but that shouldnt be necessary)
#
# You may also define composer_user=root prior to call this helper if you absolutely need composer to run as root, but this is discouraged...
#
# usage: ynh_composer_exec commands
#
# Requires YunoHost version 4.2 or higher.
ynh_composer_exec() {
    local workdir="${composer_workdir:-$install_dir}"

    COMPOSER_HOME="$workdir/.composer" \
    COMPOSER_MEMORY_LIMIT=-1 \
    sudo -E -u "${composer_user:-$app}" \
    php${php_version} "$workdir/composer.phar" $@ \
        -d "$workdir" --no-interaction --no-ansi 2>&1
}

# Install and initialize Composer in the given directory
#
# The installed version is defined by $composer_version which should be defined
# as global prior to calling this helper.
#
# Will use $install_dir as workdir unless $composer_workdir exists (but that shouldnt be necessary)
#
# usage: ynh_composer_install
#
# Requires YunoHost version 4.2 or higher.
ynh_composer_install() {
    local workdir="${composer_workdir:-$install_dir}"

    [[ -n "${composer_version}" ]] || ynh_die "\$composer_version should be defined before calling ynh_composer_install. (In the past, this was called \$YNH_COMPOSER_VERSION)"

    [[ ! -e "$workdir/composer.phar" ]] || ynh_safe_rm $workdir/composer.phar

    local composer_url="https://getcomposer.org/download/$composer_version/composer.phar"

    # NB. we have to declare the var as local first,
    # otherwise 'local foo=$(false) || echo 'pwet'" does'nt work
    # because local always return 0 ...
    local out
    # Timeout option is here to enforce the timeout on dns query and tcp connect (c.f. man wget)
    out=$(wget --tries 3 --no-dns-cache --timeout 900 --no-verbose --output-document=$workdir/composer.phar $composer_url 2>&1) \
        || ynh_die "$out"
}