#!/bin/sh
#日志级别
ERROR="ERROR"
INFO="INFO"
WARNING="WARNING"

#设置颜色
bgcred=1

#通过全局变量更改颜色
c_notify=$bgcred
c_error=$bgcred

#function: 写日志
Log() {
    log_level=$1
    msg=$2
    if [ ! -f "/var/log/qaxsafeinst.log" ]; then
        touch /var/log/qaxsafeinst.log
    fi
    local datefmt="$(date "+%Y-%m-%d %H:%M:%S")"
    local log_info="$datefmt $log_level: $msg"
    echo "$log_info" >>/var/log/qaxsafeinst.log
}

#设置常量
LINUX_SERVER=1
LINUX_CLIENT=2
LINUX_CLIENT_SERVER=3
LINUX_NO_UI=0
LINUX_UI=1

#设置单元测试开关
UNIT_TEST_SWITCH=0

#设置全局变量
filename=$0
argc=$#
arg=$1
is_susport_chinese=0
server_ip=""     #控制中心IP地址
server_port=""   #控制中心端口号
SP_FLAG="______" #特殊字符替换空格
exc_filepath=""  #安装包完整路径地址
intime="$(date '+%s')"

#本地中英文提示
local_index=0
eval local_chinese$local_index="命令行参数不对,请重新运行脚本"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="输入有误,程序退出,请重新运行"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="您当前系统暂且不支持自动安装部署！"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="网络异常，请检查网络后重新运行！"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="很抱歉,您当前的系统暂不支持..."
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="下载失败"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="正在更新..."
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="安装失败！"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="警告:您需要在root用户下运行安装脚本!"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="您已经安装过该程序,执行安装失败!"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="安装失败，没有足够的空间!"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="安装完成!"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="按回车即可开始自动安装，如识别错误，请键入对应芯片架构、安装包管理器的数字，按回车进行安装："
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="##\ 当前系统信息:"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="##\ 系统位数：64位系统"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="##\ 系统位数：32位系统"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="##\ 安装包管理器：deb"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="##\ 安装包管理器：rpm"
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="按回车即可开始自动安装，如识别错误，请按照实际情况键入对应的终端类型的数字，按回车进入下一步："
local_index=$(expr $local_index + 1)
eval local_chinese$local_index="请键入对应芯片架构、安装包管理器的数字，按回车进行安装："

local_index=0
eval local_english$local_index="The\ command\ line\ argument\ is\ incorrect.\ Please\ run\ the\ script\ again!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="you\ inputed\ error\ arguments,\ and\ the\ program\ exited,\ please\ run\ again!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="Your\ current\ system\ does\ not\ be\ supported\ by\ the\ automatic\ installation!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="Network\ exception,\ please\ run\ again\ after\ checking\ network\ condition!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="Sorry,\ your\ current\ system\ is\ not\ supported..."
local_index=$(expr $local_index + 1)
eval local_english$local_index="downloaded\ error!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="updating\ now..."
local_index=$(expr $local_index + 1)
eval local_english$local_index="installed\ error!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="warning:you\ should\ run\ this\ script\ in\ the\ root\ environment"
local_index=$(expr $local_index + 1)
eval local_english$local_index="you\ had\ installed\ this\ software,\ and\ installed\ error!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="installation\ fails,\ there\ is\ not\ enough\ space!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="The\ installation\ is\ complete!"
local_index=$(expr $local_index + 1)
eval local_english$local_index="Press\ enter\ to\ start\ the\ automatic\ installation.\ If\ identified\ error,\ please\ type\ the\ number\ of\ the\ installation\ package\ manager,\ and\ press\ enter\ to\ install"
local_index=$(expr $local_index + 1)
eval local_english$local_index="##\ Current\ system\ information:"
local_index=$(expr $local_index + 1)
eval local_english$local_index="##\ System\ bit:\ 64\ bit\ system"
local_index=$(expr $local_index + 1)
eval local_english$local_index="##\ System\ bit:\ 32\ bit\ system"
local_index=$(expr $local_index + 1)
eval local_english$local_index="##\ package\ manager:deb"
local_index=$(expr $local_index + 1)
eval local_english$local_index="##\ package\ manager:rpm"
local_index=$(expr $local_index + 1)
eval local_english$local_index="Press\ enter\ to\ start\ the\ automatic\ installation.\ If\ identified\ error,\ please\ type\ the\ number\ of\ the\ corresponding\ terminal\ type\ according\ to\ the\ actual\ situation,\ and\ press\ enter\ to\ enter\ the\ next\ step:"
local_index=$(expr $local_index + 1)
eval local_english$local_index="please\ type\ the\ number\ of\ the\ installation\ package\ manager,\ and\ press\ enter\ to\ install"

#本机系统信息记录表, 0 -- "osname", 1 -- "version", 2 -- "release_info"
#Os_Name=(["osname"]="" ["version"]="" ["release_info"]="")
Os_Name_index=0
eval Os_Name$Os_Name_index=""
Os_Name_index=$(expr $Os_Name_index + 1)
eval Os_Name$Os_Name_index=""
Os_Name_index=$(expr $Os_Name_index + 1)
eval Os_Name$Os_Name_index=""

#本地所识别的系统名称
sys_name_index=0
eval sys_name$sys_name_index="centos"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="ubuntu"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="suse"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="deepin"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="neokylin"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="fedora"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="xenserver"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="kylin"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="isoft"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="redhat"
sys_name_index=$(expr $sys_name_index + 1)
eval sys_name$sys_name_index="uos"

#function:  判断系统是否支持中文
isSupportChinese() {
    local tty_content="$(tty 2>/dev/null | grep -i "tty")"
    if [ "$tty_content" != "" ]; then
        return 0
    fi

    local byte_command="$(locale -a 2>/dev/null | grep -i "zh_cn.utf8")"
    local connect_command="$(export 2>/dev/null | grep -i "LANG" | grep -i "zh_cn")"
    if [ "$byte_command" != "" ] && [ "$connect_command" != "" ]; then
        return 1
    else
        return 0
    fi
}

