#!/bin/bash

BLACKLIST_PATH="/etc/modprobe.d/blacklist.conf"

_install() {
    if [[ $VMLAG = 1 ]];then
        return 0
    fi

    # Disable HP hpwdt module
    if [[ $Manufacture = HP && $CpuModel == "CpuV3" ]];then
        if [[ $(grep -c 'blacklist hpwdt' $BLACKLIST_PATH) -eq 0 ]];then
            echo -e "\n# hp watchdog timer\nblacklist hpwdt" >> $BLACKLIST_PATH
        fi
    fi    
    
    # Disable sb_edac edac_core
    if [ -f $BLACKLIST_PATH ];then
        sed -i "/blacklist edac_core/d" $BLACKLIST_PATH
        sed -i "/blacklist sb_edac/d" $BLACKLIST_PATH
    fi
        rm -rf /etc/modprobe.d/blacklist-edac.conf
    echo -e "blacklist sb_edac\nblacklist edac_core" >> /etc/modprobe.d/blacklist-edac.conf

    # Prevent kipmi0 from consuming 100% CPU
    rm -rf /etc/modprobe.d/ipmi.conf
    echo "# Prevent kipmi0 from consuming 100% CPU" >> /etc/modprobe.d/ipmi.conf
    echo "options ipmi_si kipmid_max_busy_us=100" >> /etc/modprobe.d/ipmi.conf
    if [ -f "/sys/module/ipmi_si/parameters/kipmid_max_busy_us" ];then
        echo 100 > /sys/module/ipmi_si/parameters/kipmid_max_busy_us
    fi
}

_check() {
    _check="PASS"

    if [[ $VMLAG = 1 ]];then
        return 0
    fi

    if [[ $Manufacture = HP && $CpuModel == "CpuV3" && $(grep -c 'blacklist hpwdt' $BLACKLIST_PATH) -eq 0 ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] hpwdt Module Not Found In Kernel Module Blacklist."
    fi    

    if [[ $(grep -c 'blacklist edac_core' /etc/modprobe.d/blacklist-edac.conf) -eq 0 ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] edac_core Module Not Found In Kernel Module Blacklist."
    fi

    if [[ $(grep -c 'blacklist sb_edac' /etc/modprobe.d/blacklist-edac.conf) -eq 0 ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] sb_edac Module Not Found In Kernel Blacklist."
    fi

    if [[ $(grep -c 'ipmi_si' /etc/modprobe.d/ipmi.conf) -eq 0 ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] ipmi_si Module Not Found In Kernel Blacklist."
    fi

    if [ -f "/sys/module/ipmi_si/parameters/kipmid_max_busy_us" ];then
        if [[ $(grep -c 100 /sys/module/ipmi_si/parameters/kipmid_max_busy_us) -eq 0 ]];then
             _check="FAIL"
             PRINT_ERR "[ERROR] kipmid_max_busy_us Not 100."
        fi
    fi
}
