#!/bin/bash

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

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

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

ynh_setup_source --dest_dir="$install_dir"
myynh_clean_source

mkdir "$install_dir/private"
ln -s "$data_dir/uploads" "$install_dir/uploads"
ln -s "$data_dir/thumbs" "$install_dir/thumbs"

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

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

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

ynh_add_nginx_config

ynh_add_fpm_config

#=================================================
# SETUP APPLICATION WITH CURL
#=================================================
ynh_script_progression --message="Configuring admin user in BoZon..." --weight=2

## fill the superadmin creation form (helper ynh_local_curl doesn't work due to --data vs --data-urlencode ?)
ynh_local_curl "/index.php?p=login" "creation=1" "login=$admin" "pass=$password" "confirm=$password"

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

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