#function:  系统如果支持中文,就中文输出,否则英文输出
printtext() {
    local index_i
    if [ $is_susport_chinese -eq 1 ]; then
        # 输出中文信息
        if [ $# -eq 2 ]; then
            cecho $1 $2
        else
            echo $1
        fi
    else
        # 输出英文信息
        for index_i in $(seq 0 $local_index); do
            eval temp_data=\$local_chinese$index_i
            if [ "$temp_data" = "$1" -a $# -eq 2 ]; then
                eval temp_data_eng=\$local_english$index_i
                cecho "${temp_data_eng}" $2
            elif [ "$temp_data" = "$1" -a $# -eq 1 ]; then
                eval echo "\$local_english$index_i"
                return
            fi
        done
    fi
}

#function:  判断脚本是否运行在Root权限下
isExcuteinRootAuth() {
    local id_num=$(id -u 2>/dev/null)
    if [ "$id_num" = "" ] && [ "$USER" != "root" ]; then
        # esxi系统
        printtext "警告:您需要在root用户下运行安装脚本!" $c_error
        exit 1
    elif [ $id_num -ne 0 ]; then
        printtext "警告:您需要在root用户下运行安装脚本!" $c_error
        exit 1
    fi
}

#function:  获取硬件平台的位数, 1 == 64, 0 == 32
getMachineBit() {
    local bitcontent="$(uname -a)"

    if [ "$(echo $bitcontent | grep "mips64")" != "" ] ||
        [ "$(echo $bitcontent | grep "aarch64")" != "" ] ||
        [ "$(echo $bitcontent | grep "x86_64")" != "" ] ||
        [ "$(echo $bitcontent | grep "sw64")" != "" ]; then
        return 1
    fi

    #增加了对i386、i586和i686的检查
    if [ "$(echo $bitcontent | grep "x86")" != "" ] ||
        [ "$(echo $bitcontent | grep "i386")" != "" ] ||
        [ "$(echo $bitcontent | grep "i586")" != "" ] ||
        [ "$(echo $bitcontent | grep "i686")" != "" ]; then
        return 0
    fi

    return 1
}

#function:  获取硬件平台CPU架构
getMachineArc() {
    local machine_content="$(uname -a)"
    if [ "$(echo "$machine_content" | grep "x86")" != "" ]; then
        machine_arc="x86"
    elif [ "$(echo "$machine_content" | grep "i386")" != "" ]; then
        machine_arc="x86"
    elif [ "$(echo "$machine_content" | grep "i686")" != "" ]; then
        machine_arc="x86"
    elif [ "$(echo "$machine_content" | grep "mips")" != "" ]; then
        machine_arc="mips"
    elif [ "$(echo "$machine_content" | grep "aarch")" != "" ]; then
        machine_arc="aarch"
    elif [ "$(echo "$machine_content" | grep "sw")" != "" ]; then
        machine_arc="sw"
    fi
}

#function:  获取操作系统位数，区别于机器位数
getOsBit() {
    #获取ls指令的位置（某些系统自带了alias定义）
    local ls_ret="$(\which ls 2>/dev/null)"
    if [ "$ls_ret" = "" ]; then
        ls_ret="$(readlink /bin/ls 2>/dev/null)"
    fi
    local file_ls_content="$(file "$ls_ret" 2>/dev/null)"
    if [ "$(echo "$file_ls_content" | grep "64-bit")" != "" ]; then
        return 1
    elif [ "$(echo "$file_ls_content" | grep "32-bit")" != "" ]; then
        return 0
    else
        return 1
    fi
}

#function:  根据当前安装脚本文件的名称，获得ip和端口号
getServerIpAndPort() {
    local temp_filename=${filename#*(}
    local ip_and_port=${temp_filename%)*}
    server_ip=${ip_and_port%_*}
    server_port=${ip_and_port##*_}
    if [ "$(echo $ip_and_port | grep "\[" | grep "\]")" != "" ]; then
        server_ip="$(echo $server_ip | sed 's/_/:/g')"
    fi
}

#function:  根据文件名称判断客户端类型
getClientUI_by_filename() {
    if [ "$(echo $filename | grep "linuxc")" ]; then
        return ${LINUX_UI}
    elif [ "$(echo $filename | grep "linuxs")" ]; then
        return ${LINUX_NO_UI}
    else
        return -1
    fi
}

#function:  排除一定不支持的系统和平台
isSupportOsandPlatform() {
    getMachineBit
    Machine_Bit=$?

    getOsBit
    Os_Bit=$?

    getMachineArc
    if [ "${machine_arc}" != "x86" -a ${Machine_Bit} -eq 0 -a ${Os_Bit} -eq 0 ]; then
        printtext "警告:安装包不支持此32位平台!" ${c_error}
        exit 1
    fi
}

#function:  根据客户端类型，得到主界面显示的系统数组
init_supported_os_matrix() {
    array_len=0
    if [ ${client_ui} -eq ${LINUX_NO_UI} ]; then
        #LINUX_SERVER
        eval osType_array$array_len="x86_64\ deb"
        eval pkg_array$array_len="360safe-for-x64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_64\ rpm"
        eval pkg_array$array_len="360safe-for-x64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="aarch\ deb"
        eval pkg_array$array_len="360safe-for-arm64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="aarch\ rpm"
        eval pkg_array$array_len="360safe-for-arm64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_32\ deb"
        eval pkg_array$array_len="360safe-for-x86.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_32\ rpm"
        eval pkg_array$array_len="360safe-for-x86.rpm"
    elif [ ${client_ui} -eq ${LINUX_UI} ]; then
        #LINUX CLIENT
        eval osType_array$array_len="aarch\ deb"
        eval pkg_array$array_len="360safe-for-arm64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="aarch\ rpm"
        eval pkg_array$array_len="360safe-for-arm64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="mips\ deb"
        eval pkg_array$array_len="360safe-for-mips64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="mips\ rpm"
        eval pkg_array$array_len="360safe-for-mips64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="sw\ deb"
        eval pkg_array$array_len="360safe-for-sw64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="sw\ rpm"
        eval pkg_array$array_len="360safe-for-sw64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_64\ deb"
        eval pkg_array$array_len="360safe-for-x64.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_64\ rpm"
        eval pkg_array$array_len="360safe-for-x64.rpm"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_32\ deb"
        eval pkg_array$array_len="360safe-for-x86.deb"
        array_len=$(expr $array_len + 1)
        eval osType_array$array_len="x86_32\ rpm"
        eval pkg_array$array_len="360safe-for-x86.rpm"
    else
        #未处理
        return
    fi
}

#function:  获取h3c系统的名称和版本信息
getH3CInfo() {
    if [ ! -f "/etc/h3c_cas_cvk-version" ]; then return 0; fi
    local version_info="$(cat /etc/h3c_cas_cvk-version 2>/dev/null | awk '{print $2}' | head -n 1)"
    local version="$(expr substr "$version_info" 2 1)"
    if [ "$version" = "2" ] || [ "$version" = "3" ]; then
        eval Os_Name0="h3c"
        eval Os_Name1="$version"
        eval Os_name2="$version"
        return 1
    else
        printtext "很抱歉,您当前的系统暂不支持..."
        exit 1
    fi
}

#function:  获取esx | esxi系统的名称和版本信息
getESXiInfo() {
    # is esx 4 ?
    if [ -f "/etc/vmware-release" ]; then
        local osname="$(cat /etc/vmware-release 2>/dev/null | awk '{print $2}' | head -n 1)"
        local version_info="$(cat /etc/vmware-release 2>/dev/null | awk '{print $3}' | head -n 1)"
        local version="${version_info:0:1}"
        if [ "$osname" = "ESX" ] && [ "$version" = "4" ]; then
            eval Os_Name0="VMware\ ESX"
            eval Os_Name1="$version"
            eval Os_name2="$version"
            return 1
        else
            printtext "很抱歉,您当前的系统暂不支持..."
            exit 1
        fi
    fi

    # is esxi 5 or 6 ?
    local osname="$(uname -a | awk '{print $1}' | head -n 1)"
    if [ "$osname" = "VMkernel" ]; then
        local version_info="$(uname -a | awk '{print $3}' | head -n 1)"
        local version="${version_info:0:1}"
        if [ "$version" = "5" -o "$version" = "6" ]; then
            eval Os_Name0="VMware\ ESXi"
            eval Os_Name1="$version"
            eval Os_name2="$version"
            return 1
        else
            printtext "很抱歉,您当前的系统暂不支持..."
            exit 1
        fi
    fi

    return 0
}

#function:  判断是否通过lsb_release命令获取系统名称和系统版本信息
useCmd_lsb_release() {
    #不使用lsb_release命令获取系统信息的返回0（速度慢或其它原因）
    #mipsel-x86不使用lsb_release
    local machine_arc
    getMachineArc
    if [ "$machine_arc" = "mips" -a $Os_Bit -eq 0 ]; then
        return 0
    fi
    #没有相应指令的不使用lsb_release
    local lsb_release_ret="$(which lsb_release 2>/dev/null)"
    if [ "$lsb_release_ret" = "" ]; then
        return 0
    else
        return 1
    fi
}

#function:  特殊处理/etc/issue | /etc/issue.net | *-release文件
needSpecialProcessInfoFileforxenserver() {
    local release_info_oneword
    local index_i
    for index_i in $(seq 0 $index_idx); do
        eval release_info_oneword="\$osinfo_array$index_i"
        local temp_version_domain="$(echo "$release_info_oneword" | grep '\.')"
        if [ "$temp_version_domain" != "" ]; then
            eval Os_Name1="$release_info_oneword"
            eval Os_Name2="$release_info_oneword"
            break
        fi
    done
}

#function:  数组化/etc/issue | /etc/issue.net文件中的信息
getOsInfoArrayWithIssueFile() {
    local file_text=""
    if [ -f "/etc/issue" ]; then
        file_text="$(cat /etc/issue |
            sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' |
            sed 's/ /'$SP_FLAG'/g' |
            sed 's/\\\n/ /g')"
    elif [ -f "/etc/issue.net" ]; then
        file_text="$(cat /etc/issue.net |
            sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' |
            sed 's/ /'$SP_FLAG'/g' |
            sed 's/\\\n/ /g')"
    else
        return
    fi

    osinfo="'$file_text'"
    local index_i=0
    local file_text_oneline
    local file_text_line_list
    for file_text_oneline in $file_text; do
        eval file_text_line_list$index_i="'$file_text_oneline'"
        index_i=$(expr $index_i + 1)
    done
    index_i=$(expr $index_i - 1)

    # 目前所支持的系统中，在第一行中都能找到所需要的版本信息
    eval file_text_oneline="\$file_text_line_list0"
    file_text_oneline="$(echo $file_text_oneline | sed 's/'$SP_FLAG'/ /g')"

    index_idx=0
    local everyword_in_line
    for everyword_in_line in $file_text_oneline; do
        eval osinfo_array$index_idx="'$everyword_in_line'"
        index_idx=$(expr $index_idx + 1)
    done
    index_idx=$(expr $index_idx - 1)
}

#function:  处理/etc/issue | /etc/issue.net文件
processIssueFile() {
    local release_info_oneword
    local index_i
    for index_i in $(seq 0 $index_idx); do
        eval release_info_oneword="\$osinfo_array$index_i"
        local temp_version_domain="$(echo $release_info_oneword | sed 's/\.//g' | sed 's/-[0-9][a-z]$//g')"
        if [ "$(echo $temp_version_domain | sed 's/[0-9]//g')" = "" ]; then
            eval Os_Name1="$release_info_oneword"
            eval Os_Name2="$release_info_oneword"
            break
        fi
    done
}

#function:  数组化lsb-release & os-release文件中的信息
getOsInfoArrayWithLsbOsReleaseFile() {
    #注意顺序：先lsb-release再os-release
    local file_text_lsb=""
    if [ -f "/etc/lsb-release" ]; then
        file_text_lsb="$(cat /etc/lsb-release |
            sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' |
            sed 's/ /'$SP_FLAG'/g' |
            sed 's/=/ /g' |
            sed 's/\\\n/ /g')"
    fi
    local file_text_os=""
    if [ -f "/etc/os-release" ]; then
        file_text_os="$(cat /etc/os-release |
            sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' |
            sed 's/ /'$SP_FLAG'/g' |
            sed 's/=/ /g' |
            sed 's/\\\n/ /g')"
    fi
    if [ "$file_text_lsb" = "" -a "$file_text_os" = "" ]; then
        return
    fi
    local file_text=""
    if [ "$file_text_lsb" = "" ]; then
        file_text="$file_text_os"
    elif [ "$file_text_os" = "" ]; then
        file_text="$file_text_lsb"
    else
        file_text="$file_text_lsb $file_text_os"
    fi
    osinfo="'$file_text'"
    index_idx=0
    local release_info_oneword
    for release_info_oneword in $file_text; do
        eval osinfo_array$index_idx="'$release_info_oneword'"
        index_idx=$(expr $index_idx + 1)
    done
    index_idx=$(expr $index_idx - 1)
}

