#!/bin/bash

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

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

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

if [ -z "${debug_enabled:-}" ]; then
    debug_enabled="0"
    ynh_app_setting_set --app="$app" --key=debug_enabled --value="$debug_enabled"
fi

if [ -z "${log_level:-}" ]; then
    log_level="WARNING"
    ynh_app_setting_set --app="$app" --key=log_level --value="$log_level"
fi

if [ -z "${admin_email:-}" ]; then
    admin_email="${admin}@${domain}"
    ynh_app_setting_set --app="$app" --key=admin_email --value="$admin_email"
fi

if [ -z "${default_from_email:-}" ]; then
    default_from_email="${app}@${domain}"
    ynh_app_setting_set --app="$app" --key=default_from_email --value="$default_from_email"
fi

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping systemd service '$app'..." --weight=5

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

#=================================================
# PIP INSTALLATION
#=================================================
ynh_script_progression --message="Installing project via pip..." --weight=45

# Always recreate everything fresh with current python version
ynh_secure_remove "$install_dir/venv"

_install_scovie_venv

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

#=================================================
# copy config files
# ================================================
ynh_script_progression --message="Create project configuration files..."

ynh_add_config --template="gunicorn.conf.py" --destination="$install_dir/gunicorn.conf.py"

ynh_add_config --template="manage.py" --destination="$install_dir/manage.py"
chmod +x "$install_dir/manage.py"

ynh_add_config --template="settings.py" --destination="$install_dir/settings.py"
ynh_add_config --template="setup_user.py" --destination="$install_dir/setup_user.py"
ynh_add_config --template="urls.py" --destination="$install_dir/urls.py"
ynh_add_config --template="wsgi.py" --destination="$install_dir/wsgi.py"

#=================================================
# MIGRATE PYINVENTORY
#=================================================
ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10

cd "$install_dir" || exit

# Just for debugging:
./manage.py diffsettings

./manage.py migrate --no-input
./manage.py collectstatic --no-input

# Create/update Django superuser (set unusable password, because auth done via SSOwat):
./manage.py create_superuser --username="$admin" --email="$(ynh_user_get_info "$admin" mail)"

# Check the configuration
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
./manage.py check --deploy || true

#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1

ynh_add_nginx_config

ynh_add_systemd_config --service="$app" --template="systemd.service"
yunohost service add "$app" --description="Digital signage system for high schools" --log="/var/log/$app/$app.log"

ynh_use_logrotate --non-append
chmod o-rwx "$log_path"
chown -R "$app:" "$log_path"

#=================================================
# Start the app server via systemd
#=================================================
ynh_script_progression --message="Starting systemd service '$app'..." --weight=5

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

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

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