#!/bin/bash
#upgrade在minisystem下的命令脚本
G_SERIOUS_COUNT_FILE=/startup_disk/conf/conf_local/serious_count_cfg
#打印高危提示信息
PRINT_HIGH_RISK_TIPS()
{
    echo "DANGER: You are going to perform a danger command of ignoring upgrade failed steps."
    echo "Suggestion: Contact technical support engineers for help."
    echo "Have you read danger alert message carefully?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" ]; then
        echo "Are you sure you really want to perform the operation?(y/n)"
        read -p "" L_INPUT
        if [ "${L_INPUT}" != "y" ]; then
            return 1
        fi
    else
        return 1
    fi

    return 0
}

#打印高危提示信息
PRINT_HIGH_RISK_TIPS_FOR_SYNC()
{
    echo "DANGER: You are going to perform a danger command of terminating sync flow."
    echo "Suggestion: Contact technical support engineers for help."
    echo "Have you read danger alert message carefully?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" ]; then
        echo "Are you sure you really want to perform the operation?(y/n)"
        read -p "" L_INPUT
        if [ "${L_INPUT}" != "y" ]; then
            return 1
        fi
    else
        return 1
    fi

    return 0
}

ExecDiagnose()
{
    L_CMD=$1

diagnose --auto <<EOF
$L_CMD
quit
EOF

    return 0
}

#打印NO_ALARM_FLAG高危提示信息
PRINT_NO_ALARM_FLAG_HIGH_RISK_INFO()
{
    echo "DANGER: You are going to perform a danger command of creating no-alarm flag on local board."
    echo "Suggestion: Contact technical support engineers for help."
    echo "Have you read danger alert message carefully?(y/n)"
    read -p "" L_INPUT
    if [ "${L_INPUT}" == "y" ]; then
        echo "Are you sure you really want to perform the operation?(y/n)"
        read -p "" L_INPUT
        if [ "${L_INPUT}" != "y" ]; then
            return 1
        fi
    else
        return 1
    fi

    return 0
}

#忽略当前升级失败步骤
IGNORE_CURRENT_UPGRADE_FAILED_STEP()
{
    local nodeId=$1
    #正则表达式，检查用户输入
    dataPattern="^[0-9]{1,2}$"
    [[ "$nodeId" =~ $dataPattern ]] || return 1

    PRINT_HIGH_RISK_TIPS
    if [ "$?" != "0" ]; then
        return 0
    fi

    #本控前端链路冗余检查
    /OSM/bin/upgrade_atom -f IGNORE_CURRENT_UPGRADE_FAILED_STEP -h $nodeId
    local ret=$?
    if [ "$ret" == "0" ]; then
        echo -e "The command was executed successfully."
    elif [ "$ret" == "1" ]; then
        echo -e "Command execution failed: Handle message failed."
    elif [ "$ret" == "2" ]; then
        echo -e "Command execution failed: Target node exception."
    elif [ "$ret" == "3" ]; then
        echo -e "Command execution failed: Not in fail suspended status."
    elif [ "$ret" == "4" ]; then
        echo -e "Command execution failed: No fail step."
    else
        echo -e "Command execution failed."
    fi

    return 0
}

# Query Option suspend flow for upgrading
MANAGE_GAP_OPERATION()
{
    operation=$1
    case "$operation" in
        "get")
            if [ $# -ne 1 ]; then
                echo "Parameter error."
                return 1
            fi
            /OSM/script/product_gap.sh -g
            ;;
        "set")
            if [ $# -ne 3 ]; then
                echo "Parameter error."
                return 1
            fi
            /OSM/script/product_gap.sh -s "$2" "$3"
            exit 0
            ;;
        "force_set")
            if [ $# -ne 3 ]; then
                echo "Parameter error."
                return 1
            fi
            /OSM/script/product_gap.sh -f "$2" "$3"
            exit 0
            ;;
        *)
            echo -e "There is no such operation type: $1."
            return 1
    esac
}

