#!/bin/bash

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

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

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

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

#=================================================
# 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
tempdir="$(mktemp -d)"
ynh_setup_source --dest_dir="$tempdir"
mkdir -p $install_dir

chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"

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

ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version

#=================================================
# SPECIFIC UPGRADE
#=================================================
# UPGRADE VIA PIP
#=================================================
ynh_script_progression --message="Upgrading via pip" --weight=1

ynh_use_nodejs
python3 -m venv "${install_dir}/venv"
cp ../conf/requirements.txt "$install_dir/requirements.txt"
chown -R "$app" "$install_dir"

#run source in a 'sub shell'
(
    set +o nounset
    source "${install_dir}/venv/bin/activate"
    set -o nounset
    ynh_exec_as $app $install_dir/venv/bin/pip install --upgrade pip
    ynh_exec_as $app $install_dir/venv/bin/pip install -r "$install_dir/requirements.txt"
)

# we use this virtualenv archivebox for further commands now
archivebox_cmd="$install_dir/venv/bin/archivebox"

#=================================================
# UPGRADE NODE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading node dependencies..."
	
cp -f $tempdir/package.json "$install_dir/package.json"
cp -f $tempdir/package-lock.json "$install_dir/package-lock.json"
ynh_secure_remove --file="$tempdir"
pushd $install_dir
	ynh_use_nodejs
	ynh_exec_warn_less ynh_exec_as $app $ynh_node_load_PATH $ynh_npm ci
popd

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

ynh_store_file_checksum --file="$data_dir/ArchiveBox.conf"
ynh_add_config --template="ArchiveBox.conf" --destination="$data_dir/ArchiveBox.conf"

chmod 600 "$data_dir/ArchiveBox.conf"
chown $app:$app "$data_dir/ArchiveBox.conf"

#=================================================
# FINISH ARCHIVEBOX SETUP
#=================================================

# rerun archivebox setup (its idempotent, so it should be ok during upgrade)
ynh_script_progression --message="Finishing Archivebox Setup" --weight=1
pushd $data_dir
	ynh_exec_warn_less ynh_exec_as $app $ynh_node_load_PATH $archivebox_cmd init --setup
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

# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append

yunohost service add $app --description="Self-hosted internet archiving" --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" --log_path="/var/log/$app/$app.log"

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

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