#!/bin/bash

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers

#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1

app=$YNH_APP_INSTANCE_NAME

path_url=$(ynh_app_setting_get --app="$app" --key=path)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
db_name=$(ynh_app_setting_get --app="$app" --key=db_name)
db_user=$db_name
domain=$(ynh_app_setting_get --app="$app" --key=domain)
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
admin=$(ynh_app_setting_get --app="$app" --key=admin)
admin_mail=$(ynh_user_get_info "$admin" mail)
github_account=$(ynh_app_setting_get --app="$app" --key=github_account)
github_token=$(ynh_app_setting_get --app="$app" --key=github_token)
key=$(ynh_string_random 50)
redis_db=$(ynh_app_setting_get --app="$app" --key=redis_db)

#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=40

# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
	# restore it if the upgrade fails
	ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors

#=================================================
# Migrate legacy permissions to new system
#=================================================
if ynh_legacy_permissions_exists
then
	ynh_legacy_permissions_delete_all

	ynh_app_setting_delete --app=$app --key=is_public
fi

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

if [[ -d "$final_path/bin/" ]]
then
	ynh_secure_remove --file="$final_path/bin/"
fi

# (<3.8) log cleanups
if [[ -e "/var/log/uwsgi/app/$app" ]]
then
    ynh_systemd_action --service_name="$app" --action="stop"
    ynh_systemd_action --service_name="$app-celery" --action="stop"
	ynh_secure_remove --file="/var/log/uwsgi/app/$app"
	ynh_secure_remove --file="/var/log/$app-celery"
fi

#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping systemd services..." --weight=5

ynh_systemd_action --service_name="$app" --action="stop"
ynh_systemd_action --service_name="$app-celery" --action="stop"

#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=3

# Create a dedicated nginx config
ynh_add_nginx_config

#=================================================
# SPECIFIC UPGRADE
#=================================================
# Update dependencies
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=5

ynh_exec_warn_less ynh_install_app_dependencies "$pkg_dependencies"

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1

# Create a system user
ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell

#=================================================
# CONFIGURE HUB
#=================================================
ynh_script_progression --message="Configure hub..." --weight=2

mkdir -p "$final_path/.config/"
ynh_add_config --template="../conf/hub_config" --destination="$final_path/.config/hub"

cat <<EOF > "$final_path/.bashrc"
eval "$(hub alias -s /bin/bash)"
EOF

#=================================================
# SPECIFIC SETUP UWSGI
#=================================================
ynh_script_progression --message="Configure uwsgi..." --weight=2

finaluwsgiini="/etc/uwsgi/apps-available/$app.ini"

ynh_add_config --template="../conf/uwsgi.ini" --destination="$finaluwsgiini"

#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=3

ynh_add_systemd_config --service="$app" --template="weblate.service"

#=================================================
# ACTIVATE CELERY
#=================================================
ynh_script_progression --message="Configure celery..." --weight=2

celeryconf="$final_path/celery-weblate"

ynh_add_config --template="../conf/celery-weblate" --destination="$celeryconf"

ynh_add_systemd_config --service="$app-celery" --template="celery-weblate.service"

#=================================================
# UPGRADE WEBLATE
#=================================================

upgrade() {
	new_version=$1
	settings_template=$2
	#=================================================
	# PIP INSTALLATION
	#=================================================
	ynh_script_progression --message="Install weblate using PIP..." --weight=15

	ynh_systemd_action --service_name="$app-celery" --action="stop"

	virtualenv --python=python3 "${final_path}/venv"
	chown -R "$app": "$final_path"

	#run source in a 'sub shell'
	(
		set +o nounset
		source "${final_path}/venv/bin/activate"
		set -o nounset
		cd "${final_path}"

		sudo --user=$app $final_path/venv/bin/pip install --upgrade pip setuptools wheel
		# Tmp fix
		sudo --user=$app $final_path/venv/bin/pip install --upgrade cffi==1.14
		# Still needed with latest version of weblate?
		sudo --user=$app $final_path/venv/bin/pip install --upgrade Weblate=="$new_version"
		sudo --user=$app $final_path/venv/bin/pip install psycopg2-binary ruamel.yaml aeidon phply
		#pip install pytz python-bidi PyYaML Babel pyuca pylibravatar py3dns psycopg2-binary phply django-redis hiredis aeidon ruamel.yaml
		# specific to YunoHost package:
		sudo --user=$app $final_path/venv/bin/pip install django_sendmail_backend
	)

	#=================================================
	# MODIFY A CONFIG FILE
	#=================================================
	ynh_script_progression --message="Create weblate configuration file..." --weight=2
	# save old settings file
	settings="$final_path/venv/lib/$weblate_pypath/site-packages/weblate/settings.py"
	path_url="${path_url%/}"

	ynh_add_config --template="../conf/settings.py" --destination="$settings"

	ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"

	# Recalculate and store the config file checksum into the app settings
	ynh_store_file_checksum --file="$settings"

	touch "$final_path/local_settings.py"
	ln -sf "$final_path/local_settings.py" "$final_path/venv/lib/$weblate_pypath/site-packages/weblate/local_settings.py"

	#=================================================
	# MIGRATE WEBLATE
	#=================================================
	ynh_script_progression --message="Run migration scripts..." --weight=10

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

	(
		set +o nounset
		source "${final_path}/venv/bin/activate"
		set -o nounset
		export DJANGO_SETTINGS_MODULE="weblate.settings"
		cd "${final_path}"

		sudo --user=$app $final_path/venv/bin/weblate migrate --noinput
		sudo --user=$app $final_path/venv/bin/weblate collectstatic --noinput
		sudo --user=$app $final_path/venv/bin/weblate setuplang
		sudo --user=$app $final_path/venv/bin/weblate setupgroups
		sudo --user=$app $final_path/venv/bin/weblate compilemessages
		
		# Check the configuration
		# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
		if [ "$new_version" == "$weblate_version" ]; then
			sudo --user=$app $final_path/venv/bin/weblate check --deploy || true
		fi
	)
}

file_version="${final_path}/venv/lib/$weblate_pypath/site-packages/weblate/__init__.py"
if [ -e $file_version ]
then
	current_version=$(cat $file_version | grep "^VERSION = " | grep -o "[0-9].[0-9]" | head -n1 | cut -d"." -f1)
	if [ -z "$current_version" ]
	then
		file_version="${final_path}/venv/lib/$weblate_pypath/site-packages/weblate/utils/version.py"
		current_version=$(cat $file_version | grep "^VERSION = " | grep -o "[0-9].[0-9]" | head -n1 | cut -d"." -f1)
	fi
else
	current_version=3
fi

ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database=$db_name

if [ "$current_version" -lt "4" ]
then
	upgrade "4.1.1" "../conf/settings.4.1.1.py"
fi

upgrade $weblate_version "../conf/settings.py"

#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1

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

#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=2

yunohost service add "$app" --log "/var/log/$app/weblate.log"
yunohost service add "$app-celery" --log "/var/log/$app/weblate-celery-w1.log"

#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================

# Set right permissions for curl installation
mkdir -p "$final_path/avatar-cache"
chown -R "$app": "$final_path"
chown "$app:www-data" "$final_path"
chown -R "$app:www-data" "$final_path/data"

chmod 750 "$final_path"
chmod -R o-rwx "$final_path"

#=================================================
# START SYSTEMD SERVICES
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=5

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

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1

ynh_systemd_action --service_name=nginx --action=reload

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

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