#!/bin/bash

findsh(){
    find "$1" -name '[0-9][0-9][0-9]-*.sh' | sort
}

find_script_list() {
    cd "$BASEDIR"/modules || exit 1
    # dns,yum等，顺序必须在最前的初始化任务，内网跑base，控制台跑console
    if [[ `echo $HOSTIP|grep -c '^100.'` -eq 0  ]];then
        script_list=`findsh base/`
    else
        script_list=`findsh console/`
    fi

    # 通用
    script_list="$script_list"$'\n'`findsh general/ `

    # wiwo,监控,业务个性化设置等乱七八糟的
    if [[ `echo $HOSTIP|grep -c '^100.'` -eq 0 ]];then
        script_list="$script_list"$'\n'`findsh plus/ `
    fi

    # 物理机
    if [[ $VMFLAG -eq 0 && $BFFLAG -eq 0 ]];then
        script_list="$script_list"$'\n'`findsh physical/ `
    fi
    total_scripts=`echo "$script_list" | wc -l`
}

DO_INIT() {
    find_script_list
    for module in $script_list
    do
#        if [[ -n $PANGU ]];then
#            UPLOAD_PROCESSING "init_initing" $module $NR_PASS $NR_FAIL $total_scripts
#        fi
        source $module
        if _install ;then
            PRINT_PASS $module
        else
            PRINT_FAIL $module
        fi
    done

    (( NR_TOTAL=NR_PASS+NR_FAIL ))
    install_done_time=$(date -u '+%Y-%m-%d %H:%M:%S')

    PRINT "--------------------------------------------------------"
    PRINT "<font color='green'>$install_done_time UTC 初始化执行完成</font>"

    if [[ $NR_PASS -eq 0 || $NR_FAIL -ne 0 ]] ;then
        run_status=1
        PRINT "<font color='red'>执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL</font>"
    else
        PRINT "<font color='green'>执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL</font>"
    fi

    if [[ -n $PANGU && $run_status == 1 ]] ;then
        UPLOAD_LOG FAIL
        exit 1
    elif [[ -n $bios_fw && $bios_fw == "reboot" ]] ;then
        PRINT "<font color='green'>检测到有配置需要重启生效，请等待机器重启并重新初始化</font>"
        UPLOAD_LOG FAIL
        shutdown -r 1 "BIOS升级完成1分钟后重启"
    elif [[ -n $PANGU ]] ;then
        UPLOAD_LOG PASS
        if [[ $PANGU = 'sodpangu' ]];then
            NVME_INFO=$(/usr/sbin/nvme list|grep dev|awk '{if ($3 == "INTEL") print $4;else print $3}'|sort|uniq)
            if [[ $VMFLAG -eq 0 ]] && [[ ${ManufactureType} =~ "R4900 G2" ]] || [[ $NVME_INFO =~ ^"P" ]];then
                # 遇到初始化后有些文件0k的问题，非常怀疑这个地方的重启导致的，非正常重启，系统来不及把buffer落盘
                # H3C R4900 G2 BIOS配置导入后，如果执行reboot会导致卡在引导界面
                # Memblaze NVME 固件升级依赖
                service ipmi restart
                nohup bash -c "sleep 300; ipmitool chassis power cycle;" &>> power_cycle.log &
            else
                reboot
            fi
        fi
    elif [[ $run_status == 1 ]] ;then
        echo -e "\033[31m执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL\033[0m"
        Usage
        exit 1
    else
        echo -e "\033[34m执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL\033[0m"
        Usage
    fi
    PRINT "--------------------------------------------------------"
}

DO_CHECK() {
    find_script_list
    for module in $script_list
    do
        #if [[ -n $PANGU ]];then
        #    UPLOAD_PROCESSING "away_checking" $module $NR_PASS $NR_FAIL $total_scripts
        #fi
        source $module
        _check

        if [[ $_check = PASS ]]; then
            PRINT_PASS $module
        else
            PRINT_FAIL $module
        fi
    done

    (( NR_TOTAL=NR_PASS+NR_FAIL ))
    check_done_time=$(date -u '+%Y-%m-%d %H:%M:%S')

    PRINT "--------------------------------------------------------"
    PRINT "<font color='green'>$check_done_time UTC 交付前检测执行完成</font>"
    if [[ -n $PANGU ]] && [[ $NR_PASS -eq 0 || $NR_FAIL -ne 0 ]];then
        PRINT "<font color='red'>执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL</font>"
        UPLOAD_LOG FAIL
        exit 1
    elif [[ -n $PANGU ]] ;then
        PRINT "<font color='green'>执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL </font>"
        WALLE_LLDP_RUN
        if [[ $? -ne 0 ]];then
            UPLOAD_LOG FAIL
        else
            UPLOAD_LOG PASS
        fi
        Done_check
    elif [[ $NR_PASS -eq 0 || $NR_FAIL -ne 0 ]] ;then
        echo -e "\033[31m执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL\033[0m"
        exit 1
    else
        echo -e "\033[34m执行结果: 总共$total_scripts, 实际运行$NR_TOTAL, 成功$NR_PASS, 失败$NR_FAIL\033[0m"
        WALLE_LLDP_RUN
        Done_check
    fi
}

PRINT_INFO $ACTION $ENVFLAG $PANGU $CHANNEL
if [[ $ACTION = install ]]; then
    DO_INIT
else
    DO_CHECK
fi

