#!/bin/bash

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

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

upgrade_type=$(ynh_check_app_version_changed)

#=================================================
# 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="/var/log/$app/$app.log"
ynh_systemd_action --service_name=$app.timer --action="stop"

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

if [[ "${mailalert:-}" != "always" && "${mailalert:-}" != "errors_only" && "${mailalert:-}" != "never" ]]; then
    ynh_app_setting_set --app=$app --key="mailalert" --value="errors_only"
    export mailalert="errors_only"
fi
ynh_app_setting_set --app=$app --key="state" --value="not run since last update"
ynh_app_setting_set --app=$app --key="last_run" --value="-"


if [ -z "$repository" ]; then
    repository="ssh://$ssh_user@$server/~/backup"
    server=$(echo "$repository" | cut -d"@" -f2 | cut -d"/" -f1)
    if [[ $server == *":"* ]]; then
        server="[$(echo "$server" | cut -d":" -f1)]:$(echo "$server" | cut -d":" -f2)"
    fi
    ynh_app_setting_set $app repository "$repository"
    ynh_app_setting_set $app server "$server"
    ynh_app_setting_delete $app ssh_user
fi

if grep "borg.timer" /etc/yunohost/services.yml > /dev/null ; then
    yunohost service remove $app.timer
    systemctl enable $app.timer --quiet
    systemctl start $app.timer
fi

# Replace backports with pip
[ ! -e " /etc/apt/sources.list.d/$app-stretch-backports.list" ] || rm -f /etc/apt/sources.list.d/$app-stretch-backports.list

# Clear legacy stuff
if [ -d /opt/borg-env ]; then
    ynh_secure_remove --file="/opt/borg-env"
    ynh_secure_remove --file="/usr/local/bin/borg"
    ynh_secure_remove --file="/usr/local/bin/backup-with-borg"
fi

#=================================================
# UPGRADE BORG
#=================================================
ynh_script_progression --message="Upgrading Borg..." --weight=1

install_borg_with_pip

_gen_and_save_public_key

mkdir -p "/var/log/$app"
chown -R $app:$app "/var/log/$app"
chmod u+w "/var/log/$app"

#=================================================
# SETUP THE BACKUP METHOD
#=================================================
ynh_script_progression --message="Setting up backup method..." --weight=1

mkdir -p /etc/yunohost/hooks.d/backup
mkdir -p /etc/yunohost/hooks.d/backup_method
mkdir -p /usr/share/yunohost/backup_method

ynh_add_config --template="backup_method" --destination="/etc/yunohost/hooks.d/backup_method/05-${app}_app"
chmod go=--- "/etc/yunohost/hooks.d/backup_method/05-${app}_app"

ynh_add_config --template="backup-with-borg" --destination="$install_dir/backup-with-borg"
chmod u+x "$install_dir/backup-with-borg"
chown "$app:$app" "$install_dir/backup-with-borg"

ynh_add_config --template="sudoer" --destination="/etc/sudoers.d/$app"
chown root:root "/etc/sudoers.d/$app"

ynh_add_config --template="logging.conf" --destination="$install_dir/logging.conf"
chown "$app:$app" "$install_dir/logging.conf"

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

# Create a dedicated systemd config
ynh_add_systemd_config
yunohost service add $app --description="Deduplicating backup program" --test_status="systemctl show $app.service  -p ActiveState --value | grep -v failed"  --log "/var/log/$app/borg.log"
# Disable the service, this is to prevent the service from being triggered at boot time
systemctl disable $app.service --quiet

ynh_add_config --template="systemd.timer" --destination="/etc/systemd/system/$app.timer"
systemctl enable $app.timer --quiet
systemctl start $app.timer

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

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