#!/bin/sh
export deploy_path=/opt/builds/${JOB_NAME}/${BUILD_NUMBER}
echo "--------moveFile start--------------"
rm -rf $deploy_path
mkdir -p $deploy_path
cd $deploy_path
rm -rf *.jar
echo "workspace is ${WORKSPACE}"
mv ${WORKSPACE}/sgzb-auth/target/sgzb-auth-nw24.8.3.jar $deploy_path
mv ${WORKSPACE}/sgzb-gateway/target/sgzb-gateway-nw24.8.3.jar $deploy_path
mv ${WORKSPACE}/sgzb-modules/sgzb-system/target/sgzb-modules-system-nw24.8.3.jar $deploy_path
mv ${WORKSPACE}/sgzb-modules/sgzb-base/target/sgzb-modules-base-nw24.8.3.jar $deploy_path
mv ${WORKSPACE}/sgzb-modules/sgzb-file/target/sgzb-modules-file-nw24.8.3.jar $deploy_path
mv ${WORKSPACE}/sgzb-modules/sgzb-material/target/sgzb-modules-material-nw24.8.3.jar $deploy_path
chmod 777 *.jar
echo "--------moveFile end--------------"
export sgzb_auth_enable=true
export sgzb_gateway_enable=true
export sgzb_modules_system_enable=true
export sgzb_modules_base_enable=false   #已去掉base服务
export sgzb_modules_file_enable=false   #暂时不需要file服务
export sgzb_modules_material_enable=true
 
export sgzb_auth_jar=sgzb-auth-nw24.8.3.jar
export sgzb_gateway_jar=sgzb-gateway-nw24.8.3.jar
export sgzb_modules_system_jar=sgzb-modules-system-nw24.8.3.jar
export sgzb_modules_base_jar=sgzb-modules-base-nw24.8.3.jar
export sgzb_modules_file_jar=sgzb-modules-file-nw24.8.3.jar
export sgzb_modules_material_jar=sgzb-modules-material-nw24.8.3.jar

export sgzb_auth_port=49200
export sgzb_gateway_port=49080
export sgzb_modules_base_port=49301
export sgzb_modules_file_port=9300
export sgzb_modules_material_port=49302
export sgzb_modules_system_port=49201

if($auth_service_enable);then
	echo "--------auth开始启动--------------"
	echo "--------删除auth开始--------------"
	 P_ID=`ps -ef | grep -w $sgzb_auth_jar | grep -v "grep" | awk '{print $2}'`
		if [ "$P_ID" == "" ]; then
			echo "===auth service process not exists or stop success"
		else
			kill -9 $P_ID
			echo "auth service killed success"
		fi
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_auth_jar >/dev/null 2>&1 &
	sgzb_auth_pid=`lsof -i:$sgzb_auth_port|grep "LISTEN"|awk '{print $2}'`
	until [ -n "$sgzb_auth_pid" ]
		do
		  sgzb_auth_pid=`lsof -i:$sgzb_auth_port|grep "LISTEN"|awk '{print $2}'`
		done
	echo "auth service pid is $sgzb_auth_pid"
	echo "--------auth_service 启动成功--------------"
fi

if($sgzb_gateway_enable);then
	echo "--------gateway 开始启动--------------"
	echo "--------删除gateway开始--------------"
	P_ID=`ps -ef | grep -w $sgzb_gateway_jar | grep -v "grep" | awk '{print $2}'`
  if [ "$P_ID" == "" ]; then
    echo "===gateway process not exists or stop success"
  else
    kill -9 $P_ID
    echo "gateway killed success"
  fi
  echo "--------删除gateway开始2-------------- $sgzb_gateway_port"
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_gateway_jar >/dev/null 2>&1 &
	echo "--------删除gateway开始3-------------- $sgzb_gateway_port"
	sgzb_gateway_pid=`lsof -i:$sgzb_gateway_port|grep "LISTEN"|awk '{print $2}'`
	echo "--------删除gateway开始4-------------- $sgzb_gateway_pid"
	until [ -n "$sgzb_gateway_pid" ]
		do
		  echo "--------删除gateway开始5-------------- $sgzb_gateway_pid"
		  sgzb_gateway_pid=`lsof -i:$sgzb_gateway_port|grep "LISTEN"|awk '{print $2}'`
		  echo "--------删除gateway开始6-------------- $sgzb_gateway_pid"
		done
	echo "gateway pid is $sgzb_gateway_pid"
	echo "--------gateway 启动成功--------------"
fi


