#!/bin/bash

#=================================================
# GENERIC START
#=================================================

# Import common cmd
source ./experimental_helper.sh
source ./_common.sh

# Source YunoHost helpers
source /usr/share/yunohost/helpers

#=================================================
# STANDARD REMOVE
#=================================================

ynh_script_progression --message="Stoping services..."

# Stop all services
ynh_systemd_action --service_name seafile --action stop
ynh_systemd_action --service_name seahub --action stop

# Force to kill all process in case of a process is not stoped cleanly
pkill -f seafile-controller || true
pkill -f seaf-server || true
pkill -f ccnet-server || true
pkill -f seahub || true

ynh_script_progression --message="Removing logs..."
ynh_secure_remove --file=/var/log/seafile
ynh_script_progression --message="Removing code..."
ynh_secure_remove --file=/var/www/$app
ynh_secure_remove --file=/opt/yunohost/$app

# Remove databases
ynh_script_progression --message="Removing databases..."
db_helper=ynh_"mysql"_remove_db
$db_helper --db_name ccnetdb --db_user seafile
$db_helper --db_name seahubdb --db_user seafile

# Remove systemd service
ynh_script_progression --message="Removing systemd units..."
ynh_remove_systemd_config --service seafile
ynh_remove_systemd_config --service seahub

# Remove nginx config
ynh_script_progression --message="Removing nginx configuration..."
ynh_remove_nginx_config

# Remove logrotate
ynh_remove_logrotate

# Remove the dedicated fail2ban config
ynh_script_progression --message="Removing fail2ban configuration..."
ynh_remove_fail2ban_config

ynh_script_progression --message="Removing seafile service..."
yunohost service remove seafile
yunohost service remove seahub

ynh_script_progression --message="Removal of $app completed" --last
sleep 1