#!/bin/bash

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

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

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

ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version
ynh_use_nodejs

ynh_exec_warn_less "$ynh_npm" install -g configurable-http-proxy

#=================================================
# SET ADMIN GROUP
#=================================================
ynh_script_progression --message="Adding $admin to $app's group..." --weight=5

usermod -a -G "$app" "$admin"

#=================================================
# BUILD APP
#=================================================
ynh_script_progression --message="Building app..." --weight=5

python3 -m venv "$install_dir/venv"

ynh_exec_warn_less "$install_dir/venv/bin/python3" -m pip install \
    jupyterlab=="$(jupyterlab_upstream_version)" \
    jupyterhub \
    jupyter_collaboration \
    notebook \
    jupyterhub-ldapauthenticator \
    pyzmq \
    jupyterlab-language-pack-fr-FR

#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding $app's configuration file..." --weight=1

mkdir -p "$install_dir/config"

path_no_slash=${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 "$app:$app" "$install_dir"

#=================================================
# SYSTEM CONFIGURATION
#=================================================
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1

# Create a dedicated NGINX config
ynh_add_nginx_config

# Create a dedicated systemd config
ynh_add_systemd_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 $app's systemd service..." --weight=3

# Start a systemd service
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="Installation of $app completed" --last