#!/bin/sh

msg()
{
    msg_code="$1"
    FW_name="$2"
    case "$msg_code" in
    1)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input BMC image name."
        echo "*"
        echo "**************************************************************************"
        ;;
    2)
        echo "**************************************************************************"
        echo "*"
        echo "*  $FW_name doesn't exist and please double check."
        echo "*"
        echo "**************************************************************************"
        ;;
    3)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input BMC user name."
        echo "*"
        echo "**************************************************************************"
        ;;
    4)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input BMC user password."
        echo "*"
        echo "**************************************************************************"
        ;;
    5)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input Current BMC FW Version."
        echo "*"
        echo "**************************************************************************"
        ;;
    6)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input Target BMC FW Version."
        echo "*"
        echo "**************************************************************************"
        ;;
    7)        
        echo "**************************************************************************"
        echo "*"
        echo "*  Direct do FW update"
        echo "*"
        echo "**************************************************************************"
        ;;
    8)
        echo "**************************************************************************"
        echo "*"
        echo "*  Transition Downgrade FW update"
        echo "*"
        echo "**************************************************************************"
        ;;
    9)
        echo "**************************************************************************"
        echo "*"
        echo "*  Transition Upgrade FW update"
        echo "*"
        echo "**************************************************************************"
        ;;
    10)
        echo "**************************************************************************"
        echo "*"
        echo "*  Please input BMC IP."
        echo "*"
        echo "**************************************************************************"
        ;;        
    *)
        echo "Mode is invalid"
        ;;
    esac 

}
check_inputfile()
{
    BMC_IP="$1"
    IMAGE_FILE="$2"
    USER="$3"
    PASSWD="$4"
    CUR_VER="$5"
    TAR_VER="$6" 

    if [ "$#" -ne 6 ]; then
        echo "Usage:"
        echo "$0 <BMC_IP> <IMAGE_FILE> <USER> <PASSWD> <CUR_VER> <TAR_VER>"
    fi
    
    if [ -z "$BMC_IP" ]; then
        msg 10
        exit 1
    fi    

    if [ -z "$IMAGE_FILE" ]; then
        msg 1
        exit 1
    fi
    
    if [ ! -f "$IMAGE_FILE" ]; then
        msg 2 $IMAGE_FILE
        exit 1
    fi
    
    if [ -z "$USER" ]; then
        msg 3
        exit 1
    fi
    
    if [ -z "$PASSWD" ]; then
        msg 4
        exit 1
    fi
    
    if [ -z "$CUR_VER" ]; then
        msg 5
        exit 1
    fi
    
    if [ -z "$TAR_VER" ]; then
        msg 6
        exit 1
    fi
        
    newname="${2%%MS*}MS"
    CONFIG_FILE="config_files/linux/config_$newname.sh"

    if [ -f "$CONFIG_FILE" ]; then
        echo "Found config.sh, loading..."
        . "$CONFIG_FILE"
    else
        echo "Error: $CONFIG_FILE not found!"
    fi

    if [ -z "$transition_type" ]; then
        echo "The transition_type doesn't define in $CONFIG_FILE"
        exit 1

    fi

    echo "BMC IP     : $1"
    echo "Image File : $2"
    echo "User       : $3"
    echo "Old Ver    : $5"
    echo "New Ver    : $6"
    echo ""
}

