#!/bin/bash

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

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

#=================================================
# INITIALIZE AND STORE SETTINGS
#=================================================

preinstall=0

#=================================================
# 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

#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Configuring $app's PostgreSQL database..." --weight=1

# Make sure that its encoding is UTF-8
ynh_psql_execute_as_root --sql="update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name'"

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up 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

#=================================================
# SETUP DATABASE
#=================================================
ynh_script_progression --message="Setting up the database..." --weight=1

setup_database

#=================================================
# 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" --log="/var/log/$app.log"

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

# Start a systemd service
ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app.log"

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

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