#!/bin/bash
action=$1
target=$2

source ~/.hcrc 2>/dev/null || echo ""
app_sc_dir=${app_sc_dir:-/ops_project/Health-cloud-server/data-center}
source ${app_sc_dir}/install.ini

#DB-compose文件DB_DEPLOY_DIR
COMPOSE_FILE=$(cat ${app_sc_dir}/compose_files 2>/dev/null || echo "")

#修改es连接信息(默认-->外部)
edit_es_info(){
		echo -e " 使用外部MQ"
		echo -e " 即将修改配置...."
		sleep 2
		sed -i -e "s#host: es#host: ${ES_HOST}#g"\
			-e "s#username: elastic#username: ${ES_USERNAME}#g"\
			-e "s#password: changeme#password: ${ES_PASSWORD}#g"\
			-e "s#httpPort: 9200#httpPort: ${ES_HTTP_PORT}#g"\
			-e "s#tcpPort: 9300#tcpPort: ${ES_TCP_PORT}#g" \
			-e "s#clusterName: elasticsearch-cluster#clusterName: ${ES_ClusterName}#g" ${APP_CONF_DIR}/application.yml
}
#修改mq连接信息(默认-->外部)
edit_mq_info(){
		echo -e " 使用外部MQ"
		echo -e " 即将修改配置...."
		sleep 2
		sed -i -e "s#host: rabbitmq01#host: ${MQ_HOST}#g"\
			-e "s#username: admin#username: ${MQ_USERNAME}#g"\
			-e "s#password: admin#password: ${MQ_PASSWORD}#g"\
			-e "s#port: 5672#port: ${MQ_PORT}#g"\
			-e "s#virtual-host: /#virtual-host: ${MQ_VHOST}#g" ${APP_CONF_DIR}/application.yml
}

function usage() {
   echo "Health-Cloud 控制脚本"
   echo
   echo "Usage: "
   echo "  ./hcctl.sh [COMMAND] [ARGS...]"
   echo "  ./hcctl.sh --help"
   echo
   echo "Commands: "
   echo "  status    查看 Health-Cloud 服务运行状态"
   echo "  start     启动 Health-Cloud 服务"
   echo "  stop      停止 Health-Cloud 服务"
   echo "  restart   重启 Health-Cloud 服务"
   echo "  uninstall 卸载 Health-Cloud 服务"
}

#docker-compose的基础配置

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


	#是否使用外部组件
	if [ ${INSTALL_MODE} == "all" ];then
			echo -e "正在创建数据目录"
			#mkdir -p ${db_sc_dir}/data/{es_data,mq_data}
			mkdir -p ${db_sc_dir}/data/{es_data,mq_data,mongo_data,redis_data,mysql_data}
			echo -e "正在创建日志目录"
			mkdir -p ${app_sc_dir}/log
	elif [ ${INSTALL_MODE} == "server" ];then
			echo -e "正在创建日志目录"
			mkdir -p ${app_sc_dir}/log
			edit_es_info
			edit_mq_info
	elif [ ${INSTALL_MODE} == "controller" ];then
		#判断es
		if [ ${ES_ISUSE_STATUS} == 'false' ];then
			echo -e "正在创建Elasticsearch数据目录"
			mkdir -p ${db_sc_dir}/data/es_data
			chown -R 1000:0 ${db_sc_dir}/data/es_data
			compose_files="${compose_files} -f ${DB_DEPLOY_DIR}/docker-compose-es.yaml -f ${app_sc_dir}/docker-compose-searchcenter.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
			echo -e "正在创建MQ数据目录"
			mkdir -p ${db_sc_dir}/data/es_data
			compose_files="${compose_files} -f ${DB_DEPLOY_DIR}/docker-compose-mq.yaml -f ${app_sc_dir}/docker-compose-searchcenter.yaml"
		elif [ ${MQ_ISUSE_STATUS} == 'true' ];then
			edit_mq_info
		else
			ehco "mq_isuse_status配置错误"
			exit 127
		fi
		echo -e "正在创建日志目录"
		mkdir -p ${app_sc_dir}/log
	fi
	#将所需要部署的组件ymal输出到文件
	echo "${compose_files} ">${app_sc_dir}/compose_files
	COMPOSE_FILE=$(cat ${app_sc_dir}/compose_files)
	echo "即将启动docker-compose部署"
	sudo docker-compose ${COMPOSE_FILE} up -d
}

function status(){
	echo
	cd ${app_sc_dir}
	docker-compose ${COMPOSE_FILE} ps
}
function start(){
	echo
	cd ${app_sc_dir}
	docker-compose ${COMPOSE_FILE} start
}
function stop(){
	echo
	cd ${app_sc_dir}
	docker-compose ${COMPOSE_FILE} stop
}
function restart(){
	echo
	cd ${app_sc_dir}
	docker-compose ${COMPOSE_FILE} stop
	docker-compose ${COMPOSE_FILE} start
}
function uninstall(){
	echo 
	cd ${app_sc_dir}
	docker-compose ${COMPOSE_FILE} down
}

function main(){
	case "${action}" in
		status)
			status
			;;
		start)
			start
			;;
		stop)
			stop
			;;
		restart)
			restart
			;;
		uninstall)
			uninstall
			;;
		--help)
			usage
			;;
		-h)
			usage
			;;
		install_compose-files)
		#	pwd
			echo "即将执行方法install-compose"
			install_compose-files
			;;
		*)
			echo
			cd ${app_sc_dir}
			docker-compose ${COMPOSE_FILES} $@
			;;
	esac

}
main $@