#!/bin/bash

# Uninstall all Flatpak apps, runtimes and data files
flatpak uninstall --all -y --noninteractive --force-remove --delete-data

if [ -n "$(flatpak list)" ]; then
    echo "Failed to uninstall all Flatpak packages"
    exit 1
fi

# Uninstall Flatpak
apt-get purge -y --autoremove flatpak

if dpkg -l | grep -q flatpak; then
    echo "Failed to uninstall Flatpak"
    exit 2
fi

rm -rf /var/lib/flatpak

echo "Flatpak has been completely uninstalled system-wide."
echo "Note: User-specific Flatpak data (in ~/.local/share/flatpak and ~/.config/flatpak) must be removed manually by each user."
exit 0
