#!/bin/bash

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

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

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=3

app=$YNH_APP_INSTANCE_NAME

domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
port_hub=$(ynh_app_setting_get --app=$app --key=port_hub)
port_http_proxy=$(ynh_app_setting_get --app=$app --key=port_http_proxy)
enable_terminal=$(ynh_app_setting_get --app=$app --key=enable_terminal)

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."

# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
	ynh_app_setting_set --app=$app --key=is_public --value=1
	is_public=1
elif [ "$is_public" = "No" ]; then
	ynh_app_setting_set --app=$app --key=is_public --value=0
	is_public=0
fi

# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
	final_path=/opt/$app

	mkdir -p $final_path

	ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=48

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	ynh_clean_check_starting

	# restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

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

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

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

# Create a dedicated nginx config
ynh_add_nginx_config

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1

# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app

#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=83

ynh_install_app_dependencies $pkg_dependencies

ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version

npm install -g configurable-http-proxy

python3 -m pip install pipenv

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

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

	# Download, check integrity, uncompress and patch the source from app.src
	mkdir -p $final_path

	pushd $final_path

	PIPENV_VENV_IN_PROJECT="enabled" PIPENV_SKIP_LOCK=true  ynh_exec_warn_less python3 -m pipenv install jupyterlab==$jupyterlab_version jupyterhub notebook jupyterhub-ldapauthenticator pyzmq

	ynh_exec_warn_less python3 -m pipenv run jupyterhub upgrade-db

	popd
fi

#=================================================
# SPECIFIC UPGRADE
#=================================================
# STORE THE CONFIG FILE CHECKSUM
#=================================================

ynh_backup_if_checksum_is_different --file="$final_path/config/jupyterhub_config.py"

mkdir -p "$final_path/config"

cp -f ../conf/jupyterhub_config.py $final_path/config/jupyterhub_config.py
ynh_replace_string --match_string="__URL__" --replace_string="https://$domain" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__PATH__" --replace_string="${path_url%/}" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__PORT_HUB__" --replace_string="$port_hub" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__PORT_HTTP_PROXY__" --replace_string="$port_http_proxy" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="$final_path/config/jupyterhub_config.py"
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$final_path/config/jupyterhub_config.py"

# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum --file="$final_path/config/jupyterhub_config.py"

ynh_backup_if_checksum_is_different --file="$final_path/config/jupyter_notebook_config.py"

cp -f ../conf/jupyter_notebook_config.py $final_path/config/jupyter_notebook_config.py

ynh_replace_string --match_string="__ENABLE_TERMINAL__" --replace_string="$enable_terminal" --target_file="$final_path/config/jupyter_notebook_config.py"

ynh_store_file_checksum --file="$final_path/config/jupyter_notebook_config.py"

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

# Create a dedicated systemd config
ynh_replace_string --match_string="__NODE_PATH__" --replace_string="$nodejs_path" --target_file="../conf/systemd.service"

ynh_add_systemd_config

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================

# Set permissions on app files
chown -R root: $final_path/
chown -R $admin: $final_path/.venv/

#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================

yunohost service add $app --description="$app daemon" --log="$app"

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

ynh_systemd_action --service_name=$app --action="start" --line_match="JupyterHub is now running at" --log_path="systemd"

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..."

ynh_systemd_action --service_name=nginx --action=reload

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

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