#!/bin/sh

# This script serves as an "undo" scroll for the service installation script,
# removing a systemd service that was previously created

if [ $# -lt 1 ]; then
    echo "Usage: $0 service_name"
    exit 1
fi

# Ensure systemd is available
if ! command -v systemctl >/dev/null 2>&1; then
    echo "Systemd is not installed or not in PATH. Exiting."
    exit 1
fi

service_dir=/etc/systemd/system
service_name=$1

# Add .service if not present in the name
case $service_name in
    *.service) ;;
    *) service_name="$service_name.service" ;;
esac

service_path="$service_dir/$service_name"

# Check if service file exists
if [ ! -f "$service_path" ]; then
    echo "Service $service_name does not exist."
    exit 1
fi

# Stop and disable service if it's running
if systemctl is-active --quiet "$service_name"; then
    systemctl stop "$service_name"
fi

# Remove the service file
sudo rm -f "$service_path"

# Reload systemd
systemctl daemon-reload

echo "Service $service_name removed."
exit 0