#!/bin/bash

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

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

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

secret=$(ynh_string_random)
email=$(ynh_user_get_info --username=$admin --key=mail)

#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================

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

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

# 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=2

# Download, check integrity, uncompress and patch the source from app.src
git config --system --add safe.directory $install_dir
git clone -b $nodebb_version https://github.com/NodeBB/NodeBB.git "$install_dir" --quiet

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

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..." --weight=3

# Create a dedicated NGINX config
ynh_add_nginx_config

#=================================================
# SPECIFIC SETUP
#=================================================
# CONFIGURE NODEBB
#=================================================
ynh_script_progression --message="Configuring the app..." --weight=2

ynh_add_config --template="../conf/config.json" --destination="$install_dir/config.json"

setup="{
		\"admin:username\": \"$admin\",
		\"admin:password\": \"$password\",
		\"admin:password:confirm\": \"$password\",
		\"admin:email\": \"$email\"
	}"

pushd $install_dir
	ynh_use_nodejs
	npm install npm@latest --location=global
	ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install lodash --save
	ynh_exec_as $app env $ynh_node_load_PATH $install_dir/nodebb setup "${setup}" 2>/dev/null 
popd

chmod 400 "$install_dir/config.json"
chown $app:$app "$install_dir/config.json"

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1

# Create a dedicated systemd config
ynh_add_systemd_config

#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1

# Use logrotate to manage application logfile(s)
ynh_use_logrotate

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1

yunohost service add $app --description="Forum software" --log="/var/log/$app/$app.log"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1

# Start a systemd service
ynh_systemd_action --service_name=$app --action=restart --log_path="systemd"

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

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