#!/bin/bash

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

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

ynh_app_config_validate() {
    # Depending of the status of the $enable_regirtration we should default value of the hidden fields
    if $enable_registration; then
        # Must enable password authentication when free registration is enabled as any user must be able to authenticate
        password_enabled=true
    else
        registrations_require_3pid=email
        allowed_local_3pids_email=''
        allowed_local_3pids_msisdn=''
        disable_msisdn_registration=true
    fi
    _ynh_app_config_validate
}

ynh_app_config_apply() {
    _ynh_app_config_apply
    configure_nginx

    # Force enable it because some client like Element X don't support CAS and so require to have password authentication enabled
    password_enabled=true

    ynh_add_config --jinja --template="homeserver.yaml" --destination="/etc/matrix-$app/homeserver.yaml"
    ynh_add_config --template="log.yaml" --destination="/etc/matrix-$app/log.yaml"
    set_permissions
}

#=================================================
# GENERIC FINALIZATION
#=================================================
# SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
#=================================================
ynh_app_config_run "$1"