#!/bin/bash

set -e

case "$1" in
    remove|upgrade|deconfigure)
        echo "Deteniendo servicios antes de la eliminación o actualización..."
        systemctl stop nginx || true
        systemctl stop tftpd-hpa || true
        systemctl stop php8.3-fpm || true
        systemctl stop samba || true

        echo "Eliminando configuraciones específicas..."
        if [ "$1" = "remove" ]; then
            rm -f /etc/nginx/sites-enabled/ogboot.conf
            rm -f /etc/nginx/sites-available/ogboot.conf
            rm -f /etc/sudoers.d/opengnsys
            rm -f /etc/php/8.3/fpm/pool.d/ogboot.conf
            dpkg-divert --remove --rename --divert /etc/default/tftpd-hpa.orig /etc/default/tftpd-hpa || true
            dpkg-divert --remove --rename --divert /opt/opengnsys/ogboot/.env.local.php.orig /opt/opengnsys/ogboot/.env.local.php || true
        fi
        ;;
esac

exit 0