#function:  处理lsb-release & os-release文件
processLsbOsReleaseFile() {
    local release_info_oneword
    local index_i
    for index_i in $(seq 0 $index_idx); do
        eval release_info_oneword="\$osinfo_array$index_i"
        if [ "$release_info_oneword" = "distrib_release" -o "$release_info_oneword" = "version_id" ]; then
            index_i=$(expr $index_i + 1)
            local temp_os_version
            eval temp_os_version="\$Os_Name1"
            if [ "$temp_os_version" = "" ]; then
                eval temp_os_version="\$osinfo_array$index_i"
                temp_os_version="$(echo "$temp_os_version" | sed 's/'$SP_FLAG'/ /g' | sed 's/"//g' | sed 's/-[0-9][a-z]$//g')"
                eval Os_Name1="'$temp_os_version'"
            fi
        fi

        if [ "$release_info_oneword" = "distrib_description" -o "$release_info_oneword" = "pretty_name" ]; then
            index_i=$(expr $index_i + 1)
            local temp_os_release=""
            eval temp_os_release="\$Os_Name2"
            if [ "$temp_os_release" = "" ]; then
                eval temp_os_release="\$osinfo_array$index_i"
                temp_os_release="$(echo "$temp_os_release" | sed 's/'$SP_FLAG'/ /g')"
                eval Os_Name2="'$temp_os_release'"
            fi
        fi
    done
}

#function:  数组化/etc/*-release文件中的信息
getOsInfoArrayWithOtherReleaseFile() {
    local list_etc_file="/etc/"
    local index_i=0
    local searchfileDict

    for item in $(ls $list_etc_file | grep -i "release"); do
        if [ ! -d "$list_etc_file/$item" ]; then
            if [ "$(echo "$item" | grep -i "\-release$")" != "" ] && [ "$item" != "os-release" ] && [ "$item" != "lsb-release" ]; then
                eval searchfileDict$index_i="$item"
                index_i=$(expr $index_i + 1)
            fi
        fi
    done
    index_i=$(expr $index_i - 1)

    index_idx=0
    local index_j
    for index_j in $(seq 0 $index_i); do
        eval item="\$searchfileDict$index_j"
        local file_text="$(cat "$list_etc_file$item" | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')"
        osinfo="$osinfo'$file_text'"
        local release_info_oneword
        for release_info_oneword in $file_text; do
            eval osinfo_array$index_idx="'$release_info_oneword'"
            index_idx=$(expr $index_idx + 1)
        done
    done
    index_idx=$(expr $index_idx - 1)
}

#function:  处理*-release文件
processOtherReleaseFile() {
    local release_info_oneword
    local index_i
    for index_i in $(seq 0 $index_idx); do
        eval release_info_oneword="\$osinfo_array$index_i"
        local temp_version_domain="$(echo $release_info_oneword | sed 's/\.//g' | sed 's/-[0-9][a-z]$//g')"
        if [ "$(echo $temp_version_domain | sed 's/[0-9]//g')" = "" ]; then
            eval Os_Name1="$release_info_oneword"
            eval Os_Name2="$release_info_oneword"
            break
        fi
    done
}