if($sgzb_modules_system_enable);then
	echo "--------sgzb_modules_system 开始启动--------------"
	echo "--------删除sgzb_modules_system开始--------------"
	 P_ID=`ps -ef | grep -w $sgzb_modules_system_jar | grep -v "grep" | awk '{print $2}'`
		if [ "$P_ID" == "" ]; then
			echo "===sgzb_modules_system process not exists or stop success"
		else
			kill -9 $P_ID
			echo "sgzb_modules_system killed success"
		fi
	echo "--------sgzb_modules_system开始部署--------------"
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_modules_system_jar >/dev/null 2>&1 &
	sgzb_modules_system_pid=`lsof -i:$sgzb_modules_system_port|grep "LISTEN"|awk '{print $2}'`
	until [ -n "$sgzb_modules_system_pid" ]
		do
		  sgzb_modules_system_pid=`lsof -i:$sgzb_modules_system_port|grep "LISTEN"|awk '{print $2}'`
		done
	echo "sgzb_modules_system pid is $sgzb_modules_system_pid"
	echo "--------sgzb_modules_system 启动成功--------------"
fi

if($sgzb_modules_base_enable);then
	echo "--------sgzb_modules_base 开始启动--------------"
	echo "--------删除sgzb_modules_base开始--------------"
	 P_ID=`ps -ef | grep -w $sgzb_modules_base_jar | grep -v "grep" | awk '{print $2}'`
		if [ "$P_ID" == "" ]; then
			echo "===sgzb_modules_base process not exists or stop success"
		else
			kill -9 $P_ID
			echo "sgzb_modules_base killed success"
		fi
	echo "--------sgzb_modules_base开始部署--------------"
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_modules_base_jar >/dev/null 2>&1 &
	sgzb_modules_base_pid=`lsof -i:$sgzb_modules_base_port|grep "LISTEN"|awk '{print $2}'`
	until [ -n "$sgzb_modules_base_pid" ]
		do
		  sgzb_modules_base_pid=`lsof -i:$sgzb_modules_base_port|grep "LISTEN"|awk '{print $2}'`
		done
	echo "sgzb_modules_base pid is $sgzb_modules_base_pid"
	echo "--------sgzb_modules_base 启动成功--------------"
fi

if($sgzb_modules_file_enable);then
	echo "--------sgzb_modules_file 开始启动--------------"
	echo "--------删除sgzb_modules_file开始--------------"
	 P_ID=`ps -ef | grep -w $sgzb_modules_file_jar | grep -v "grep" | awk '{print $2}'`
		if [ "$P_ID" == "" ]; then
			echo "===sgzb_modules_file process not exists or stop success"
		else
			kill -9 $P_ID
			echo "sgzb_modules_file killed success"
		fi
	echo "--------sgzb_modules_file开始部署--------------"
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_modules_file_jar >/dev/null 2>&1 &
	sgzb_modules_file_pid=`lsof -i:$sgzb_modules_file_port|grep "LISTEN"|awk '{print $2}'`
	until [ -n "$sgzb_modules_file_pid" ]
		do
		  sgzb_modules_file_pid=`lsof -i:$sgzb_modules_file_port|grep "LISTEN"|awk '{print $2}'`
		done
	echo "sgzb_modules_file pid is $sgzb_modules_file_pid"
	echo "--------sgzb_modules_file 启动成功--------------"
fi

if($sgzb_modules_material_enable);then
	echo "--------sgzb_modules_material 开始启动--------------"
	echo "--------删除sgzb_modules_material开始--------------"
	 P_ID=`ps -ef | grep -w $sgzb_modules_material_jar | grep -v "grep" | awk '{print $2}'`
		if [ "$P_ID" == "" ]; then
			echo "===sgzb_modules_material process not exists or stop success"
		else
			kill -9 $P_ID
			echo "sgzb_modules_material killed success"
		fi
	echo "--------sgzb_modules_material开始部署--------------"
	nohup /usr/lib/jvm/jdk1.8.0_381/bin/java  -jar $sgzb_modules_material_jar >/dev/null 2>&1 &
	sgzb_modules_material_pid=`lsof -i:$sgzb_modules_material_port|grep "LISTEN"|awk '{print $2}'`
	until [ -n "$sgzb_modules_material_pid" ]
		do
		  sgzb_modules_material_pid=`lsof -i:$sgzb_modules_material_port|grep "LISTEN"|awk '{print $2}'`
		done
	echo "sgzb_modules_material pid is $sgzb_modules_material_pid"
	echo "--------sgzb_modules_material 启动成功--------------"
fi