check_transition_type()
{
    IMAGE_FILE="$2"
    USER="$3"
    PASSWD="$4"
    CUR_VER="$5"
    TAR_VER="$6"    

    if [ "$transition_type" -eq 1 ]; then
        ########################## up & down need to check transition operation ##########################
        
        if [ -z "$transition_fw_up" ] || [ -z "$transition_fw_down" ] || [ -z "$UP_TRANSITION_VER" ] || [ -z "$DOWN_TRANSITION_VER" ]; then
            echo "The $IMAGE_FILE file is incomplete"
            exit 1
        fi

        # Current FW Version == Target FW Version for backward compatible
        if [ "$CUR_VER" -eq "$TAR_VER" ]; then # Direct FW update
            msg 7
            return 0
        fi

        cur_len=${#CUR_VER}
        tar_len=${#TAR_VER}
        # Check the input length of Current & Target Version (6~8 digits)
        if [ "$cur_len" -lt 6 ] || [ "$cur_len" -gt 8 ] || \
           [ "$tar_len" -lt 6 ] || [ "$tar_len" -gt 8 ]; then
            echo "Error: The version format should be 6 or 8 number. Ex: 010705 or 01060101"
            exit 1
        fi
        
        # UP_TRANSITION_VER <= Current FW Version <= DOWN_TRANSITION_VER
        if [ "$CUR_VER" -ge "$UP_TRANSITION_VER" ] && [ "$CUR_VER" -le "$DOWN_TRANSITION_VER" ]; then
            msg 7
            return 0
        fi

        # Current FW Version > DOWN_TRANSITION_VER
        if [ "$CUR_VER" -gt "$DOWN_TRANSITION_VER" ]; then
            if [ "$TAR_VER" -ge "$DOWN_TRANSITION_VER" ]; then
                msg 7
                return 0 # Direct BMC FW update
            fi
            if [ "$TAR_VER" -lt "$DOWN_TRANSITION_VER" ]; then
                msg 8 
                
                if [ ! -f "$transition_fw_down" ]; then
                    echo "$transition_fw_down doesn't exist and please double check"
                    exit 1
                fi
                
                return 2 # Downgrade BMC FW to transition version
            fi
        fi
        
        # Current FW Version < UP_TRANSITION_VER
        if [ "$CUR_VER" -lt "$UP_TRANSITION_VER" ]; then
            if [ "$TAR_VER" -le "$UP_TRANSITION_VER" ]; then
                msg 7 
                return 0  # Direct BMC FW update
            fi
            if [ "$TAR_VER" -gt "$UP_TRANSITION_VER" ]; then
                msg 9

                if [ ! -f "$transition_fw_up" ]; then
                    echo "$transition_fw_up doesn't exist and please double check"
                    exit 1
                fi

                return 1 # Upgrade BMC FW to transition version
            fi
        fi


    elif [ "$transition_type" -eq 2 ]; then
        ########################## only down need to check transition operation ##########################
        echo "Transition Type is 2"
        
        if [ -z "$transition_fw_down" ] || [ -z "$DOWN_TRANSITION_VER" ]; then
            echo "The config.sh file is incomplete"
            exit 1
        fi
        
        # Current FW Version == Target FW Version for backward compatible
        if [ "$CUR_VER" -eq "$TAR_VER" ]; then 
            msg 7
            return 0  # Direct BMC FW update
        fi
        old_len=${#CUR_VER}
        new_len=${#TAR_VER}
        # Check the input length of Current & Target Version (6~8 digits)
        if [ "$old_len" -lt 6 ] || [ "$old_len" -gt 8 ] || \
           [ "$new_len" -lt 6 ] || [ "$new_len" -gt 8 ]; then
            echo "Error: The version format should be 6 or 8 number. Ex: 010705 or 01060101"
            exit 1
        fi
        
        # Current FW Version >= DOWN_TRANSITION_VER

        if [ "$CUR_VER" -ge "$DOWN_TRANSITION_VER" ]; then

            if [ "$TAR_VER" -ge "$DOWN_TRANSITION_VER" ]; then
                msg 7
                return 0  # Direct BMC FW update
            fi
            if [ "$TAR_VER" -lt "$DOWN_TRANSITION_VER" ]; then
                msg 8

                if [ ! -f "$transition_fw_down" ]; then
                    echo "$transition_fw_down doesn't exist and please double check"
                    exit 1
                fi
                
                return 2 # Downgrade BMC FW to transition version
            fi
        else
            msg 7
            return 0 # Direct BMC FW update
        fi
    fi
}
start_bmcfwupdate()
{
    BMC_IP="$1"
    IMAGE_FILE="$2"
    USER="$3"
    PASSWD="$4"
    ret="$7"   # Get return value
    TIMEOUT=60  # Ping timeout
    INTERVAL=1  # Ping BMC every second
    COUNT=0    
    PING_SUCCESS=0
    updatebmc="updatebmc"
    UP_Down="Upgrade"
    if echo "$IMAGE_FILE" | grep -q "B401MS"; then
        updatebmc="UpdateCmm"
    fi


    if [ "$ret" -eq 1 ]; then # Upgrade BMC FW to transition version
        UP_Down="Upgrade"
        echo "$UP_Down BMC FW to transition version..."
        ./saa_1.4.0_Linux_x86_64/saa -i "$BMC_IP" -u "$USER" -p "$PASSWD" -c "$updatebmc" --file "$transition_fw_up" 
    
    elif [ "$ret" -eq 2 ]; then # Downgrade BMC FW to transition version
        UP_Down="Downgrade"
        echo "$UP_Down BMC FW to transition version..."
        ./saa_1.4.0_Linux_x86_64/saa -i "$BMC_IP" -u "$USER" -p "$PASSWD" -c "$updatebmc" --file "$transition_fw_down"   
    fi

    while [ $COUNT -lt $TIMEOUT ]; do
        if ping -c 1 -W 1 "$BMC_IP" > /dev/null 2>&1; then
            echo "Ping successful!"
            PING_SUCCESS=1
            break
        fi
        sleep $INTERVAL
        COUNT=$((COUNT + INTERVAL))
    done

    if [ $PING_SUCCESS -eq 0 ]; then
        echo "Ping failed after $TIMEOUT seconds."
        exit 1
    fi
    
    echo "$UP_Down BMC FW to target version..."

    ./saa_1.4.0_Linux_x86_64/saa -i "$BMC_IP" -u "$USER" -p "$PASSWD" -c "$updatebmc" --file "$IMAGE_FILE" 


    exit 0
}
main()
{
    echo ""
    echo "================================================="
    echo " BMC Transition Firmware Update Script"
    echo " File    : flash_linux.sh"
    echo " Version : v00.00.01"
    echo " Date    : 2026-01-07"
    echo "================================================="
    echo ""

    BMC_IP="$1"
    IMAGE_FILE="$2"
    USER="$3"
    PASSWD="$4"
    CUR_VER="$5"
    TAR_VER="$6"
    check_inputfile "$@"
    check_transition_type "$@"
    ret=$?   # Get return value

    start_bmcfwupdate "$BMC_IP" "$IMAGE_FILE" "$USER" "$PASSWD" "$CUR_VER" "$TAR_VER" "$ret"
}

main "$@"