#function:  数组化执行指令lsb_release -a后获取的信息
getOsInfoArrayWithLsbReleaseCmd() {
    local lsb_release_info="$(lsb_release -a 2>/dev/null |
        sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' |
        sed 's/ /'$SP_FLAG'/g' |
        sed 's/\\\n/ /g')"
    osinfo="'$lsb_release_info'"
    index_idx=0
    local release_info_oneword
    for release_info_oneword in $lsb_release_info; do
        eval osinfo_array$index_idx="'$release_info_oneword'"
        index_idx=$(expr $index_idx + 1)
    done
    index_idx=$(expr $index_idx - 1)
}

#function:  通过lsb_release -a获取系统名称和系统版本信息
getOsInfoWithLsbReleaseCmd() {
    #获取系统版本、release信息
    local release_info_oneword
    local index_i
    for index_i in $(seq 0 $index_idx); do
        eval release_info_oneword="\$osinfo_array$index_i"

        if [ "$release_info_oneword" = "release:" ]; then
            index_i=$(expr $index_i + 1)
            eval release_info_oneword="\$osinfo_array$index_i"
            release_info_oneword="$(echo $release_info_oneword | sed 's/'$SP_FLAG'/ /g' | sed 's/-[0-9][a-z]$//g')"
            eval Os_Name1="'$release_info_oneword'"
        fi

        if [ "$release_info_oneword" = "description:" ]; then
            index_i=$(expr $index_i + 1)
            eval release_info_oneword="\$osinfo_array$index_i"
            release_info_oneword="$(echo $release_info_oneword | sed 's/'$SP_FLAG'/ /g')"
            eval Os_Name2="'$release_info_oneword'"
        fi
    done
}

#function:  查找字串中是否包含有系统名称
findOsNameinArrayInfo() {
    #获取系统名称
    local index_i
    for index_i in $(seq 0 $sys_name_index); do
        local os_item
        eval os_item="\$sys_name$index_i"
        if [ "$(echo "$1" | grep "$os_item")" != "" ]; then
            if [ "$os_item" = "neokylin" ]; then
                #如果是麒麟系统, 则需要判断是否为server版本，还是desktop版本
                if [ "$(echo "$1" | grep -i "server")" != "" ]; then
                    os_item=$os_item"_server"
                else
                    os_item=$os_item"_desktop"
                fi
            elif [ "$os_item" = "kylin" ]; then
                #如果是银河麒麟系统, 显示时需要在其名称前添加"yh"字串
                os_item="yh"$os_item
            else
                #其它对获取的系统名称有特殊处理的均可添加于此处
                os_item="$os_item"
            fi
            eval Os_Name0="$os_item"
            return 0
        fi
        if [ "$os_item" = "redhat" ] && [ "$(echo "$1" | grep "red")" != "" ] && [ "$(echo "$1" | grep "hat")" != "" ]; then
            eval Os_Name0="$os_item"
            return 0
        fi
    done
    return 1
}

#function:  获取系统名称，构建分类基础
getOsNameInfo() {
    local index_idx
    local osinfo_array
    #判断是否使用lsb_release命令
    useCmd_lsb_release
    if [ $? -eq 1 ]; then
        #通过lsb_release -a获取系统名称
        getOsInfoArrayWithLsbReleaseCmd
        findOsNameinArrayInfo "$osinfo"
        if [ $? -eq 0 ]; then return 0; fi
    fi
    #通过读取记录系统信息文件内容的方法获取系统名称
    getOsInfoArrayWithOtherReleaseFile
    findOsNameinArrayInfo "$osinfo"
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithLsbOsReleaseFile
    findOsNameinArrayInfo "$osinfo"
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithIssueFile
    findOsNameinArrayInfo "$osinfo"
    if [ $? -eq 0 ]; then return 0; fi

    #如若此四条规则无法正确获取系统名称，其他规则可添加于此处
    return 1
}

#function:  判断是否正确获取系统版本号和Release信息
getVersionandReleaseInfoSuc() {
    local os_version_info=""
    eval os_version_info="\$Os_Name1"
    local os_release_info=""
    eval os_release_info="\$Os_Name2"
    if [ "$os_version_info" = "" -o "$os_release_info" = "" ]; then
        return 1
    else
        return 0
    fi
}

#分情况获取系统版本信息

#function:  获取系统版本信息通用方法
getOsVersionandReleaseInfo_for_common() {
    local osinfo
    local index_idx
    local osinfo_array
    #判断是否使用lsb_release命令
    useCmd_lsb_release
    if [ $? -eq 1 ]; then
        #通过lsb_release -a获取系统名称
        getOsInfoArrayWithLsbReleaseCmd
        getOsInfoWithLsbReleaseCmd
        getVersionandReleaseInfoSuc
        if [ $? -eq 0 ]; then return 0; fi
    fi
    #通过读取记录系统信息文件内容的方法获取系统名称
    getOsInfoArrayWithOtherReleaseFile
    processOtherReleaseFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithLsbOsReleaseFile
    processLsbOsReleaseFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithIssueFile
    processIssueFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    #如若此四条规则无法正确获取系统名称，其他规则可添加于此处
    return 1
}

#function:  ubuntu获取系统版本信息
getOsVersionandReleaseInfo_for_ubuntu() {
    getOsVersionandReleaseInfo_for_common
}

#function:  centos获取系统版本信息
getOsVersionandReleaseInfo_for_centos() {
    getOsVersionandReleaseInfo_for_common
}

#function:  redhat获取系统版本信息
getOsVersionandReleaseInfo_for_redhat() {
    getOsVersionandReleaseInfo_for_common
}

#function:  suse获取系统版本信息
getOsVersionandReleaseInfo_for_suse() {
    getOsVersionandReleaseInfo_for_common
}

#function:  xenserver获取系统版本信息
getOsVersionandReleaseInfo_for_xenserver() {
    local osinfo
    local index_idx
    local osinfo_array
    #判断是否使用lsb_release命令
    useCmd_lsb_release
    if [ $? -eq 1 ]; then
        #通过lsb_release -a获取系统名称
        getOsInfoArrayWithLsbReleaseCmd
        getOsInfoWithLsbReleaseCmd
        getVersionandReleaseInfoSuc
        if [ $? -eq 0 ]; then return 0; fi
    fi
    #通过读取记录系统信息文件内容的方法获取系统名称
    getOsInfoArrayWithOtherReleaseFile
    needSpecialProcessInfoFileforxenserver
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithLsbOsReleaseFile
    processLsbOsReleaseFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithIssueFile
    needSpecialProcessInfoFileforxenserver
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    return 1
}

#function:  arm-64位中标麒麟桌面版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_desktop_arm64_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-64位中标麒麟服务器版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_server_mips_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  x86-64位中标麒麟服务器版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_server_x86_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-32位中标麒麟桌面版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_desktop_mips_32bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-64位中标麒麟桌面版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_desktop_mips_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  x86-64位中标麒麟桌面版获取系统版本信息
getOsVersionandReleaseInfo_for_neokylin_desktop_x86_64bit() {
    local osinfo
    local index_idx
    local osinfo_array

    getOsInfoArrayWithOtherReleaseFile
    processOtherReleaseFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithLsbOsReleaseFile
    processLsbOsReleaseFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    getOsInfoArrayWithIssueFile
    processIssueFile
    getVersionandReleaseInfoSuc
    if [ $? -eq 0 ]; then return 0; fi

    return 1
}

#function:  uos-64位获取系统版本信息
getOsVersionandReleaseInfo_for_UOS_64bit(){
    getOsVersionandReleaseInfo_for_common
}

