cd ./build/autodeploy
set -x
echo $1
set -x
currentDate=$(date "+%Y%m%d%H%M%S")-$current_branch

cp /home/edoc2/dockercompose/$2/docker-compose.yml /home/edoc2/dockercompose/$2/docker-compose-edoc2.yml
cp /home/edoc2/dockercompose/$2/docker-compose-deploy.yml /home/edoc2/dockercompose/$2/docker-compose-deploy-edoc2.yml

sed -i "s/{PRODUCTION}/edoc2/g" /home/edoc2/dockercompose/$2/docker-compose-deploy.yml
sed -i "s/{PRODUCTION}/edoc2/g" /home/edoc2/dockercompose/$2/docker-compose-deploy-edoc2.yml

#if [ $1 = 'ci' ]; then
#	who am i
	#ssh edoc2@sd "cd /home/smb/ci/ && mkdir -p /home/smb/ci/$currentDate && cp /home/smb/middleware/$3/middleware-master-cluster.yml /home/smb/ci/$currentDate/middleware-master-cluster.yml && cp /home/smb/middleware/$3/middleware-master-standalone.yml /home/smb/ci/$currentDate/middleware-master-standalone.yml && cp /home/smb/middleware/$3/middleware-region-cluster.yml /home/smb/ci/$currentDate/middleware-region-cluster.yml && cp /home/smb/middleware/$3/middleware-region-standalone.yml /home/smb/ci/$currentDate/middleware-region-standalone.yml "

	#批量更新模板的端口
#	sed -i "s/{ports}/80/g" /home/edoc2/dockercompose/$1/*.yml

	#sed -i "s/{PRODUCTION}/InDrive/g" /home/edoc2/dockercompose/$1/docker-compose.yml
	#sed -i "s/{PRODUCTION}/edoc2/g" /home/edoc2/dockercompose/$1/docker-compose-edoc2.yml

	#scp -r /home/edoc2/dockercompose/$1/* edoc2@sd:/home/smb/ci/$currentDate/

#	scp  /home/edoc2/dockercompose/$1/envfile.env edoc2@192.168.251.78:/home/edoc2/autodeploy/envfile.env
#	scp  /home/edoc2/dockercompose/$1/docker-compose.yml edoc2@192.168.251.78:/home/edoc2/autodeploy/docker-compose.yml
#	ssh edoc2@192.168.251.78 "cd /home/edoc2/autodeploy && sudo rm -rf /dev/shm/db-ci && sudo mkdir -p /dev/shm/db-ci && COMPOSE_HTTP_TIMEOUT=200 && sudo docker stack rm $1"
#	ssh edoc2@192.168.251.78 "cd /home/edoc2/autodeploy && COMPOSE_HTTP_TIMEOUT=200 && sudo docker stack deploy -c 'docker-compose.yml' $1"
#else

ports=$(echo $1 | cut -d'-' -f3) #ci端口
ci=$(echo $1 | cut -d'-' -f1) #ci服务器
autodeploy=$(echo $1 | cut -d'-' -f4) #是否自动部署在服务器上

if [ -z "$ports"]; then
	ports=80
fi

if [ "$1" = 'inbiz-ci' ]; then
    dockerhost="192.168.251.78"
else
    dockerhost="172.16.2.78"
fi

sed -i "s/{dockerhost}/$dockerhost/g" /home/edoc2/dockercompose/$2/*.yml

#批量更新模板的端口
sed -i "s/{ports}/$ports/g" /home/edoc2/dockercompose/$2/*.yml

sed -i "s/{PRODUCTION}/edoc2/g" /home/edoc2/dockercompose/$2/docker-compose.yml
sed -i "s/{mysqlport}/$(($ports + 100))/g" /home/edoc2/dockercompose/$2/docker-compose.yml
sed -i "s/{mongodbport}/$(($ports + 200))/g" /home/edoc2/dockercompose/$2/docker-compose.yml
sed -i "s/{solrport}/$(($ports + 300))/g" /home/edoc2/dockercompose/$2/docker-compose.yml
sed -i "s/{rabbitmqport}/$(($ports + 400))/g" /home/edoc2/dockercompose/$2/docker-compose.yml
sed -i "s/{redisport}/$(($ports + 500))/g" /home/edoc2/dockercompose/$2/docker-compose.yml

sed -i "s/{ci-name}/$2/g" /home/edoc2/dockercompose/$2/deploy.ps1

cd /home/edoc2/dockercompose/$2/
rm -rf /home/cifile/${ci}/$2/$current_branch/ && mkdir -p /home/cifile/${ci}/$2/$current_branch/
cp -r * /home/cifile/${ci}/$2/$current_branch/

if [ "${autodeploy}" = 'autodeploy' ]; then
	rm -rf /dev/shm/db-$1 && mkdir -p /dev/shm/db-$1 && rm -rf /home/edoc2/autodeploy/$2 && mkdir -p /home/edoc2/autodeploy/$2 && COMPOSE_HTTP_TIMEOUT=200 && sudo docker stack rm $2
	docker stack deploy -c 'docker-compose.yml' $2 --resolve-image never
fi

rm -rf /opt/$CI_PIPELINE_ID
