#!/bin/bash

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

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

#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=15

# Install Nodejs
ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version

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

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

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

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring PHP-FPM..." --weight=2

# Create a dedicated PHP-FPM config
ynh_add_fpm_config

# Create a dedicated NGINX config
ynh_add_nginx_config

ynh_add_config --template="../conf/feeds.txt" --destination="$data_dir/feeds.txt"

ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app"
chown root: "/etc/cron.d/$app"
chmod 644 "/etc/cron.d/$app"

#=================================================
# INSTALL LYCHEE WITH COMPOSER
#=================================================
ynh_script_progression --message="Installing RSS with Composer..." --weight=10

# Install composer
ynh_install_composer --install_args="--no-dev"

# Create database file
touch $install_dir/storage/database/database.sqlite

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1

ynh_add_config --template="../conf/.env" --destination="$install_dir/.env"

#=================================================
# FINAL BOOKSTACK INSTALL
#=================================================
ynh_script_progression --message="Install the app" --weight=14

pushd $install_dir
	php$phpversion artisan key:generate --no-interaction --force
	php$phpversion artisan migrate --no-interaction --force
	php$phpversion artisan storage:link
	ynh_use_nodejs
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH $ynh_npm install
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH $ynh_npm run build
popd

# Set permissions to app files
chmod 755 $install_dir
chmod -R o-rwx $install_dir
chown -R $app:www-data $install_dir

chmod 600 $install_dir/.env
chown $app:$app $install_dir/.env

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

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