#function:  uos-32位获取系统版本信息
getOsVersionandReleaseInfo_for_UOS_32bit(){
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-64位银河麒麟获取系统版本信息
getOsVersionandReleaseInfo_for_yhkylin_mips_64bit(){
    getOsVersionandReleaseInfo_for_common
    if [ "$(echo "$osinfo" | grep -i "server")" != "" ]; then
        eval Os_Name0="yhkylin_server"
    else
        eval Os_Name0="yhkylin_desktop"
    fi
}

#function:  arm-64位银河麒麟获取系统版本信息
getOsVersionandReleaseInfo_for_yhkylin_aarch_64bit() {
    getOsVersionandReleaseInfo_for_common

    if [ "$(echo "$osinfo" | grep -i "server")" != "" ]; then
        eval Os_Name0="yhkylin_server"
    else
        eval Os_Name0="yhkylin_desktop"
    fi
}

#function:  mips-32位deepin获取系统版本信息
getOsVersionandReleaseInfo_for_deepin_mips_32bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-64位deepin获取系统版本信息
getOsVersionandReleaseInfo_for_deepin_mips_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  x86-64位deepin获取系统版本信息
getOsVersionandReleaseInfo_for_deepin_x86_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function:  mips-64位deepin获取系统版本信息
getOsVersionandReleaseInfo_for_isoft_mips_64bit() {
    getOsVersionandReleaseInfo_for_common
}

#function: x86-64位银河麒麟获取版本信息
getOsVersionandReleaseInfo_for_yhkylin_x86_64bit() {
    getOsVersionandReleaseInfo_for_common
    if [ "$(echo "$osinfo" | grep -i "server")" != "" ]; then
        eval Os_Name0="yhkylin_server"
    else
        eval Os_Name0="yhkylin_desktop"
    fi
}

#function:  从系统文件中获取系统名称,得到系统的名称和发行版本号
getOsInfo() {
    #获取系统名称，构建分类基础
    local osinfo
    getOsNameInfo
    if [ $? -eq 1 ]; then return; fi

    #根据系统名称、操作系统位数、CPU架构分别获取系统版本信息
    local os_name=""
    eval os_name="\$Os_Name0"
    if [ "$os_name" = "ubuntu" ]; then
        getOsVersionandReleaseInfo_for_ubuntu
    elif [ "$os_name" = "centos" ]; then
        getOsVersionandReleaseInfo_for_centos
    elif [ "$os_name" = "redhat" ]; then
        getOsVersionandReleaseInfo_for_redhat
    elif [ "$os_name" = "suse" ]; then
        getOsVersionandReleaseInfo_for_suse
    elif [ "$os_name" = "xenserver" ]; then
        getOsVersionandReleaseInfo_for_xenserver
    elif [ "$os_name" = "neokylin_desktop" ] && [ "$machine_arc" = "aarch" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_neokylin_desktop_arm64_64bit
    elif [ "$os_name" = "neokylin_server" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_neokylin_server_mips_64bit
    elif [ "$os_name" = "neokylin_server" ] && [ "$machine_arc" = "x86" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_neokylin_server_x86_64bit
    elif [ "$os_name" = "neokylin_desktop" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 0 ]; then
        getOsVersionandReleaseInfo_for_neokylin_desktop_mips_32bit
    elif [ "$os_name" = "neokylin_desktop" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_neokylin_desktop_mips_64bit
    elif [ "$os_name" = "neokylin_desktop" ] && [ "$machine_arc" = "x86" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_neokylin_desktop_x86_64bit
    elif [ "$os_name" = "yhkylin" ] && [ "$machine_arc" = "aarch" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_yhkylin_aarch_64bit
    elif [ "$os_name" = "deepin" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 0 ]; then
        getOsVersionandReleaseInfo_for_deepin_mips_32bit
    elif [ "$os_name" = "deepin" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_deepin_mips_64bit
    elif [ "$os_name" = "deepin" ] && [ "$machine_arc" = "x86" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_deepin_x86_64bit
    elif [ "$os_name" = "isoft" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_isoft_mips_64bit
    elif [ "$os_name" = "yhkylin" ] && [ "$machine_arc" = "x86" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_yhkylin_x86_64bit
    elif [ "$os_name" = "yhkylin" ] && [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_yhkylin_mips_64bit
    elif [ "$os_name" = "uos" ] && [ $Os_Bit -eq 1 ]; then
        getOsVersionandReleaseInfo_for_UOS_64bit
    elif [ "$os_name" = "uos" ] && [ $Os_Bit -eq 0 ]; then
        getOsVersionandReleaseInfo_for_UOS_64bit
    else
        #新增系统可加入此处
        return
    fi
}

#function: 得到系统的名称
getOsName() {
    eval osname="\$Os_Name0"
}

#function: 得到系统的全版本号
getOsAllVersion() {
    eval version="\$Os_Name1"
}

#function: 得到系统发行大版本号
getOsVersion() {
    eval version="\$Os_Name1"
    version="${version%%.*}"
}

#function: 得到系统包管理器类别
#result:   0=RPM;1=DEB
getpakeagemanger() {
    [ -f "/etc/debian_version" ] && pkg_mgr=1 || pkg_mgr=0
}

#function:  自动检测系统类型，并显示
showAutoCheckOs() {

    getH3CInfo && [ !$? ] && getESXiInfo && [ !$? ] && getOsInfo

    getOsName
    getOsAllVersion

    getpakeagemanger
    getautoType

    printtext "## 当前系统信息:"
    if [ ${is_susport_chinese} -eq 1 ]; then
        echo "## 操作系统："$osname $version
    else
        echo "## operating system:"$osname $version
    fi

    [ ${Machine_Bit} -eq 1 ] && printtext "## 系统位数：64位系统" || printtext "## 系统位数：32位系统"

    if [ ${is_susport_chinese} -eq 1 ]; then
        echo "## 芯片架构："$machine_arc
    else
        echo "## Chip architecture:"$machine_arc
    fi

    #local result=$(echo $os_name | grep "server")
    [ ${pkg_mgr} -eq 1 ] && printtext "## 安装包管理器：deb" || printtext "## 安装包管理器：rpm"

    if [ $client_ui -eq ${LINUX_NO_UI} ]; then
        client_type=${LINUX_SERVER}
        case ${is_susport_chinese} in
        0) echo "## Terminal category:$(tput setaf ${bgcred})Linux Server$(tput sgr0)" ;;
        1) echo "## 终端类别：$(tput setaf ${bgcred})Linux服务器$(tput sgr0)" ;;
        esac
    elif [ $client_ui -eq ${LINUX_UI} ] && [ -z "$(echo $Os_Name0 | grep "server")" ]; then
        client_type=${LINUX_CLIENT}
        case ${is_susport_chinese} in
        0) echo "## Terminal category:$(tput setaf ${bgcred})Domestic general terminal$(tput sgr0)" ;;
        1) echo "## 终端类别：$(tput setaf ${bgcred})国产通用终端$(tput sgr0)" ;;
        esac
    elif [ $client_ui -eq ${LINUX_UI} ] && [ $(echo $Os_Name0 | grep "server") ]; then
        client_type=${LINUX_CLIENT_SERVER}
        case ${is_susport_chinese} in
        0) echo "## Terminal category:$(tput setaf ${bgcred})Domestic general server$(tput sgr0)" ;;
        1) echo "## 终端类别：$(tput setaf ${bgcred})国产通用服务器$(tput sgr0)" ;;
        esac
    fi

}

#function:  输出彩色字符串
cecho() {
    message=$1
    color=$2
    echo "$(tput setaf $color)$message$(tput sgr0)"
    return
}

#function:  定义一级显示菜单
menu1() {
    cat <<TEND
-------------- menu --------------
## 1：国产通用终端
## 2：国产通用服务器
TEND
}
#function:  定义英文一级显示菜单
menu1_e() {
    cat <<TEND
-------------- menu --------------
## 1：Domestic general terminal
## 2：Domestic general server
TEND
}
#function:  定义终端二级显示菜单
menu1_2() {
    if [ ${is_susport_chinese} -eq 1 ]; then
        case ${Os_Bit} in
        0) _bit="32位" ;;
        1) _bit="64位" ;;
        esac
    else
        case ${Os_Bit} in
        0) _bit="32bit" ;;
        1) _bit="64bit" ;;
        esac
    fi

    if [ ${is_susport_chinese} -eq 1 ]; then
        echo "## 1、 芯片${machine_arc} ${_bit} deb安装包"
        echo "## 2、 芯片${machine_arc} ${_bit} rpm安装包"
    else
        echo "## 1、 chip${machine_arc} ${_bit} deb installation package"
        echo "## 2、 chip${machine_arc} ${_bit} rpm installation package"
    fi
}

#function:  主菜单显示逻辑
main_menu_show() {

    #若为linuxs无界面终端，直接显示二级菜单，返回
    [ $client_ui -eq $LINUX_NO_UI ] && printtext "按回车即可开始自动安装，如识别错误，请键入对应芯片架构、安装包管理器的数字，按回车进行安装：" ${c_notify} && menu1_2 && read index2 && return

    #判断为linuxc有界面终端，显示一级界面，等待输入界面索引
    if [ ${is_susport_chinese} -eq 1 ]; then
        [ $client_ui -eq 1 ] && printtext "按回车即可开始自动安装，如识别错误，请按照实际情况键入对应的终端类型的数字，按回车进入下一步：" ${c_notify} && menu1 && read index1
    else
        [ $client_ui -eq 1 ] && printtext "按回车即可开始自动安装，如识别错误，请按照实际情况键入对应的终端类型的数字，按回车进入下一步：" ${c_notify} && menu1_e && read index1
    fi

    #一级目录输入有误，退出
    [ ${index1} ] && ([ ${index1} -gt 2 ] || [ ${index1} -le 0 ]) && printtext "输入有误,程序退出,请重新运行" ${c_error} && exit 1

    if [ "${index1}" = "1" ]; then
        client_type=${LINUX_CLIENT}
    elif [ "${index1}" = "2" ]; then
        client_type=${LINUX_CLIENT_SERVER}
    fi

    #判断一级目录输入无误后，根据index1显示二级菜单
    [ ${index1} ] && menu1_2 && printtext "请键入对应芯片架构、安装包管理器的数字，按回车进行安装：" ${c_notify} && read index2

}

#function:  Linux Server自动安装,主界面上的索引号
getautoTypeforLinuxServer() {
    #echo $osname $version $machine_arc $Os_Bit
    if [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 1 ]; then
        choose_content=1
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 1 ]; then
        choose_content=2
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "aarch" ]; then
        choose_content=3
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "aarch" ]; then
        choose_content=4
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 0 ]; then
        choose_content=5
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 0 ]; then
        choose_content=6
    else
        Log $INFO "自动安装没有得到主界面上的索引号"
        choose_content=-1
    fi
}

