#!/bin/bash

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

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

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

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

if [ -d "/opt/yunohost/$app" ]; then
    if [ -d "$install_dir/app" ]; then
        ynh_secure_remove --file="/opt/yunohost/$app"
    else
        mv "/opt/yunohost/$app" "$install_dir/app"
    fi
fi

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

mkdir -p "$install_dir/"{app,public/media,public/static}

ynh_add_config --template="requirements.txt" --destination="$install_dir/app/requirements.txt"

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

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

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

chown -R "$app:www-data" "$install_dir"

chown -R "$app:www-data" "$install_dir"

mkdir -p "/var/log/$app"
touch "/var/log/$app/$app.log"
chown -R "$app:$app" "/var/log/$app"

#=================================================
# PYTHON VIRTUALENV
#=================================================
ynh_script_progression --message="Upgrading $app..." --weight=10

_venv_install

_build_app

pushd "$install_dir/app"
    # Just for debugging:
    ynh_exec_as "$app" "$venvpy" ./manage.py diffsettings

    ynh_exec_as "$app" "$venvpy" ./manage.py migrate --no-input
    ynh_exec_as "$app" "$venvpy" ./manage.py collectstatic --no-input

    # Create/update Django superuser (set unusable password, because auth done via SSOwat):
    ynh_exec_as "$app" "$venvpy" ./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.
    ynh_exec_as "$app" "$venvpy" ./manage.py check --deploy || true
popd

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

ynh_add_nginx_config

ynh_add_systemd_config
yunohost service add "$app" --description="Django-fritzconnection server" --log="/var/log/$app/$app.log"

ynh_use_logrotate --non-append

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

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

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