#!/bin/bash

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

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


mkdir -p "$install_dir/public/media" "$install_dir/public/static"
mkdir -p "$install_dir"

mkdir -p "/var/log/$app"
touch "/var/log/$app/$app.log"

redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"

# 'debug_enabled' -> '__DEBUG_ENABLED__' -> settings.DEBUG
debug_enabled="0"

# 'log_level' -> '__LOG_LEVEL__' -> settings.LOG_LEVEL
log_level="WARNING"

# 'admin_email' -> '__ADMIN_EMAIL__' add in settings.ADMINS
admin_email="${admin}@${domain}"

# 'default_from_email' -> '__DEFAULT_FROM_EMAIL__' -> settings.DEFAULT_FROM_EMAIL
default_from_email="${app}@${domain}"


ynh_app_setting_set --app="$app" --key=debug_enabled --value="$debug_enabled"
ynh_app_setting_set --app="$app" --key=log_level --value="$log_level"
ynh_app_setting_set --app="$app" --key=admin_email --value="$admin_email"
ynh_app_setting_set --app="$app" --key=default_from_email --value="$default_from_email"

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

_install_fmd_venv

mkdir -p "$install_dir/public/media" "$install_dir/public/static"

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

mkdir -p "/var/log/$app"
touch "/var/log/$app/$app.log"

chmod o-rwx "/var/log/$app"
chown -R "$app:$app" "/var/log/$app"

#=================================================
# copy config files
# ================================================
ynh_script_progression --message="Create $app 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"

touch "$install_dir/local_settings.py"

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

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

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

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

#=================================================
# 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 --service="$app" --template="systemd.service"
yunohost service add "$app" --description="$app service" --log="/var/log/$app/$app.log"

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

#=================================================
# 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="Installation of $app completed" --last