#function:  Linux Client自动安装,主界面上的索引号
getautoTypeforLinuxClient() {

    #echo $osname $version $machine_arc $Os_Bit
    if [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "aarch" ]; then
        choose_content=1
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "aarch" ]; then
        choose_content=2
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "mips" ]; then
        choose_content=3
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "mips" ]; then
        choose_content=4
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "sw" ]; then
        choose_content=5
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "sw" ]; then
        choose_content=6
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 1 ]; then
        choose_content=7
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 1 ]; then
        choose_content=8
    elif [ ${pkg_mgr} -eq 1 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 0 ]; then
        choose_content=9
    elif [ ${pkg_mgr} -eq 0 ] && [ "$machine_arc" = "x86" ] && [ ${Os_Bit} -eq 0 ]; then
        choose_content=10
    else
        Log $INFO "自动安装没有得到主界面上的索引号"
        choose_content=-1
    fi
}

#function: 自动安装,主界面上的索引号
getautoType() {
    local osname=""
    eval osname="\$Os_Name0"
    local version=""
    getOsVersion

    Log $INFO "系统为$osname"
    Log $INFO "版本为$version"

    if [ $client_ui -eq $LINUX_UI ]; then
        getautoTypeforLinuxClient
    else
        getautoTypeforLinuxServer
    fi
}

#function:  命令行参数分类
preActionInstallqaxsafe() {

    local osname=""
    local version=""

    #带显示界面，不带命令行参数执行
    #显示自动获取的系统信息
    showAutoCheckOs
    #显示主界面
    local index1
    local index2
    main_menu_show

    #读入键盘输入
    #判断输入的数字范围，以及是否为整型
    if ([ -z "$(echo $index2 | sed -e 's/[0-9]//g')" ] && [ "${index2}" != "" ]) || [ "${index1}" = "" -a "${index2}" != "" ]; then

        if [ $index2 -gt 2 -o $index2 -lt 1 ]; then
            printtext "输入有误,程序退出,请重新运行" ${bgcred}
            Log $ERROR "输入有误"
            printtext "输入有误,程序退出,请重新运行" ${bgcred}
            exit 1
        else
            Log $INFO "手动下载"
            case ${index2} in
            1) pkg_mgr=1 ;;
            2) pkg_mgr=0 ;;
            esac
            case ${client_ui} in
            ${LINUX_UI}) getautoTypeforLinuxClient ;;
            ${LINUX_NO_UI}) getautoTypeforLinuxServer ;;
            esac
        fi
    fi

}

#function:  获取ESXi的执行文件路径
getEsxiSaveDir() {
    local index_i
    num=$(df -h | grep -E '^VMFS' | wc -l)
    for index_i in $(seq 1 $num); do
        available_unit=$(df -h | grep -E '^VMFS' | awk '{print $4}' | head -n $index_i | tail -n 1 | sed -e 's/[0-9.]//g')
        available_size=$(df -h | grep -E '^VMFS' | awk '{print $4}' | head -n $index_i | tail -n 1 | sed -e 's/\..*$//g')
        if [ "$available_unit"x == "T"x ] || [ "$available_unit"x == "G"x ] || ([ "$available_unit"x == "M"x ] && [ $available_size -gt 100 ]); then
            excutefilepath="$(df -h | grep -E '^VMFS' | sed -e 's/^.*\/vmfs/\/vmfs/g' | head -n $index_i | tail -n 1)"
            excutefilepath=$(readlink -f "$excutefilepath")/
            #echo "save dir: $excutefilepath"
            break
        fi
    done
}

#function:  获取执行文件路径
getSaveDir() {
    local osname=""
    eval osname="\$Os_Name0"
    local version=""
    getOsVersion
    if [ "$osname" = "VMware ESXi" ] && [ "$version" = "5" -o "$version" = "6" ]; then
        getEsxiSaveDir
        if [ -z "$excutefilepath" ]; then
            printtext "安装失败，没有足够的空间!"
            exit 1
        fi
    else
        excutefilepath="/opt/"
    fi
}

