#!/bin/bash
# Uninstall script for swai_inst installed via test-bootc.sh

set -e

# Check for root privileges
if [ "$EUID" -ne 0 ]; then
    echo "Error: This script must be run as root"
    exit 1
fi

echo "Determining Python purelib path..."
# Dynamically fetch the purelib path using python3
SITELIB=$(python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))")

if [ -z "$SITELIB" ]; then
    echo "Error: Could not determine Python purelib path."
    exit 1
fi

echo "Removing swai_inst from $SITELIB..."
if [ -d "${SITELIB}/swai_inst" ]; then
    rm -rf "${SITELIB}/swai_inst"
    echo "Successfully removed ${SITELIB}/swai_inst"
else
    echo "Directory ${SITELIB}/swai_inst not found. Skipping."
fi

echo "Removing /usr/bin/swai-inst..."
if [ -f "/usr/bin/swai-inst" ]; then
    rm -f /usr/bin/swai-inst
    echo "Successfully removed /usr/bin/swai-inst"
else
    echo "File /usr/bin/swai-inst not found. Skipping."
fi

echo "Removing desktop and mime files..."
rm -f /usr/share/applications/swai-installer.desktop
rm -f /usr/share/mime/packages/swai-mime.xml

echo "Updating mime and desktop databases..."
update-mime-database /usr/share/mime || true
update-desktop-database /usr/share/applications || true

echo "Done! swai_inst has been uninstalled."
