#!/bin/bash

root_dir="$(dirname $(readlink -f $(dirname $0)))"
rpm_dir="$root_dir/repo"
os_version=$(uname -r | cut -d - -f 1)
#生产安装标准路径，不能随意修改
LOG_PATH="/var/log/pangea_internal_upgrade/"
LOG_FILE="/var/log/pangea_internal_upgrade/idm_upgrade.log"
build_os_type="Euler"
#出rpm包时的内核版本
rpm_build_kernel_rel="4.19.90-vhulk2103.1.0.h462.eulerosv2r9.aarch64"
cur_kernel_rel=$(uname -r)

function log()
{
    if [ ! -d "$LOG_PATH" ]; then 
        mkdir "$LOG_PATH" 
    fi 

    if [ ! -f "$LOG_FILE" ]; then 
        touch "$LOG_FILE" 
    fi 

    chmod 640 "$LOG_FILE" 
    echo "$(date +"[%Y-%m-%d %H:%M:%S,%N]") [$0:${BASH_LINENO}] [$1]: [IDM] $2" >> ${LOG_FILE}
}

#设置升级进度:覆盖写
function set_process()
{
    cd $root_dir/action
    if [ ! -e "process.txt" ]; then
        echo "$1%" > process.txt
    fi
    chmod 640 process.txt
    echo "$1%" > process.txt
    log "INFO" "install rpm current process $1."
}

#获取升级状态
function get_upgradestatus()
{
    cd $root_dir/action
    if [ ! -e "process.txt" ]; then
        log "ERR" "process.txt not exist"
        echo "failure"
        return 1
    fi
    #增加重试:smartkit升级时，升级脚本转后台操作时，记录升级文件进度和查询进度同时进行，可能会导致process.txt里的文件内容不完整
    #策略对齐:当读取内容不是对应关键字就重试,每次sleep 1s,超时时间10s
    for((i=1;i<=10;i++)); do
        STATUS=`cat process.txt`
        if [ "$STATUS"x = "success%"x ] ||  [ "$STATUS"x = "failure%"x ] || [[ "$STATUS"x = upgrading*%x ]]; then
            #删除%输出,STATUS变量值不变
            echo "${STATUS/\%}"
            log "INFO" "cur upgrade status $STATUS."
            return 0
        fi
        log "INFO" "get progress retry index: $i."
        sleep 1
    done

    echo "failure"
    log "ERR" "get upgrade status failed."
    return 1
}

#1.判断硬件形态
function CheckHardwareForm()
{
    support_product_type=("STL6SPCM" "STL6SPCN" "STL6SPCO" "STL6IEC2F001" "STL6IEX2F001" "STL6SPCY" "STL6SPCX"
        "STL6SPCY55" "STL6SPCY44" "STL6SPCH01" "STL6SPCH02" "STL6SPCY91" "STL6SPCY92")
    support_product_num=${#support_product_type[@]}
    cur_product_type=$(dmidecode -t 2 |grep "Product Name" -i | awk -F ': ' '{print $2}')

    for((i=0;i<$support_product_num;i++)) do
        if [ "$cur_product_type" == "${support_product_type[i]}" ]; then
            break
        fi
    done;

    #循环完支持列表还没找到说明不支持
    if [ "$i" -ge  "$support_product_num" ]; then
        log "ERR" "Current environment is Product Name: ${cur_product_type}, not need base driver."
        #打印到屏显,便于现网定位
        echo "failure"
        echo "reason: Current environment is ${cur_product_type}, not need idm driver."
        exit 1
    fi
}

#2.判断os类型
function CheckOsType()
{
    current_os_type="Euler"
    # 先判断uvp文件是否存在,存在才能获取内容,否则屏显会打印文件不存在,smartkit工具识别不了
    if [ -f "/etc/uvp_version" ];then
        if [ $(cat /etc/uvp_version |wc -l) -ne 0 ];then
            current_os_type="Uvp"
        fi
    fi
    if [ "$build_os_type" != "$current_os_type" ]; then
        log "ERR" "Current os type is $current_os_type, build os type is $build_os_type, not match."
        #打印到屏显,便于现网定位
        echo "failure"
        echo "reason: Current os type is $current_os_type, build os type is $build_os_type, not match."
        exit 1
    fi
}

function handle_not_match()
{
    local kernel_version=$1
    local rpm_name=$2
    local rpm_kernel_ver=$3

    log "ERR" "Current kernel version is $kernel_version, $rpm_name rpm kernel ver is $rpm_kernel_ver, not match."
    log "ERR" "IF YOU INSTALL IT, THE OS WILL CRASH !!!!!!!!"
    #打印到屏显,便于现网定位
    echo "failure"
    echo "reason: Current kernel version is $kernel_version, $rpm_name rpm kernel ver is $rpm_kernel_ver, not match."
    exit 1
}

#3.判断内核版本(v2r9根据小版本做区分，v2r9以后只判断大版本，不检测小版本)
function CheckKernelVersion()
{
    cd $root_dir/repo
    log "INFO" "Check dmi rpms kerner version start."
    cur_kernel_ver=$(uname -r |awk -F 'euleros' '{print $2}')
    rpms_list=$(ls |grep rpm)
    for rpm_pkg in $rpms_list; do
        if [[ "$rpm_build_kernel_rel" =~ "eulerosv2r9" ]]; then
            if [[ "$rpm_build_kernel_rel" != "$cur_kernel_rel" ]] || [[ ! "$cur_kernel_rel" ]]; then
                rpm_name=$(echo $rpm_pkg | cut -d - -f 2)
                handle_not_match "$cur_kernel_rel" "$rpm_name" "$rpm_build_kernel_rel"
            fi
        else
            rpm_pkg_kernel=$(rpm -qi $rpm_pkg |grep kernel |awk '{print $8}' |awk -F 'euleros' '{print $2}')
            if [ "$rpm_pkg_kernel" != "$cur_kernel_ver" -o ! "$cur_kernel_ver" ]; then
                rpm_name=$(echo $rpm_pkg | cut -d - -f 2)
                handle_not_match "$cur_kernel_ver" "$rpm_name" "$rpm_pkg_kernel"
            fi
        fi
    done
    log "INFO" "Check idm rpms kerner version success, end."
    return 0
}

# 脚本入口处理
case $1 in
    "UpgradeVersion")
        ## hardware form check
        CheckHardwareForm
        ## os type check
        CheckOsType
        ## kernel version check
        CheckKernelVersion
        ##update
        cd $root_dir/action 
        #升级动作转后台后返回sucess后就会来查询升级状态,此时process.txt可能在后台还未创建,就会返回失败
        set_process "upgrading 0" 
        setsid sh update_knl.sh 1>/dev/null 2>&1 &
        if [ $? -ne 0 ]; then
            echo "failure"
        else
            echo "success"
        fi
        ;;
    "GetUpgradeStatus")
        get_upgradestatus
        ;;
    *)
        echo "Usage: $0 {UpgradeVersion|GetUpgradeStatus}"
        exit 1
        ;;
esac
