#!/bin/bash

# Remove the tuning flag added by the install script.
if grep -q "amdgpu.ppfeaturemask" /etc/default/grub 2> /dev/null; then
    sed -i '/GRUB_CMDLINE_LINUX_DEFAULT/s/ amdgpu.ppfeaturemask=0xfffd7fff//' /etc/default/grub
    update-grub > /dev/null 2>&1 || true
fi

# Reuse login.defs UID boundaries to target regular users only.
uid_min="$(awk '/^UID_MIN/ {print $2; exit}' /etc/login.defs)"
uid_max="$(awk '/^UID_MAX/ {print $2; exit}' /etc/login.defs)"
[ -n "$uid_min" ] || uid_min=1000
[ -n "$uid_max" ] || uid_max=60000

while IFS=: read -r user _ uid _ _ home _; do
    [ "$user" = "nobody" ] && continue
    [ "$uid" -ge "$uid_min" ] && [ "$uid" -le "$uid_max" ] || continue
    [ -d "$home" ] || continue
    case "$home" in
        /home/*|/var/home/*) ;;
        *) continue ;;
    esac

    # Remove files created by the companion configuration script.
    rm -f "$home/.config/autostart/org.corectrl.CoreCtrl.desktop"
    rm -f "$home/.config/corectrl/corectrl.ini"
    rm -f "/etc/polkit-1/rules.d/90-corectrl-$user.rules"
done < /etc/passwd
