#!/bin/bash
#进入项目目录
#cd /Users/wucongxing/Desktop/work/php/hospital-applets-api  || exit
cd /data/www/hospital-applets-api  || exit

#构建env
echo "
APP_NAME=gdxz
APP_ENV=dev
DOMAIN_NAME_DEV=https://dev.hospital.applets.igandanyiyuan.com/
DOMAIN_NAME_PROD=https://dev.hospital.applets.igandanyiyuan.com/

# [mysql]
DB_DRIVER=mysql
DB_HOST=121.5.206.61
DB_PORT=3306
DB_DATABASE=internet_hospital
DB_USERNAME=root
DB_PASSWORD=Wucongxing1..
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
DB_PREFIX=gdxz_

# [redis]
REDIS_HOST=121.5.206.61
REDIS_AUTH=Wucongxing1..
REDIS_PORT=6379
REDIS_DB=0

# [jwt]
JWT_SECRET=X8p44RvrFDlnrvHLN2juwK1sSAlFtfvdZJLLKt97DLf50W7TPOzCKDUZdVkW+PZzWIqPT8fyoXGBAcBK2faHiA==
JWT_TTL=604800
JWT_ALGO=HS256

# [PATIENT]
#  [WECHAT]
PATIENT_WECHAT_APP_ID=wx70a196902e0841b6
PATIENT_WECHAT_APP_SECRET=2671d2f4285180ddec5a5a2b16ed50f2
PATIENT_WECHAT_INQUIRY_PAY_NOTIFY_URL=callback/wxpay/inquiry/success
PATIENT_WECHAT_INQUIRY_REFUND_NOTIFY_URL=callback/wxpay/inquiry/refund
PATIENT_WECHAT_PRODUCT_PAY_NOTIFY_URL=callback/wxpay/product/success
PATIENT_WECHAT_PRODUCT_REFUND_NOTIFY_URL=callback/wxpay/product/refund

# [DOCTOR]
#  [WECHAT]
DOCTOR_WECHAT_APP_ID=wxc83296720404aa7b
DOCTOR_WECHAT_APP_SECRET=817665d3763637fe66d56548f8484622

# [AMQP]
AMQP_HOST=42.193.16.243
AMQP_PORT=5672
AMQP_USER=gdxz_2022rabbitmq
AMQP_PASSWORD=qwr2p&¥e@3.2p
AMQP_VHOST=gdxz_2022rabbitmq

# [CA]
CA_APP_ID=SCCA1560150113940832258
CA_APP_SECRET=facded39929f4aef9893cb766cd52e25

# [处方平台]
PRE_PLAT_CLIENT_ID=ZD-004
PRE_PLAT_CLIENT_SECRET=0baa5927164710b9f800bf33546b6da3
PRE_PLAT_APP_URL=http://49.233.3.200:6304/api/thridapi/
">.env

#nginx_upstrame="/Users/wucongxing/Desktop/test/hospital-upstream.conf"
nginx_upstrame="/etc/nginx/upstream/hospital-upstream.conf"
#echo nginx_upstrame

#确定可使用端口
online_port=0
reload_port=0

ports_list=(9508 9509)

for i in "${ports_list[@]}";
do
  ports_count=$(lsof -i:$i | wc -l)
  if [[ $ports_count -eq 0 ]]; then
      # 未占用 可使用
      reload_port=$i
  fi

  if [[ $ports_count -ne 0 ]]; then
      # 占用 需重启
      online_port=$i
  fi
done

if [[ $online_port == 0 ]]; then
    reload_port=${ports_list[1]}
else
    echo "当前端口 $online_port 使用中"
    echo "重启使用端口 $reload_port"
fi

if [[ $reload_port == 0 ]]; then
 echo '部署失败, 未获取到可用端口'
 exit 1;
fi

# 定义负载
upstrame="
upstream hospital {
    server 127.0.0.1:$reload_port;
}
"

echo "$upstrame" > $nginx_upstrame

# 复制证书
cp -r /data/www/cret/wechat/pay/1636644248/ ./extend/Wechat/certs/

#获取可用端口镜像数量
image=$(docker images "hospital-applets-api-$reload_port" | wc -l)
if [[ $image -eq 1 ]];then
  # 无可用端口镜像
  # 可用端口构建镜像
  echo '可用端口构建镜像'
  docker build -t "hospital-applets-api-$reload_port" .
else
  # 存在可用端口镜像，删除镜像容器
  docker rm "hospital-applets-api-$reload_port"

  echo '可用端口构建镜像'
  docker build -t "hospital-applets-api-$reload_port" .
fi

echo '启动新端口容器'
docker run --name "hospital-applets-api-$reload_port" -d -p "$reload_port":9501 "hospital-applets-api-$reload_port":latest

echo '重启nginx'
nginx -s reload

if [[ $online_port -ne 0 ]]; then
  echo '停止原端口容器'
  docker stop "hospital-applets-api-$online_port"

  echo '删除原端口容器'
  docker rm "hospital-applets-api-$online_port"

  echo '删除原端口镜像'
  docker rmi "hospital-applets-api-$online_port"
fi

echo "构建成功"