#!/bin/bash

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

source /usr/share/yunohost/helpers

#=================================================
# INITIALIZE AND STORE SETTINGS
#=================================================

ynh_app_setting_set --key=php_upload_max_filesize --value=250M
ynh_app_setting_set --key=php_post_max_size --value=260M

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression "Setting up source files..."

# Download, check integrity, uncompress and patch the source from manifest.toml
ynh_setup_source --dest_dir="$install_dir"

mkdir $install_dir/data $install_dir/public/media $install_dir/storage
chown -R $app:$app $install_dir/data $install_dir/public/media $install_dir/storage
chmod -R u+rwX $install_dir/data $install_dir/public/media $install_dir/storage

#=================================================
# APP INITIAL CONFIGURATION
#=================================================
ynh_script_progression "Adding $app's configuration files..."

ynh_config_add --template=".env" --destination="$install_dir/.env"

#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression "Adding system configurations related to $app..."

# Create a PHP-FPM config (with conf/extra_php-fpm.conf being appended to it)
ynh_config_add_phpfpm

# Create a dedicated NGINX config using the conf/nginx.conf template
ynh_config_add_nginx

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

ynh_script_progression "Installation of $app completed"
