project_name=$1
tag=$2
target_user='root'
target_ip='172.30.220.22'
target_dir="/home/quant_group/${project_name}"

qaapi='http://172.30.220.22:3003'
git_path=`curl -s "${qaapi}/proconfig/get_info?system_name=${project_name}&key=git_path"`
build_command=`curl -s "${qaapi}/proconfig/get_info?system_name=${project_name}&key=build_command"`

if [ ! -d "$project_name" ];then
	echo "not exist"
	git clone ${git_path}
	cd ./${project_name}
	ssh ${target_user}@${target_ip} "mkdir -p ${target_dir}/"
else
	echo "exist"
	cd ./${project_name}
	git pull
fi

npm install
$build_command

tar zcf ${tag}.dist.tgz ./dist/
scp ${tag}.dist.tgz ${target_user}@${target_ip}:${target_dir}
ssh ${target_user}@${target_ip} "mkdir -p ${target_dir}/${tag}"
ssh ${target_user}@${target_ip} "tar zxf ${target_dir}/${tag}.dist.tgz -C /${target_dir}/${tag}"
ssh ${target_user}@${target_ip} "rm -rf ${target_dir}/dist"
ssh ${target_user}@${target_ip} "ln -s ${target_dir}/${tag}/dist/ ${target_dir}/dist"
ssh ${target_user}@${target_ip} "rm -rf ${target_dir}/*.dist.tgz"
rm -rf *.dist.tgz
echo "done"
