#!/bin/bash

TARGET=$1
# 获取最新版本号
VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)
if [[ "${VERSION}" = "" ]]
then
    VERSION=0.0.1-SNAPSHOT
fi

if [[ -z ${TARGET} ]]
then
  # 默认平台
  TARGET=admin
fi
rm -rf ${PROJECT_PATH}/dist
if [[ $TARGET = "admin" ]]
then
  cd front;
  npm run deploy:prod ${TARGET}
  cd ../;
  # mv admin/dist ./;
  export UPLOAD=front/dist
  export UPLOADED=static-${TARGET}
elif [[ $TARGET = "www" ]]
then
  cd front;
  npm run deploy:prod ${TARGET};
  cd ../;
  # mv web/dist  ./;
  export UPLOAD=front/dist
  export UPLOADED=static-${TARGET}
elif [[ $TARGET = "gateway" ]]
then
  rm -rf dist
  mkdir dist
  export ENV=$ENV
  cd server;
  ./gradlew :gateway-api:bootJar 
  cd ../

  mv server/gateway-api/build/libs/gateway-api-*-boot.jar dist/gateway-api-boot.jar
  cat server/pm2-gateway-api.json > dist/pm2.json
  chmod +x dist/*

  echo ${VERSION} > dist/LAST_VERSION

  export FORCE_UPLOAD=false
  export UPLOAD=dist
  export UPLOADED="gateway"
elif [[ $TARGET = "gateway-all" ]]
then
  rm -rf dist
  mkdir dist
  export ENV=$ENV
  cd server;
  ./gradlew :gateway-api:bootJar 
  cd ../

  mv server/gateway-api/build/libs/* dist/
  mv dist/gateway-api-*-boot.jar dist/gateway-api-boot.jar
  cat server/pm2-gateway-api.json > dist/pm2.json
  chmod +x dist/*

  echo ${VERSION} > dist/LAST_VERSION

  export FORCE_UPLOAD=true
  export UPLOAD=dist
  export UPLOADED="gateway"
fi