#function:  获取安装包下载地址
getDownloadURL() {
    if [ "$pkg_name" != "" ]; then
        Log $INFO "server_ip:$server_ip"
        Log $INFO "server_port:$server_port"
        local url_part="http://$server_ip:$server_port/download/setup_linux/"
        url="${url_part}${pkg_name}"
        return 1
    else
        return 0
    fi
}

#function: 判断url是否可以访问
getNetStateCode() {
    url_ok=1
}

#function: 下载安装包
downloadPkgFile() {
    local url=""
    getDownloadURL
    if [ $? -eq 0 ]; then
        Log $ERROR "url无效"
        printtext "很抱歉,您当前的系统暂不支持..."
        return 0
    fi

    #下载相应安装包
    Log $INFO "下载的url：$url"
    echo $url
    local url_ok=0
    getNetStateCode $url
    if [ $url_ok -eq 0 ]; then
        printtext "网络异常，请检查网络后重新运行！"
        return 0
    fi

    local osname=""
    eval osname="\$Os_Name0"
    local version=""
    getOsVersion

    if [ "$osname" = "VMware ESX" ] && [ "$version" = "4" ]; then
        # ESX 4不支持wget
        curl -o "$exc_filepath" $url
    elif [ "$osname" = "VMware ESXi" ] && [ "$version" = "5" -o "$version" = "6" ]; then
        # ESXi wget参数不支持-t
        wget -O "$exc_filepath" $url
    else
        #其他特殊下载方式均可添加于此处
        if [ "$(which wget 2>/dev/null)" = "" ]; then
            curl -o "$exc_filepath" $url
        else
            wget -t 3 -O "$exc_filepath" $url
        fi

    fi
    echo ""
    # -o/-O会使得若没有安装包，同样会在相应目录下产生一个大小为0的文件
    if [ "$(ls -al "$exc_filepath" | awk '{print $5}')" \> "0" ]; then
        return 1
    else
        return 0
    fi
}

#function:  ipv6环境下下载安装包
downloadPkgFile_with_ipv6() {
    # 嵌入python增加对本地链路地址的支持
    PYTHON_ARG="$1 $2 $3" python - <<END
#coding:utf-8
import urllib2,urllib
import socket
import os,sys
import copy
import time

python_arg = os.environ['PYTHON_ARG']
python_arg = python_arg.replace('[', '')
python_arg = python_arg.replace(']_', ' ')
server_ip = python_arg.split(' ')[0]
server_port = python_arg.split(' ')[1]
pkg_name = python_arg.split(' ')[2]
exc_filepath = python_arg.split(' ')[3]

# 获取所有网卡名称
def get_ifnames():
    ifnames = []
    p = os.popen('cat /proc/net/dev')
    for line in p.readlines():
        line = line.strip()
        eth_name = line.split(':', 1)
        if len(eth_name) == 2:
            ifnames.append(eth_name[0])
    return ifnames

# 获取各网卡对应的所有ipv6地址
def get_ipv6_address():
    ifnames = get_ifnames()
    ipv6_address = {}
    p = os.popen('ifconfig')
    eth_name = ""
    ip_list = []
    for line in p.readlines():
        line = line.strip()
        flag = 0
        eth_name_old = eth_name
        for ifname in ifnames:
            if line.find(ifname+':') != -1:
                eth_name = ifname
                flag = 1
                break
        if flag == 1:
            if len(ip_list) != 0:
                ipv6_address[eth_name_old] = copy.deepcopy(ip_list)
                del ip_list[:]
            continue
        info = line.split(' ')
        for index in range(len(info)):
            if info[index] == 'inet6' and index < len(info) - 1:
                ip_list.append(info[index+1])
                break
    if len(ip_list) != 0:
        ipv6_address[eth_name_old] = copy.deepcopy(ip_list)
    return ipv6_address

# 判断终端对控制中心的网络联通状态
def get_net_status(url, requests_times):
    while requests_times > 0:
        try:
            code = urllib2.urlopen(url, timeout = 1).getcode()
            if code == 200:
                return True
        except urllib2.URLError, e:
            if isinstance(e.reason, socket.timeout):
                print "connect to: \" ", url, " \" timeout, retry..."
        requests_times = requests_times - 1
    return False

# 获取能够联通控制中心的有效网卡名称，支持本地链路地址
def get_useful_ifname():
    # ipv6_address = get_ipv6_address()
    ipv6_address = get_ifnames()
    for ifname in ipv6_address:
        server_url = 'http://[' + server_ip + '%' + ifname + ']:' + server_port
        if True == get_net_status(server_url, 3):
            return ifname
    return ''

# 进度条显示
def processbar(width, percent, total_size):
    time.sleep(0.0001)
    sys.stdout.flush()
    sys.stdout.write ("Download progress[size:%d bytes]:[%s %d%%]\r" % (total_size ,('%%-%ds' % width) % (width * percent / 100 * '='), percent))

# 下载进度显示
def download_schedule(finish_blocks, block_size, total_size):
    percent = (int)(100 * finish_blocks * block_size) / total_size
    if percent > 100:
        percent = 100
    processbar(35, percent,total_size)

if __name__ == "__main__":
    try:
        ifname = get_useful_ifname()
        if ifname == '':
            print "network error: cannot connect to control center"
        else:
            download_url = 'http://[' + server_ip + '%' + ifname + ']:' + server_port + '/download/setup_linux/' + pkg_name
            if False == get_net_status(download_url, 3):
                print "error: connot get the install package from control center, download failed"
            else:
                print
                # 增加进度条效果
                urllib.urlretrieve(download_url, exc_filepath, download_schedule)
                print
                # with open(exc_filepath, "wb") as code:
                #     code.write(urllib2.urlopen(download_url).read())
        sys.exit()
    except:
        sys.exit()
END
    if [ -f $exc_filepath ]; then
        return 1
    else
        return 0
    fi
}

#function:  ipv6 global address环境下下载安装包
downloadPkgFile_with_ipv6_use_global_address() {
    downloadPkgFile
}

