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

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

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

ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version

ynh_use_nodejs

$ynh_npm install -g configurable-http-proxy npm

#=================================================
# SPECIFIC UPGRADE
#=================================================
# BUILD APP
#=================================================

if [ "$upgrade_type" == "UPGRADE_APP" ]
then
	ynh_script_progression --message="Building app..." --weight=15

#REMOVEME? 	ynh_secure_remove $install_dir/.venv
	python3 -m venv $install_dir/venv
	source $install_dir/venv/bin/activate

	pushd $install_dir
		ynh_exec_warn_less pip install jupyterlab==$jupyterlab_version jupyterhub notebook jupyter_collaboration jupyterhub-ldapauthenticator pyzmq
	popd
fi

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

mkdir -p "$install_dir/config"

path=${path%/}

ynh_add_config --template="jupyterhub_config.py" --destination="$install_dir/config/jupyterhub_config.py"
ynh_add_config --template="jupyter_notebook_config.py" --destination="$install_dir/config/jupyter_notebook_config.py"

chown -R root: $install_dir/
chown -R $admin: $install_dir/venv/

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

# Create a dedicated systemd config
ynh_add_systemd_config

ynh_add_nginx_config

yunohost service add $app --description="Console environment for running Python code" --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" --line_match="JupyterHub is now running at" --log_path="systemd"

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

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