#!/bin/bash

CONFIG_FILE=deploy-config/all.conf

source scripts/utils.sh
source scripts/config_get.sh
source scripts/global_variables.sh
source scripts/hardware_perfcheck.sh

local_deploy_ecm(){
    source $install_path"/scripts/"k8s_allinone_deploy.sh "${install_path}" "${Es_Version}"
    echo "allinone middleware 中间件部署完毕！"
}

get_install_path() {
    find_path=`find / -name "auto_install.sh" -type f | awk -F "auto_install.sh" '{print $1}'`
    install_path="${find_path%%/}"
    #install_path=$(pwd)
    echo "${install_path}"
    cd "${install_path}"
    chmod -R +x scripts/*
}

config_info(){
    log_info "#### get config_info function start"
    log_info "[install_type] TYPE=${TYPE}"
    log_info "#### get config_info function end"
    source $install_path"/scripts/"create_remote_env.sh
}

main(){
    source scripts/echo_variable.sh

    echo "${SYSTEM}"
    if [ "${TYPE}" == "allinone" ];then
        config_info
        echo "### exec allinone choice ################################"
        #source scripts/install_requirements.sh
        get_install_path
        sleep 5
        #source scripts/hardware_perfcheck.sh
        if [ "${SYSTEM}" == "Ubuntu" ] || [ "${SYSTEM}" == "ubuntu" ];then
            apt autoremove -y fio* &
            sleep 9
            dpkg -i "$install_path"/Install_Packages/ubuntu18.04_deb/fio-3.30.deb &
            sleep 9
            #hardware_perfcheck
        else
	    yum remove fio* -y &
	    sleep 9
	    rpm -ivh "$install_path"/Install_Packages/fio/* --force &
	    sleep 9
            hardware_perfcheck
        fi
        sleep 1
        if [ "${ACTION_TYPE}" == "install" ];then
#            echo "### install harbor ##################################"
#            local_install_harbor

            echo "### local k8s deploy app service ####################"
            local_deploy_ecm 
        fi
    fi
    log_info "system_version"  
}

get_install_path
Es_Version="es6"

echo “****************************************************************************************************************************”
if [ -z "$Es_Version" ]; then
    echo "Error: Elasticsearch 版本参数不能为空(ES版本选择)！"
    echo "请执行 source allinone_deploy_middleware.sh es6 或者 source allinone_deploy_middleware.sh es7"
    echo "（es6支持ecm6.6以下版本，es7支持ecm6.6及以上版本, ecm产线版本对es版本有兼容依赖性，请确认你需要部署产线对应的es版本！！！）"
    exit 1
fi

if [ "$Es_Version" != "es7" ] && [ "$Es_Version" != "es6" ]; then
    echo "Error: Elasticsearch 版本参数错误(ES版本选择)！"
    echo "请执行 source allinone_deploy_middleware.sh es6 或者 source allinone_deploy_middleware.sh es7"
    echo "（es6支持ecm6.6以下版本，es7支持ecm6.6及以上版本, ecm产线版本对es版本有兼容依赖性，请确认你需要部署产线对应的es版本！！！）"
    exit 1
fi

if [ "$Es_Version" == "es6" ]; then
    echo "当前ES版本为："${Es_Version}
    echo "适配兼容的Ecm版本小于 < ecm-v6.6.0.0"
fi
if [ "$Es_Version" == "es7" ]; then
    echo "当前ES版本为："${Es_Version}
    echo "适配兼容的Ecm版本大于等于 >= ecm-v6.6.0.0"
fi

 echo “****************************************************************************************************************************”
#install_type=`cat conf/default.conf|grep -vE '^$|#|\[' | grep type | grep "=" | awk -v FS='=' '{print $2}'`
#system_type=`cat conf/default.conf|grep -vE '^$|#|\[' | grep system | grep "=" | awk -v FS='=' '{print $2}'`
#system_version=`cat conf/default.conf|grep -vE '^$|#|\[' | grep version | grep "=" | awk -v FS='=' '{print $2}'`
#time_server=`cat conf/default.conf|grep -vE '^$|#|\[' | grep timeserver | grep "=" | awk -v FS='=' '{print $2}'`
#k8s_version=`cat conf/default.conf|grep -vE '^$|#|\[' | grep kversion | grep "=" | awk -v FS='=' '{print $2}'`
main
