#!/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=systemd --line_match="Stopped $app"

if ynh_compare_current_package_version --comparison gt --version "1.1.1~ynh1"; then
    pkill -u "$app" || true
fi

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

if [ -n "${psql_db:-}" ]; then
    ynh_app_setting_delete --app="$app" --key=psql_db
fi

if [[ -z "${instance_name:-}" ]]; then
    instance_name="$name"
    ynh_app_setting_set --app="$app" --key=instance_name --value="$instance_name"
    ynh_app_setting_delete --app="$app" --key=name
fi

# Close a port
if yunohost firewall list | grep -q "\- $port$"; then
    ynh_script_progression --message="Closing port $port..."
    ynh_exec_warn_less yunohost firewall disallow TCP "$port"
fi

# Remove old repository
ynh_secure_remove --file="/etc/apt/sources.list.d/erlang-solutions.list"
apt-key del A14F4FCA

# Switch to $install_dir/live
if [ ! -d "$install_dir/live" ]; then
    mv "$install_dir/$app" "$install_dir/live"
fi

config="/etc/$app/config.exs"

if ynh_compare_current_package_version --comparison le --version "1.1.1~ynh1"; then
    ynh_script_progression --message="Upgrading to OTP release..." --weight=1

    # Give permission to the data_dir
    chown -R "$app":"$app" "$data_dir"

    rsync -a "$install_dir/live/uploads/" "$data_dir/uploads/"
    if [ -d "$install_dir/live/instance/static/" ]; then
        rsync -a "$install_dir/live/instance/static/" "$data_dir/static/"
    fi
    rsync -a "$install_dir/live/priv/static/emoji/" "$data_dir/static/emoji/"

    mkdir -p "/etc/$app"
    chown -R "$app" "/etc/$app"
    mv "$install_dir/live/config/prod.secret.exs" "$config"
    ynh_replace_string --target_file="$config" \
        --match_string="use Mix.Config" \
        --replace_string="import Config"
    echo "config :pleroma, :instance, static_dir: \"/home/yunohost.app/$app/static\"" >> "$config"
    echo "config :pleroma, Pleroma.Uploaders.Local, uploads: \"/home/yunohost.app/$app/uploads\"" >> "$config"

    ynh_backup_if_checksum_is_different --file="$config"
fi

if ynh_compare_current_package_version --comparison le --version "2.0.5~ynh1"; then
    cat "../conf/ldap.exs" >> "$config"
fi

if ynh_compare_current_package_version --comparison lt --version "2.5.5~ynh2"; then
    # packaging v1 did not set a shell
    usermod -s /bin/sh "$app"
fi

ynh_replace_string --target_file="$config" \
    --match_string="config :pleroma, configurable_from_database: false" \
    --replace_string="config :pleroma, configurable_from_database: true"

ynh_replace_string --target_file="$config" \
    --match_string='registrations_open: true' \
    --replace_string="registrations_open: $(boolstr "$registration")"

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

ynh_setup_source --dest_dir="$install_dir/live" --full_replace=1

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

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

if [ "$cache" -eq 1 ]; then
    ynh_add_config --template="../conf/cache.conf" --destination="/etc/nginx/conf.d/$app-cache.conf"
    cat ../conf/media.conf >> ../conf/nginx.conf
fi

# Create a dedicated NGINX config
ynh_add_nginx_config

# Create a dedicated systemd config
ynh_add_systemd_config
yunohost service add "$app" --description="$app daemon for Pleroma"

#=================================================
# SPECIFIC UPGRADE
#=================================================
# MAKE UPGRADE
#=================================================
ynh_script_progression --message="Making upgrade..." --weight=1

ynh_exec_warn_less ynh_exec_as "$app" -i "$install_dir/live/bin/pleroma_ctl" migrate

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

config="/etc/$app/config.exs"

ynh_backup_if_checksum_is_different --file="$config"

chmod 400 "$config"
chown "$app:$app" "$config"

#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1

ynh_systemd_action --service_name="$app" --action="start" --log_path=systemd --line_match="Access Pleroma.Web.Endpoint"

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

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