#!/bin/bash

# Started by AICoder, pid:je5bbe0674p552814d2b0a3970aa493151b13629

function usage() {
    echo "Usage: $0 Uninstall all rpm packages related to zxdh"
    echo "Usage: $0 [Options1]"
    echo "Options1:"
    echo "  -h|--help       Show the usage"
    exit 1
}


# Define an array containing the prefixes of the RPM packages to be uninstalled
installed_packages_match=("zxdh-eth" "npsdk" "zxdh-rdma-core" "zxdh-neo-sda" "zxdh-smartnic-config" "zxdh-dpu-config" "zxdh-zf-mpf" "zxdh-hpf")


# Define a function to check if an RPM package is installed.
function is_package_installed {
    rpm -qa | grep "^$1" > /dev/null
}


# Define a function to uninstall a specified RPM package.
function uninstall_package {
    if is_package_installed $1; then
        echo "Uninstalling $1..."
        rpm -e --nodeps $1
    fi
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    usage;
    exit 0
fi


# Uninstall operation
# Traverse the installed_packages_prefix
for package in "${installed_packages_match[@]}"; do
    # Use wildcards to search for matching RPM packages
    installed_packages=$(rpm -qa | grep "^${package%\*}")
    #If package is installed, if it is installed, then uninstall it
    if [ -n "$installed_packages" ]; then
        for pkg in $installed_packages; do
            uninstall_package $pkg
        done
    fi
done

exit 0



# Ended by AICoder, pid:je5bbe0674p552814d2b0a3970aa493151b13629
