#!/bin/bash

# 检查用户是否正在使用白盒加密
is_user_whitebox() {
    local home_dir="$1"
    local status_file="$home_dir/.local/share/deepin-keyrings-wb/status"

    if [ -f "$status_file" ]; then
        local status_content=$(head -c 2 "$status_file")
        [ "${status_content:1:1}" == "1" ] && return 0
    fi
    return 1
}

# 清理用户的秘钥环目录
clean_user_keyrings() {
    local home_dir="$1"
    [ -d "$home_dir/.local/share/keyrings" ] && rm -rf "$home_dir/.local/share/keyrings"
    [ -d "$home_dir/.local/share/gnome-keyring" ] && rm -rf "$home_dir/.local/share/gnome-keyring"
    [ -d "$home_dir/.local/share/deepin-keyrings-wb" ] && rm -rf "$home_dir/.local/share/deepin-keyrings-wb"
}

main() {
    local uid_min=1000
    local uid_max=60000

    awk -F: "\$3 >= $uid_min && \$3 < $uid_max {OFS=\":\"; print \$1,\$3,\$6}" /etc/passwd | while IFS=: read -r username uid home_dir; do
        [ -z "$home_dir" ] || [ ! -d "$home_dir" ] && continue

        if ! is_user_whitebox "$home_dir"; then
            echo "Cleaning keyrings for user: $username"
            clean_user_keyrings "$home_dir"
        fi
    done
}

main
