#!/bin/bash

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

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

#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================

secret=$(ynh_string_random --length=64)
create_account=false

ynh_app_setting_set --app="$app" --key="secret" --value="$secret"
ynh_app_setting_set --app="$app" --key="create_account" --value="$create_account"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..." --weight=4

# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir"
ynh_setup_source --dest_dir="$install_dir/extensions/LDAPAuthentication2" --source_id="ldap_authentication2"
ynh_setup_source --dest_dir="$install_dir/extensions/LDAPProvider" --source_id="ldap_provider"
ynh_setup_source --dest_dir="$install_dir/extensions/PluggableAuth" --source_id="pluggable_auth"

# ynh_setup_source --dest_dir="$install_dir/extensions/LDAPGroups" --source_id="ldap_groups"
# ynh_setup_source --dest_dir="$install_dir/extensions/LDAPUserInfo" --source_id="ldap_userinfo"
# ynh_setup_source --dest_dir="$install_dir/extensions/LDAPAuthorization" --source_id="ldap_authorization"

chmod -R o-rwx "$install_dir"
chown -R "$app:www-data" "$install_dir"

#=================================================
# RUN INSTALLATION OF MEDIAWIKI
#=================================================
ynh_script_progression --message="Installing and configuring Mediawiki..." --weight=14

if [ "$path" = "/" ]; then
    # MediaWiki expects a "" for the root URL which is typically assumed to be
    # "/" by other application packages. Therefore, we assume end-users will do
    # this as well and make sure to ensure an "" in all cases where "/" is
    # specified
    mediawiki_path=""
else
    mediawiki_path="$path"
fi

pushd "$install_dir"
    "php$phpversion" "$install_dir/maintenance/install.php" \
        --server "https://$domain" \
        --scriptpath "$mediawiki_path" \
        --dbuser "$db_name" \
        --dbpass "$db_pwd" \
        --dbname "$db_name" \
        --dbprefix "mdk_" \
        --lang "$language" \
        --pass "$password" \
        "$wiki_name" "$admin"
popd

#=================================================
# APP INITIAL CONFIGURATION
#=================================================
ynh_script_progression --message="Adding $app's configuration files..." --weight=1

# save the original LocalSettings.php for eventual debugging
mv "$install_dir/LocalSettings.php" "$install_dir/LocalSettings.php.orig"

wiki_name_underscorified="${wiki_name//[^a-zA-Z0-9]/_}"

ynh_add_config --template="LocalSettings.php" --destination="$install_dir/LocalSettings.php"

# This is required to update database schemas
"php$phpversion" "$install_dir/maintenance/update.php"

chown -R "$app:www-data" "$install_dir"

#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1

# Create a dedicated PHP-FPM config
ynh_add_fpm_config --usage=low --footprint=low

# Create a dedicated NGINX config
ynh_add_nginx_config

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

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