#!/bin/bash
source ./install.ini
sc_dir=${DEPLOY_DIR}/search-center

if [ ! -d ${sc_dir} ];then
	sudo cp -rf search-center ${sc_dir}
fi

#将配置文件存入环境变量文件，使其全局生效
sudo cp ./install.ini ${sc_dir}/install.ini.example
#sudo echo '' >${sc_dir}/.env
env_context=$(cat ${sc_dir}/install.ini.example |grep -v "^#"|grep -v "^$")
echo "${env_context}">${sc_dir}/.env
source ${sc_dir}/.env
sudo ln -s ${sc_dir}/.env ${sc_dir}/install.ini 2>/dev/null
cd ${sc_dir}

#docker-compose的基础配置
compose_files="-f docker-compose-base.yaml"
INSTALL_MODE=$1

#修改es连接信息为外部
edit_es_info(){
	    echo -e " 使用外部MQ"
		echo -e " 即将修改配置...."
		sleep 2
		grep -A 10 -w "es:" ${APP_CONF_DIR}/application.yml | \
	         sed -i -e "s#host: .*#host: ${ES_HOST}#g"\
			     -e "s#username: .*#username: ${ES_USERNAME}#g"\
			     -e "s#password: .*#password: ${ES_PASSWORD}#g"\
			     -e "s#httpPort: .*#httpPort: ${ES_HTTP_PORT}#g"\
			     -e "s#tcpPort: .*#tcpPort: ${ES_TCP_PORT}#g"
}
#修改mq信息为外部
edit_mq_info(){
	    echo -e " 使用外部MQ"
		echo -e " 即将修改配置...."
		sleep 2
        grep -A 6 -w "rabbitmq:" ${APP_CONF_DIR}/application.yml | \
                         sed -i -e "s#host: .*#host: ${MQ_HOST}#g"\
                             -e "s#username: .*#username: ${MQ_USERNAME}#g"\
                             -e "s#password: .*#password: ${MQ_PASSWORD}#g"\
                             -e "s#virtual-host: .*#virtual-host: ${MQ_VHOST}#g"
}
#--------------------部署-----------------------
echo -e "----------安装向导------------"
echo -e "- all模式-->全部安装(主服务seatch-center+数据库+队列)"
echo -e "- server模式-->只安装主服务search-center(请注意提前修改install.ini中外部db和mq的连接配置)"
echo -e "- controller模式-->安装主服务search-center+数据库 or 队列"
echo -e "------------------------------"
PS3='请选择安装模式: '

select INSTALL_MODE in "all" "server" "controller"
do
	case ${INSTALL_MODE} in 
	all)
		echo "即将进入all模式,这将安装search-center全部服务与组件"
		sleep 2
		compose_files="${compose_files} -f docker-compose-es.yaml -f docker-compose-mq.yaml -f docker-compose-searchcenter.yaml"
		break
	;;
	server)
		echo "即将进入server模式,这将只安装search-center服务"
		sleep 2
		compose_files="${compose_files} -f docker-compose-searchcenter.yaml"
		break
	;;
	controller)
		echo "即将进入crontroller模式,这将根据install.ini中您的所配置的(es,mq)是否使用外部来选择安装"
		sleep 2
		break
	;;
	*)
		echo "错误模式-请重新输入"
		exit 127
	esac
done

#是否使用外部组件
if [ ${INSTALL_MODE} == "all" ];then
	 sudo docker-compose ${compose_files} up -d
elif [ ${INSTALL_MODE} == "server" ];then
	 edit_es_info
	 edit_mq_info
     sudo docker-compose ${compose_files} up -d
elif [ ${INSTALL_MODE} == "controller" ];then
	#判断es
	if [ ${ES_ISUSE_STATUS} == 'false' ];then
		compose_files="${compose_files} -f docker-compose-es.yaml"
	elif [ ${ES_ISUSE_STATUS} == 'true' ];then
		edit_es_info
	else
		echo "es_isuse_status配置错误"
		exit 127
	fi
	#判断mq
	if [ ${MQ_ISUSE_STATUS} == 'false' ];then
		compose_files="${compose_files} -f docker-compose-mq.yaml"
    elif [ ${MQ_ISUSE_STATUS} == 'true' ];then
		edit_mq_info
	else
		ehco "mq_isuse_status配置错误"
		exit 127
	fi
	#将所需要部署的组件ymal输出到文件
	echo "${compose_files} -f docker-compose-searchcenter.yaml">${sc_dir}/compose_files
    compose_files=$(cat ${sc_dir}/compose_files)
    #echo ${compose_files}
    sudo docker-compose ${compose_files} up -d


fi
if [ $? -ne 0 ];then
	echo "安装过程出现错误，请查看相关配置..."
	exit 127
fi
echo "search-center 服务已安装成功..."

