#!/bin/bash

echo "50-do-raid.sh" RaidInfo BASEDIR VMFLAG SecUsage SecUsage RaidType &>> $BASEDIR/raid.log
echo "50-do-raid.sh" $RaidInfo $BASEDIR $VMFLAG $SecUsage $SecUsage $RaidType &>> $BASEDIR/raid.log
echo "50-do-raid.sh" $# $@ &>> $BASEDIR/raid.log
date +%F_%T%Z &>> $BASEDIR/raid.log

create_raid() {
    local type=$1
    if [[ $(echo $RaidInfo|grep -c "+") -eq 1 ]];then
        local arg1=$(echo $RaidInfo|awk -F '+' '{print $1}'|tr 'R' 'r')
        local arg2=$(echo $RaidInfo|awk -F '+' '{print $2}'|tr 'R' 'r')

        local r1=${arg1//\*/ }
        local r2=${arg2//\*/ }
        # cmdb上的参数，R转成r，*转空格

        bash -x $BASEDIR/modules/libs/build_"$type"_raid.sh -c "$r1" -c "$r2" &>> $BASEDIR/raid.log
    else
        local raid=`echo ${RaidInfo//\*/ }|tr 'R' 'r'`
        bash -x $BASEDIR/modules/libs/build_"$type"_raid.sh -c "$raid" &>> $BASEDIR/raid.log
    fi
}

_install() {
    if [[ $VMFLAG = 1 ]] || [[ $RaidInfo =~ "noraid" ]] || [[ $SecUsage = "HW-resourcepool" ]] || [[ $SecUsage = "resourcepool" ]];then
        return 0
    fi

    if [[ $RaidType = "LSI_RAID" ]];then
        # Delete Old Raid
        local total_lsi_raid_num=$(/opt/MegaRAID/storcli/storcli64 /c0/vall show | grep RAID | wc -l)
        if [[ $total_lsi_raid_num -gt 1 ]]; then
            local data_lsi_raid_num=$(echo ${total_lsi_raid_num}-1 | bc)
            /opt/MegaRAID/storcli/storcli64 /c0/v1-${data_lsi_raid_num} delete force &>> $BASEDIR/raid.log
            if [[ $? -ne 0 ]];then
                PRINT_ERR "[ERROR] Old DataDisk Raid Delete Failed."
                return 1
            fi
        fi

        # Create New Raid
        create_raid lsi

    elif [[ $RaidType = "HP_RAID" ]];then
        local hp_slot=$(ssacli ctrl all show | awk '/Slot/{print $6}')

        # Delete Old Raid
        local total_hp_raid_num=$(ssacli  ctrl slot=$hp_slot ld all show  | grep array  | wc -l)
        if [[ $total_hp_raid_num -gt 1 ]]; then
            for (( i=2; i<=$total_hp_raid_num; i++ ))
            do
                echo y | ssacli ctrl slot=$hp_slot ld $i delete
                if [[ $? -ne 0 ]];then
                    PRINT_ERR "[ERROR] Old Raid LD$i Delete Failed."
                    return 1
                fi
            done
        fi

        # Create New Raid
        create_raid hp

    elif [[ $RaidType = "PMC_RAID" ]];then
        local DEV_ID=`arcconf list|grep -i 'Controller [0-9]'|awk -F ':' '{print $1}'|awk '{print $2}'`
        # Delete Old Raid
        local total_pmc_raid_num=$(arcconf getconfig $DEV_ID LD | grep "Logical Device number" | wc -l)
        if [[ $total_pmc_raid_num -gt 1 ]]; then
            for (( i=1; i<$total_pmc_raid_num; i++ ))
            do
                arcconf delete $DEV_ID logicaldrive $i noprompt  &>> $BASEDIR/raid.log
                if [[ $? -ne 0 ]];then
                    PRINT_ERR "[ERROR] Old Raid Logical Device $i Delete Failed."
                    return 1
                fi
            done
        fi

        # Create New Raid
        create_raid pmc
    fi
}

_check() {
    _check="PASS"
    if [[ $RaidInfo =~ "noraid" ]] || [[ $SecUsage = "HW-resourcepool" ]] || [[ $SecUsage = "resourcepool" ]];then
        return 0
    fi
    if [[ $RaidType = "LSI_RAID" ]];then
        if [[ $(echo $RaidInfo|grep -c "WB") -eq 1 ]];then
            if [[ $(/opt/MegaRAID/MegaCli/MegaCli64 -LDGetProp -cache -Lall -aAll|grep -c WriteBack) -eq 0 ]];then
                _check="FAIL"
                PRINT_ERR "[ERROR] Raid Logical Device WB check Failed."
            fi
        fi
    fi
}
