#!/bin/bash


#########color code#############
RED="31m"      ## 姨妈红
GREEN="32m"    ## 水鸭青
YELLOW="33m"   ## 鸭屎黄
BLUE="35m"     ## 基佬紫
BLUE="36m"     ## 天依蓝

###############color echo func#################
colorEcho(){
    echo -e "\033[${1}${@:2}\033[0m" 1>& 2
}

check_root(){
	if [[ $EUID != 0 ]];then
    colorEcho ${RED} "当前非root账号(或没有root权限)，无法继续操作，请更换root账号!"
    colorEcho ${YELLOW} "使用sudo -命令获取临时root权限（执行后可能会提示输入root密码）"
    exit 1
    fi
}

#######   获取系统版本及64位或32位信息
check_sys() {
    # sys_bit=$(uname -m)
    # case $sys_bit in
    # i[36]86)
    #     os_bit="32"
    #     release="386"
    #     ;;
    # x86_64)
    #     os_bit="64"
    #     release="amd64"
    #     ;;
    # *armv6*)
    #     os_bit="arm"
    #     release="arm6"
    #     ;;
    # *armv7*)
    #     os_bit="arm"
    #     release="arm7"
    #     ;;
    # *aarch64* | *armv8*)
    #     os_bit="arm64"
    #     release="arm64"
    #     ;;
    # *)
    #     colorEcho ${RED} "
    #     哈哈……这个 辣鸡脚本 不支持你的系统。 (-_-) \n
    #     备注: 仅支持 Ubuntu 16+ / Debian 8+ / CentOS 7+ 系统
    #     " && exit 1
    #     ;;
    # esac
    ## 判定Linux的发行版本
    if [ -f /etc/redhat-release ]; then
        LinuxReleaseVersion="centos"
    elif cat /etc/issue | grep -Eqi "debian"; then
        LinuxReleaseVersion="debian"
    elif cat /etc/issue | grep -Eqi "ubuntu"; then
        LinuxReleaseVersion="ubuntu"
    elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
        LinuxReleaseVersion="centos"
    elif cat /proc/version | grep -Eqi "debian"; then
        LinuxReleaseVersion="debian"
    elif cat /proc/version | grep -Eqi "ubuntu"; then
        LinuxReleaseVersion="ubuntu"
    elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
        LinuxReleaseVersion="centos"
    else
        LinuxReleaseVersion=""
    fi

    # 判断系统的包管理工具  apt, yum, or zypper
    getPackageManageTool() {
        if [[ -n $(command -v apt-get) ]]; then
            CMD_INSTALL="apt-get -y -qq install"
            CMD_UPDATE="apt-get -qq update"
            CMD_REMOVE="apt-get -y remove"
        elif [[ -n $(command -v yum) ]]; then
            CMD_INSTALL="yum -y -q install"
            CMD_UPDATE="yum -q makecache"
            CMD_REMOVE="yum -y remove"
        elif [[ -n $(command -v zypper) ]]; then
            CMD_INSTALL="zypper -y install"
            CMD_UPDATE="zypper ref"
            CMD_REMOVE="zypper -y remove"
        else
            return 1
        fi
        return 0
    }

    # 检查系统包管理方式，更新包
    getPackageManageTool
    if [[ $? -eq 1 ]]; then
        colorEcho ${RED} "系统的包管理不是 APT or YUM, 请手动安装所需要的软件."
        return 1
    fi

    ### 更新程序引索
    if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
        colorEcho ${BLUE} "正在更新软件包管理..."
        $CMD_UPDATE
        SOFTWARE_UPDATED=1
    fi
    return 0
}

##  安装所需要的程序，及依赖程序
installDemandSoftwares() {
    for software in $@; do
        ## 安装该软件
        if [[ -n $(command -v ${software}) ]]; then
            colorEcho ${GREEN} "${software}已经安装了...跳过..."
        else
            colorEcho ${BLUE} "正在安装 ${software}.."
            $CMD_INSTALL ${software}

            ## 判断该软件是否安装成功
            if [[ $? -ne 0 ]]; then
                colorEcho ${RED} "安装 ${software} 失败. 请手动安装该程序."
                return 1
            else
                colorEcho ${GREEN} "已经成功安装 ${software}."
            fi
        fi
    done
    return 0
}