#function:  执行下载后的安装包
executeSetup() {
    #根据文件的后缀名来区分应该执行的命令
    local expand_name=${exc_filepath##*.}
    echo $exc_filepath
    if [ "$expand_name" = "deb" ]; then
        printtext "正在更新..."
        Log $INFO "执行deb包安装"
        local command_pre="dpkg -i "
        local command_name="$command_pre$exc_filepath"
        echo $command_name
        $command_name
        if [ $? -ne 0 ]; then
            Log $ERROR "执行安装包失败"
            printtext "安装失败！"
        fi
    elif [ "$expand_name" = "rpm" ]; then
        printtext "正在更新..."
        Log $INFO "执行rpm包安装"
        local command_pre="rpm -U --nodeps "
        local command_name="$command_pre$exc_filepath"
        echo $command_name
        $command_name
        if [ $? -ne 0 ]; then
            Log $ERROR "执行安装包失败"
            printtext "安装失败！"
        else
            printtext "安装完成!"
        fi
    elif [ "$expand_name" = "tar" ]; then
        printtext "正在更新..."
        Log $INFO "执行tar包安装"
        local command_pre="tar -xf "
        local command_name="$command_pre$exc_filepath -C $excutefilepath"
        echo "$command_name"
        $command_name
        if [ $? -ne 0 ]; then
            Log $ERROR "执行安装包失败"
            printtext "安装失败！"
        fi
        local curdir="$(pwd)"
        cd "$excutefilepath"
        command_name="sh ${excutefilepath}install.sh ${excutefilepath} ${server_ip} ${server_port}"
        echo $command_name
        $command_name
        if [ $? -ne 0 ]; then
            Log $ERROR "执行安装包失败"
            printtext "安装失败！"
        fi
        cd "$curdir"
    else
        Log $ERROR "安装失败"
        printtext "安装失败！"
    fi
}

#function:  安装完成后清理数据，包括安装包，pkgname.txt等
removeData() {
    local excutefilepath=""
    getSaveDir
    brand=$[2*180]
    if [ -f "${excutefilepath}${brand}autostart.vib" ]; then
        rm ${excutefilepath}${brand}autostart.vib
    fi

    if [ -f "${excutefilepath}${brand}safe-for-esxi-x64.tar.gz" ]; then
        rm ${excutefilepath}${brand}safe-for-esxi-x64.tar.gz
    fi

    if [ -f "${excutefilepath}install.sh" ]; then
        rm ${excutefilepath}install.sh
    fi

    if [ -f "$exc_filepath" ]; then
        rm $exc_filepath
    fi
    # 新版本后期模块安装过程中需要倚重此文件，不能删除
    # if [ -f "/opt/pkgname.txt" ]; then
    #     rm /opt/pkgname.txt
    # fi
}

#function:  将安装包和ip以及端口号进行拼接,格式：文件名(10.1.1.1_80).后缀名, 用来保存到本地中
renameFileName() {
    local l_oldname=${pkg_name%.*}
    local r_oldname=${pkg_name##*.}
    local new_name="$l_oldname(${server_ip}_$server_port).$r_oldname"
    renamed_pkgname="$new_name"
    echo $renamed_pkgname
}

#function: 获取下载的安装包名称
getPkgName() {
    eval pkgname="\$pkg_array$(expr $idx - 1)"
    pkg_name="$pkgname"
}

#function: 将修改过安装包名称的字串写入到pkgname.txt文件中
savePkgName() {
    if [ ! -d "/opt" ]; then mkdir "/opt"; fi
    cat /dev/null >"/opt/pkgname.txt"
    # step 1: 记录控制中心IP和PORT
    echo "pkgname=\"$1\"" >>"/opt/pkgname.txt"
    # step 2: 记录终端类型
    if [ $client_type -eq $LINUX_CLIENT ]; then
        echo "type=2" >>"/opt/pkgname.txt"
    elif [ $client_type -eq $LINUX_CLIENT_SERVER ]; then
        echo "type=3" >>"/opt/pkgname.txt"
    else
        echo "type=1" >>"/opt/pkgname.txt"
    fi
    # step 3: 记录界面类型
    if [ $client_ui -eq $LINUX_NO_UI ]; then
        echo "ui=0" >>"/opt/pkgname.txt"
    else
        echo "ui=1" >>"/opt/pkgname.txt"
    fi
    # step 4: 记录架构和操作系统类型
    local machine_arc
    getMachineArc
    getOsBit
    Os_Bit=$?
    if [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 1 ]; then
        echo "arch=mips64" >>"/opt/pkgname.txt"
    elif [ "$machine_arc" = "mips" ] && [ $Os_Bit -eq 0 ]; then
        echo "arch=mips32" >>"/opt/pkgname.txt"
    elif [ "$machine_arc" = "aarch" ] && [ $Os_Bit -eq 1 ]; then
        echo "arch=arm64" >>"/opt/pkgname.txt"
    elif [ "$machine_arc" = "sw" ] && [ $Os_Bit -eq 1 ]; then
        echo "arch=sw64" >>"/opt/pkgname.txt"
    elif [ "$machine_arc" = "x86" ] && [ $Os_Bit -eq 0 ]; then
        echo "arch=x86" >>"/opt/pkgname.txt"
    else
        echo "arch=x64" >>"/opt/pkgname.txt"
    fi
}

#fucntion:  修改过安装包名称，并将其写入到pkgname.txt文件中
recordInfoIntoPkgnameFile() {
    #获取本地安装包名称
    local idx=$choose_content
    getPkgName
    #调整安装包名称格式
    local renamed_pkgname=""
    renameFileName
    #将新的文件名称记录在pkgname.txt文件中
    savePkgName $renamed_pkgname
}

#function:  获取安装包下载的全路径地址
getPkgDownloadPath() {
    #获取安装包的保存路径
    getSaveDir
    if [ ! -d "$excutefilepath" ]; then
        Log $INFO "save_dirpath文件夹失败"
        mkdir "$excutefilepath"
    fi
    #获取安装包的全路径地址
    exc_filepath="${excutefilepath}${intime}${pkg_name}"
    local osname=""
    eval osname="\$Os_Name0"
    if [ ${osname} = "uos" ]; then
        pkg_name=`echo ${pkg_name} | sed "s/qaxsafe/uos/g"`
        exc_filepath="${excutefilepath}${intime}${pkg_name}"
    fi
}

#function: 判断程序是否之前已经安装
isInstall() {
    if [ -L "/opt/qaxsafe" ]; then
        rm -f /opt/qaxsafe
    fi
    if [ ! -d "/opt/qaxsafe" ]; then
        return 0
    else
        if [ ${pkg_mgr} -eq 1 ]; then
            instat=`dpkg -l | grep qaxsafe| awk '{print $1}'`
            if [ "${instat}" = "ii" ]; then
                return 1
            else
                return 0
            fi
        else
            if [ -z "$(rpm -qa | grep qaxsafe)" ]; then
                return 0
            fi
        fi
        return 1
    fi
}

#function:  安装qax终端
Installqaxsafe() {
    isInstall
    if [ $? -eq 0 ]; then
        local pkg_name=""
        local excutefilepath=""
        #记录信息到pkgname.txt文件中
        recordInfoIntoPkgnameFile
        #获取安装包下载的全路径地址
        getPkgDownloadPath
        if [ "$(echo $server_ip | grep -i "\[fe80" | grep "\]")" != "" ]; then
            downloadPkgFile_with_ipv6 "$server_ip""_""$server_port" "$pkg_name" "$exc_filepath"
        elif [ "$(echo $server_ip | grep "\[" | grep "\]")" != "" ]; then
            downloadPkgFile_with_ipv6_use_global_address
        else
            downloadPkgFile
        fi
        if [ $? -eq 1 ]; then
            Log $INFO "更新开始"
            executeSetup
        fi
    else
        printtext "您已经安装过该程序,执行安装失败!" $c_error
        exit 1
    fi
}

#function:  主函数
run() {
    #判断是否支持中文
    isSupportChinese
    local is_susport_chinese=$?
    #判断是否运行在Root权限
    isExcuteinRootAuth

    #排除一定不支持的系统和平台
    local Machine_Bit
    local Os_Bit
    local machine_arc=""
    isSupportOsandPlatform

    #获取服务端ip和port
    getServerIpAndPort

    #获取终端类型
    getClientUI_by_filename
    local client_ui=$?

    #初始化终端支持列表
    init_supported_os_matrix

    #根据命令行参数分类执行
    local choose_content=""
    local client_type
    local pkg_mgr
    preActionInstallqaxsafe

    if [ $choose_content -eq -1 ]; then
        printtext "您当前系统暂且不支持自动安装部署！" ${c_error}
        exit 1
    fi

    #安装qax终端
    Installqaxsafe

    #安装完成后清理数据
    removeData

    exit 0
}

run
