#!/bin/bash

YUMREPO_PATH=/etc/yum.repos.d
YUM_CONFIGS=$BASEDIR/configs/yum

_install () {
    rm -f $YUMREPO_PATH/*.repo
    if [[ $OS == "CentOS6" ]];then
        local ver=6
        local gpg_key=1
    elif [[ $OS == "CentOS7" ]];then
        local ver=7
        local gpg_key=2
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi
    if [[ $ARCH == "x86_64" ]];then
        wget -P $YUMREPO_PATH http://yum.service.ucloud.cn/custom/CentOS-$ver-all.repo >/dev/null 2>&1
    elif [[ $BFFLAG = 1 ]] && lspci -nn -d 15b3: |grep -q 'SoC PCIe Bridge' && lspci -nn -d 15b3: |grep BlueField |grep -q ConnectX-5 ;then
        wget -P $YUMREPO_PATH http://yum.service.ucloud.cn/custom/CentOS-7-altarch-all.repo >/dev/null 2>&1
    elif [[ $ARCH == "aarch64" ]];then
        wget -P $YUMREPO_PATH http://yum.service.ucloud.cn/custom/CentOS-7.9-altarch-all.repo >/dev/null 2>&1
    else
        PRINT_ERR "Not support ARCH '$ARCH'"
        return 1
    fi

    cp -f $YUM_CONFIGS/$ver/RPM-GPG-KEY-ucloud-$gpg_key  /etc/pki/rpm-gpg/RPM-GPG-KEY-ucloud-$gpg_key
    yum -y -q install yum-utils pciutils >/dev/null 2>&1
    if [[ $(uname -r) =~ "4.14.0" ]];then
        yum-config-manager --enable kernel-extras >/dev/null 2>&1
    fi
    if [[ $(uname -r) =~ "5.10.0" ]];then
        yum-config-manager --enable kernel-5.10 >/dev/null 2>&1
    elif lspci| grep -qi mellanox ;then
        yum-config-manager --enable mlnx >/dev/null 2>&1
    fi

}

_check () {
    _check="PASS"
    if [[ $OS == "CentOS6" ]];then
        local ver=6
        local gpg_key=1
    elif [[ $OS == "CentOS7" ]];then
        local ver=7
        local gpg_key=2
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi

    if [ ! -f /etc/pki/rpm-gpg/RPM-GPG-KEY-ucloud-$gpg_key ];then
        _check="FAIL"
        PRINT_ERR "[ERROR] File /etc/pki/rpm-gpg/RPM-GPG-KEY-ucloud-$gpg_key Not Found."
    fi
    if [[ $(uname -r) =~ "4.14.0" ]];then
        yum repolist |grep -q "^kernel-extras"
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] kernel-extras Repo Not Enabled In Yum Repo ."
        fi
    fi
    if [[ $(uname -r) =~ "5.10.0" ]];then
        yum repolist |grep -q "^kernel-5.10"
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] kernel-5.10 Repo Not Enabled In Yum Repo ."
        fi
    elif [[ $(lspci| grep -ic mellanox) -ne 0 && $ARCH == 'x86_64' ]];then
        yum repolist |grep -q "^mlnx"
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Mellanox Repo Not Enabled In Yum Repo ."
        fi
    fi
}
