#!/bin/bash

# Script to uninstall the AppSentinels Edge Controller service
# This script works for both Ubuntu, Amazon Linux and Red Hat/CentOS systems.
# It stops and disables the service, removes the service file,
# and removes Docker if it's not needed by other services.
# Copyright (c) AppSentinels. All rights reserved.

# Detect the Linux distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
else
    echo "Cannot detect OS. Exiting."
    exit 1
fi

# Stop and disable the service
systemctl stop appsentinels-edge-controller
systemctl disable appsentinels-edge-controller

# Remove the service file
rm -f /etc/systemd/system/appsentinels-edge-controller.service

# Reload systemd daemon
systemctl daemon-reload

# Remove Docker if installed and not needed by other services
if command -v docker &> /dev/null; then
    case $OS in
        ubuntu|debian)
            apt remove docker.io -y # May not be needed if Docker is used by other services
            ;;
        rhel|centos|fedora)
            yum remove yum-utils docker-ce docker-ce-cli containerd.io -y # May not be needed if Docker is used by other services
            ;;
        amzn)
            yum remove docker -y
            ;;            
        *)
            echo "Unsupported OS: $OS"
            exit 1
            ;;
    esac
fi

echo "AppSentinels Edge Controller service uninstalled successfully."

