#!/bin/bash
# postrm — chạy SAU khi gỡ gói.
# Chỉ xóa dữ liệu khi $1 = 'remove' (gỡ/purge gói);
# các status khác (install/upgrade/replace) → thoát ngay.
set -u

if [ "$1" != "remove" ]; then
    exit 0
fi

# Xóa unit systemd + reload.
if command -v systemctl >/dev/null 2>&1; then
    rm -f /etc/systemd/system/autoai.service
    systemctl daemon-reload || true
fi

# Xóa cấu hình + toàn bộ dữ liệu cài đặt.
rm -rf /etc/autoai /opt/autoai

# Xóa user 'autoai' nếu không còn process nào của user đang chạy.
if id -u autoai >/dev/null 2>&1 && [ -z "$(pgrep -u autoai 2>/dev/null || true)" ]; then
    userdel autoai || true
fi

exit 0
