#!/bin/bash
#Config_Dir:存放config文件的目录
#Cosmic_Install_Dir：苍穹安装目录
stty_len=`stty size | awk '{print $2}'`
pd=$PWD
#输出符号
function Print(){
	if [ ! $stty_len ];then
        stty_len=40
    fi
	for (( i = 1; i <= $stty_len; i++));do
		echo -e "\033[32m$*\033[0m\c"
	done
}

function Before_Install(){
	Print "*"
	echo -e "
        \033[33m##开始安装苍穹，请注意以下信息：\033[0m
		\033[33m##1.数据库时区是否为CST？\033[0m
		\033[33m##2.安装目录需要指定为磁盘最大的目录\033[0m
		\033[33m##3.服务器主机名不能带小数点\033[0m
                \033[33m##4.初始化k8s服务器，跟新内核版本需要有重启机器后再重新执行本脚本\033[0m
                \033[33m##5.若已升级完内核版本，在提示升级内核版本的时候选择否（n）\033[0m
		"
	Print "*"
	read -r -p  "请输入config文件所在路径：" Config_Dir
	while true
	do
		ls $Config_Dir/config > /dev/null 2>&1
		if [ $? != 0 ];then
			read -r -p  "$Config_Dir目录下不存在config文件，请重新输入：" Config_Dir
		else
			break
		fi
	done
	\cp -f $Config_Dir/config ./KDCC_installtool/
	read -r -p  "请输入苍穹安装目录(绝对路径)：" Cosmic_Install_Dir
	while true
	do
		read -r -p  "苍穹安装目录为$Cosmic_Install_Dir,请选择[Y/N]确认：" confirm
		if [ $confirm == 'Y' ] || [ $confirm == 'y' ];then
			break
		elif [ $confirm == 'N' ] || [ $confirm == 'n' ];then
			read -r -p  "请重新输入苍穹安装目录(绝对路径)：" Cosmic_Install_Dir
		else
			echo "请输入[Y/N]进行确认！"
		fi
	done
	mkdir -p $Cosmic_Install_Dir

        read -r -p  "是否需要初始化k8s机器内,核若内核无需升级请选择N开始安装苍穹，选择Y不会安装苍穹只会跟新内核[Y/N]：" if_upgrade_core
        while true
        do
                if [ $if_upgrade_core == 'Y' ] || [ $if_upgrade_core == 'y' ];then
                        echo "开始升级k8s机器内核，在升级内核完成操作后重新执行此脚本"
                        echo “开始升级k8s内核”
                        break
                elif [ $if_upgrade_core == 'N' ] || [ $if_upgrade_core == 'n' ];then
                        echo "开始安装苍穹"
                        break
                else
                     echo "请输入[Y/N]进行确认！"
                fi
        done


}

function Install_Cosmic(){
	## One key install cosmic ##
	cd $pd/KDCC_installtool
	echo "Now installing cosmic..."
	echo "============================================================="
	sh install_ansible.sh $Cosmic_Install_Dir
	sh generate_config.sh $Cosmic_Install_Dir
        if [ $if_upgrade_core == 'Y' ] || [ $if_upgrade_core == 'y' ]; then
		sh ../preinstall.sh
                echo "重启完成后再重新执行此脚本"
                exit 0
	fi
	if [ $? -ne 0 ];then
		echo -e "\033[31m Something wrong occured during generating profile,please check! \033[0m"
		exit 2
	else
		sh ../k8s-install/install.sh
		sh init_sys_parm.sh
		sh install_dependencies.sh
		sh install_mysql.sh
		sh install_jdk.sh
		sh install_zookeeper.sh
		sh install_rabbitmq.sh
		sh install_redis_cache.sh
		sh install_redis_session.sh
		sh install_redis_algo.sh
		sh install_nginx.sh
		sh install_appstatic.sh
		sh install_mdd.sh
		sh install_mservice.sh
		sh install_mc.sh
		sh install_mservice-qing.sh
		sh install_fileserver.sh
		sh install_kafka.sh
		sh install_es.sh
		sh install_logstash.sh
#		sh install_zipkin.sh
#		sh install_harbor.sh
	fi
	
	if [ $? -eq 0 ];then
			echo "############################################################"
			echo "Congratulations,the Cosmic install successfully!"
			echo "############################################################"
	else
			echo "##################################################################################"
			echo -e "\033[31m Something wrong occured during the install of Cosmic,please check! \033[0m"
			echo "##################################################################################"
	fi

}

function main(){
	Before_Install
	Install_Cosmic | tee $pd/KDCC_installtool/log/kingdee_cloud_install.log
}
main 
