#!/bin/bash

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

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

#=================================================
# CHECK VERSION
#=================================================

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1

ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================

if [ "$upgrade_type" == "UPGRADE_APP" ]
then	
	ynh_script_progression --message="Upgrading source files..." --weight=1

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

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

#=======================================================
# MIGRATE MINIO BUCKET IF UPGRADING FROM AN OLD VERSION
#=======================================================

ynh_script_progression --message="Checking if a migration is needed"
if ynh_compare_current_package_version --comparison le --version 0.69.2.2~ynh1
then
	ynh_script_progression --message="Migrating MinIO data to local directory"
	
	mkdir -p "/var/lib/$app/data"
	chown -R minio:www-data "/var/lib/$app"
	ynh_exec_warn_less sudo -u minio $mc_path/mc mirror --preserve "minio/outlinestorage" "/var/lib/$app/data"

	chown -R $app:www-data "/var/lib/$app"
	ynh_script_progression --message="Data migration finished"
	ynh_script_progression --message="Data bucket was kept for safety, just in case !"
	ynh_script_progression --message="Don't forget to remove MinIO app if you don't use it !"

	db_name=$(ynh_sanitize_dbid --db_name=$app)
	ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS uuid-ossp;" --database=$db_name
else
	ynh_script_progression --message="No migration to be done"
fi

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1

# Create a dedicated NGINX config
ynh_add_nginx_config

# Install Nodejs
ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$NODEJS_VERSION

#=================================================
# UPGRADE YARN DEPENDENCIES
#=================================================
ynh_script_progression --message="Building $app. This can be very long, be patient !" --weight=10

pushd "$install_dir"
	ynh_use_nodejs
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH yarn install --frozen-lockfile --network-timeout 1000000000 2>&1
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH NODE_OPTIONS="--max-old-space-size=3900" yarn build 2>&1
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH yarn cache clean 2>&1
popd

#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Adding a configuration file..." --weight=1

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

chmod 400 "$install_dir/.env"
chown $app:$app "$install_dir/.env"

#=================================================
# RUN DB MIGRATION
#=================================================
ynh_script_progression --message="Running DB initial migration..."

pushd "$install_dir"
	ynh_use_nodejs
	ynh_exec_warn_less sudo -u $app env $ynh_node_load_PATH NODE_ENV=production yarn db:migrate
popd

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1

# Create a dedicated systemd config
ynh_add_systemd_config

# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append

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

yunohost service add $app --description="Outline server" --log="/var/log/$app/$app.log"

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

ynh_systemd_action --service_name=$app --action="start" --log_path="systemd"

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

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