while getopts ":hai:cdjktrlmG:e:fs" options
do
    case "$options" in
        h)
            echo -e "upgrade.sh [-h] | [-a] : It is just a upgrade command. Enter "upgrade.sh -a" to obtain more information."
            exit 0
            ;;
        a)
            echo -e "upgrade.sh -i [nodeId]                      : Ignore current upgrade failed step."
            echo -e "upgrade.sh -c                               : create spare_part flag on local board."
            echo -e "upgrade.sh -d                               : delete spare_part flag on local board."
            echo -e "upgrade.sh -j                               : create no-alarm flag on local board."
            echo -e "upgrade.sh -k                               : delete no-alarm flag on local board."
            echo -e "upgrade.sh -t                               : create no-disk-link-checking flag on local board."
            echo -e "upgrade.sh -r                               : delete no-disk-link-checking flag on local board."
            echo -e "upgrade.sh -l                               : Show sync info."
            echo -e "upgrade.sh -m                               : Call back sync task."
            echo "upgrade.sh -G get                              : Query  the GAP value of all controllers."
            echo "upgrade.sh -G set [node_id] [gap_value]        : Set the GAP value when the controller is offline."
            echo "upgrade.sh -G force_set [node_id] [gap_value]  : Set the GAP value forcibly without checking whether the controller is offline. This operation may cause controller power-on failures."
            echo -e "upgrade.sh -e                               : set serious reboot count, please input 1 2 3 or 4."
            echo -e "upgrade.sh -f                               : show serious reboot count."
            exit 0
            ;;
        i)
            IGNORE_CURRENT_UPGRADE_FAILED_STEP $OPTARG
            if [ "0" != "$?" ] ; then
                echo -e "Parameter error."
                exit 1
            fi
            exit 0
            ;;
        c)
            touch /startup_disk/conf/conf_local/spare_part_flag
            if [ "0" != "$?" ] ; then
                echo -e "fail to create spare_part flag."
                exit 1
            fi
            echo -e "success to create spare_part flag."
            exit 0
            ;;
        d)
            rm -f /startup_disk/conf/conf_local/spare_part_flag
            if [ "0" != "$?" ] ; then
                echo -e "fail to delete spare_part flag."
                exit 1
            fi
            echo -e "success to delete spare_part flag."
            exit 0
            ;;
        j)
            PRINT_NO_ALARM_FLAG_HIGH_RISK_INFO
            if [ "$?" != "0" ]; then
                exit 0
            fi
            touch /OSM/bin/no_alarm_flag_upg
            if [ "0" != "$?" ] ; then
                echo -e "Failed to create no-alarm flag."
                exit 1
            fi
            echo -e "Succeed in creating no-alarm flag, please check alarms manually before performing upgrade command."
            exit 0
            ;;
        k)
            rm -f /OSM/bin/no_alarm_flag_upg
            if [ "0" != "$?" ] ; then
                echo -e "Failed to delete no-alarm flag." 
                exit 1
            fi
            echo -e "Succeed in deleting no-alarm flag."
            exit 0
            ;;
        t)
            touch /tmp/no_check_disk_link_flag
            if [ "0" != "$?" ] ; then
                echo -e "Failed to create no-disk-link-checking flag."
                exit 1
            fi
            echo -e "Succeed in creating no-disk-link-checking flag."
            exit 0
            ;;
        r)
            rm -f /tmp/no_check_disk_link_flag
            if [ "0" != "$?" ] ; then
                echo -e "Failed to delete no-disk-link-checking flag."
                exit 1
            fi
            echo -e "Succeed in deleting no-disk-link-checking flag."
            exit 0
            ;;
        l)
            ExecDiagnose "upg showsyncinfo"
            exit 0
            ;;
        m)
            PRINT_HIGH_RISK_TIPS_FOR_SYNC
            if [ "$?" != "0" ]; then
                exit 0
            fi

            ExecDiagnose "upg cbsync"
            exit 0
            ;;
        G)
            MANAGE_GAP_OPERATION ${@:2}
            exit 0
            ;;
        e)
            echo -e "please input 1 2 3 or 4."
            echo $OPTARG | grep -wE "1|2|3|4" >/dev/null
            if [ "0" != "$?" ] ; then
                echo -e "input error, please input 1 2 3 or 4."
                exit 1
            fi
            rm -f $G_SERIOUS_COUNT_FILE
            echo -e "count is $OPTARG."
            echo $OPTARG > $G_SERIOUS_COUNT_FILE
            exit 0
            ;;
        f)
            if [ -f $G_SERIOUS_COUNT_FILE ];then
                cat $G_SERIOUS_COUNT_FILE
            else
                echo -e "no config file, default is 2."
            fi
            exit 0
            ;;  
        s)
            L_TMP_DIR="/tmp"
            os_cli ${L_TMP_DIR}
            if [ -f "/tmp/log_reset.txt" ];then
                cat /tmp/log_reset.txt
            else
                echo "nvram is cleared, no reset info file!"
            fi
            ;;
        \?)
            echo "usage: upgrade.sh -h"
            exit 1
            ;;
    esac
done

exit 1