#!/bin/bash

echo "=========================="
echo "====  check service  ====="
echo "=========================="


servicetatus="/tmp/servicetatus.sh"


function check(){
	
	_type=$1
	_name=$2
	_port=$3/Users/suowentao/Documents/GITRES/qa-deploy-utils/qa_shell_script/check/check.sh
	_desc=$4
	_bleo=$5
	_targ=$6
	
	

	#echo "$result" "$key"
	#verify "$result" "$key"
    #isstart=$?
	
	_name_c=''
	_status=''
	_desc_c=''
	#echo $_name "curl -s ${host}:${_port}"
	result=`curl -s ${host}:${_port}`
	
	if [[ $? == 0 ]]
    then
    	_status='\033[32m start \033[0m'
    	
	else
		_status='\033[31m stop  \033[0m'
	fi

	if [[ ${#_name} -lt 8 ]]
	then
		_name_c="${_name}\t\t\t"
	fi

	if [[ ${#_name} -ge 8 && ${#_name} -lt 16 ]]
	then
		_name_c="${_name}\t\t"
	fi

	if [[ ${#_name} -ge 16 ]]
	then
		_name_c="${_name}\t"
	fi

	if [[ $_bleo =~ "${_targ}"  ]]
    then
    	_desc_c="\033[32m ${_desc} \033[0m"
    else
    	_desc_c=" ${_desc} "
	
	fi




	echo "echo -e '${_status}\t|\t${_type}\t|\t${_name_c}\t${_desc_c}' "  >>$servicetatus
	echo "echo '----------------------------------------------------------------------------------------------'" >>$servicetatus
}



function info(){
	echo "echo -e '\033[31m ----  \033[0m\t\t\t|\t$1'   "  >>$servicetatus
}

rm -rf $servicetatus

echo "#!/bin/bash" >>"/tmp/servicetatus.sh"
echo "echo '----------------------------------------------------------------------------------------------'" >>"servicetatus.sh"

host="127.0.0.1"

projectStr=`node project_item`
echo $projectStr


for line in ${projectStr[@]}; do


    check ${line//|@|/ }

done


info "mysql"
info "zookeeper"
info "redis"

chmod 755 $servicetatus

sh $servicetatus
