#!/bin/bash

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

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

export preinstall=0

#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping $app's systemd service..." --weight=1

ynh_systemd_action --service_name="$app" --action="stop" --log_path="/var/log/$app.log"

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

#=================================================
# ADD SWAP
#=================================================

# if [ "${PACKAGE_CHECK_EXEC:-0}" -ne 1 ]; then
#     ynh_script_progression --message="Adding swap..." --weight=1
#     ynh_add_swap --size=$swap_needed
# fi

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Upgrading source files..." --weight=1

# Download, check integrity, uncompress and patch the source from app.src
setup_files

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

pushd "$install_dir"
    ynh_exec_warn_less ynh_exec_as "$app" \
        RUSTUP_HOME="$install_dir/.rustup" \
        CARGO_HOME="$install_dir/.cargo" \
        bash -c 'curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -q -y'

    export PATH="$PATH:$install_dir/.cargo/bin:$install_dir/.local/bin:/usr/local/sbin"

    if grep "python3" "$install_dir/$appname/$FORKNAME-bin" ; then
        python3 -m venv venv
        venv/bin/pip3 install --upgrade pip
        venv/bin/pip3 install wheel
        venv/bin/pip3 install -r "$appname/requirements.txt"
    else
        virtualenv venv
        venv/bin/pip3 install --upgrade pip
        venv/bin/pip install wheel
        venv/bin/pip install -r "$appname/requirements.txt"
    fi
    ynh_secure_remove --file="$install_dir/.cargo"
    ynh_secure_remove --file="$install_dir/.rustup"
popd

#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Upgrading 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" --log="/var/log/$app.log"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting $app's systemd service..." --weight=1

ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app.log"

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

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