#! /bin/bash

# Raid Type
CardInfo=$(lspci|grep -P 'RAID|SCSI')
if [[ $CardInfo =~ 'LSI' ]];then
    if [[ $CardInfo =~ 'RAID' ]];then
        RaidType="LSI_RAID"
    else
        RaidType="LSI_HBA"
    fi
    for chip in 2208 3108 3316 3408 3416 3508 38xx
    do
        if [[ $CardInfo =~ $chip ]];then
            RaidType="LSI_RAID"
            RaidChip=$chip
            break
        fi
    done
    for chip in 2008 3008 2308
    do
        if [[ $CardInfo =~ $chip ]];then
            RaidType="LSI_HBA"
            RaidChip=$chip
            break
        fi
    done
elif [[ $CardInfo =~ 'Hewlett-Packard' || $CardInfo =~ 'HP' ]];then
    RaidType="HP_RAID"
    hp_slot=$(ssacli ctrl all show | awk '/Slot/{print $6}')
    RaidChip=$(ssacli ctrl slot=$hp_slot show |grep "Smart Array"|awk '{print $3}')
elif [[ $CardInfo =~ 'Adaptec' ]];then
    RaidType="PMC_RAID"
    RaidChip=$(arcconf getconfig 1 AD|grep "Controller Model"|awk -F ": " '{print $2}'|awk '{print $NF}')
    if [[ $RaidChip = "H460-M1" ]];then
        RaidType="PMC_HBA"
    elif [[ $RaidChip = "8222-SHBA" ]];then
        RaidType="PMC_HBA"
    fi
elif [[ $(lsscsi -g|grep disk|grep -c ATA) -eq 1 ]];then
    RaidType="SingleDisk"
    RaidChip="SingleDisk"
else
    RaidType="unknown"
    PRINT_ERR "[ERROR] Unknown RaidType"
fi

_install() {
    # SUGON I620-G30 3316 card set jbod
    local sugon_type="S1-U2-V5/S2-U2-V5"
    if [[ "${ManufactureType}" =~ "I620-G30" ]] && [[ $sugon_type =~ $MachineType ]] && [[ $RaidChip = "3316" ]];then
        local jbod_status=`/opt/MegaRAID/storcli/storcli64 /c0 show jbod|grep "JBOD"|grep -c "ON"`
        if [[ $jbod_status -eq 0 ]];then
            if /opt/MegaRAID/storcli/storcli64 /c0 set jbod=on ;then
                PRINT_ERR "[ERROR] $MachineType storcli64 c0 set jbod=on 失败"
                return 1
            fi
        fi
    fi
    # SA5212M5 3008 card set raid10
    # 全网只有H4.U2.V6.34这一种机型，3008 PMC卡需要数据盘做raid10
    # fixed: 注意此处有一个bug，会导致第二次初始化报错
    # 删除ID为604的逻辑盘
    # /opt/op/sas3ircu 0 deletevolume 604 noprompt
    # # 创建 raid10 的逻辑盘
    # /opt/op/sas3ircu 0 create RAID10 MAX 2:2 2:3 2:4 2:5 2:6 2:7 2:8 2:9  noprompt
    # # 注意：LSI SAS3008最多支持2个RAID，单个RAID中最多支持10个硬盘。所有的RAID包含的硬盘总量最多14个，剩余硬盘只能以独立的“Physical drive”方式被LSI SAS3008管理。
    # # 查看控制器0的所有逻辑盘状态
    # sas3ircu 0 status
    # # 查看控制器0的所有逻辑盘、物理盘、信息
    # sas3ircu 0 display
    local type_3008="H4.U2.V6.34"
    if [[ "${ManufactureType}" =~ "SA5212M5" ]] && [[ $type_3008 =~ $MachineType ]] && [[ $RaidChip = "3008" ]];then
        if /opt/op/sas3ircu 0 display | grep -qwc 'RAID10' ;then
            return 0
        else
            if /opt/op/sas3ircu 0 create RAID10 MAX 2:2 2:3 2:4 2:5 2:6 2:7 2:8 2:9 noprompt ;then
                PRINT_ERR "[ERROR] $type_3008 sas3ircu create RAID10 失败"
                return 1
            fi
        fi
    fi
    local type_3008="G1-U4-V5"
    if [[ "${ManufactureType}" =~ "EG840G-G20" ]] && [[ $type_3008 =~ $MachineType ]] && [[ $RaidChip = "3008" ]];then
        if /opt/op/sas3ircu 0 display | grep -qwc 'RAID10' ;then
            return 0
        else
            if /opt/op/sas3ircu 0 create RAID10 MAX 2:2 2:3 2:4 2:5 2:6 2:7 noprompt ;then
                PRINT_ERR "[ERROR] $type_3008 sas3ircu create RAID10 失败"
                return 1
            fi
        fi
    fi
}

_check() {
    _check="PASS"

    # check SUGON I620-G30 3316 card jbod
    local sugon_type="S1-U2-V5/S2-U2-V5"
    if [[ "${ManufactureType}" =~ "I620-G30" ]] && [[ $sugon_type =~ $MachineType ]] && [[ $RaipChip = "3316" ]];then
        local jbod_status=`/opt/MegaRAID/storcli/storcli64 /c0 show jbod|grep "JBOD"|grep -c "ON"`
        if [[ $jbod_status -eq 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] "${ManufactureType}" $MachineType RAID CARD JBOD NOT ON."
        fi
    fi
    # check SA5212M5 3008 card raid10
    local type_3008="H4.U2.V6.34"
    if [[ "${ManufactureType}" =~ "SA5212M5" ]] && [[ $type_3008 =~ $MachineType ]] && [[ $RaipChip = "3008" ]];then
        volume_status=`/opt/op/sas3ircu 0 display | grep -wc 'IR volume'`
        if [[ $volume_status -lt 2 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] "${ManufactureType}" $MachineType RAID CARD do raid10 FAIL."
        fi
    fi
}
