#!/bin/bash
#******************************************************************************
# Copyright (C), 2001-2013, Huawei Tech. Co., Ltd.
# File : eCheck.sh
# 1.Environments check, include ipmitool, net-snmp, python version and so on
# 
#  History       :
#  1.Date        : 2016/08/10
#    Modification: Created file
#******************************************************************************

if [ -L ${0} ]
then
    REALFILE=$(readlink ${0})
else
    REALFILE=${0}
fi
CUR_DIR=$(dirname ${REALFILE})

cd "${CUR_DIR}"

LOG_FILE="../../log/eCheck/eCheck.log"

TOOLS_NAME=([1]="ipmitool" [2]="NET-SNMP" [3]="Python")
TOOLS_CMD=([1]="ipmitool -V" [2]="snmpget -V" [3]="python -V")
BASE_VERS=([1]="1.8.11" [2]="5.4.0.0" [3]="2.6.0")

#*****************************************************************************
# Prototype    : check_veriosn
# Description  : check version
# Parameter:
#   input:  $1 current version
#           $2 base version
#           $3 tool name
#   output: NA
# Return Value : 0:OK 1:FAIL
#
#  History        :
#  1.Date         : 2016/08/10
#    Modification : Created function
#
#*****************************************************************************
function check_veriosn()
{
    local current_version=${1}
    local base_version=${2}
    local c_version=($(echo ${current_version} | sed "s/\\./ /g"))
    local b_version=($(echo ${base_version} | sed "s/\\./ /g"))
    
    local length=${#c_version[@]}
    if [ ${length} -lt ${#b_version[@]} ]; then
        length=${#b_version[@]}
    fi
    
    local i=0
    for ((; i <= ${length}; i++)); do
        if [ ${c_version[${i}]} -gt ${b_version[${i}]} ]; then
            printf "%-70s%s\r\n" "${3} version ${current_version}" "Pass"
            return 0;
        elif [ ${c_version[${i}]} -lt ${b_version[${i}]} ]; then
            printf "%-70s%s\r\n" "${3} version ${current_version} needs to be upgraded to ${base_version} or later." "Failed"
            return 1;
        fi
    done
    
    printf "%-50s%s\r\n" "${3} version:${current_version}" "Pass"
    return 0
}

#*****************************************************************************
# Prototype    : get_and_check_version
# Description  : get and check version, like ipmitool, snmp and python
# Parameter:
#   input:  NA
#   output: NA
# Return Value : 0:OK 1:FAIL
#
#  History        :
#  1.Date         : 2016/08/10
#    Modification : Created function
#
#*****************************************************************************
function get_and_check_version()
{
    echo "Environment check starts."
    local ret=0
    local i=1
    local length=${#TOOLS_CMD[@]}
    for ((i = 1; i <= ${length}; i++)); do
        version=$(${TOOLS_CMD[${i}]} 2>&1)
        if [ $? -ne 0 ]; then
            printf "%-70s%s\r\n" "${TOOLS_NAME[${i}]} is not installed. Install ${BASE_VERS[${i}]} or later." "Failed"
            ((ret += 1))
            continue;
        fi
        
        # get tool version
        tool_version=$(echo "${version}" | awk '{print $NF}' | tr -d '\r\n')
        
        # check version
        check_veriosn "${tool_version}" "${BASE_VERS[${i}]}" "${TOOLS_NAME[${i}]}"
        ((ret += $?))
    done
    
    echo "Environment check stops. Problems found: ${ret}."
    return ${ret}
}

get_and_check_version
exit $?
