#!/bin/bash

batch=${DRONE_COMMIT_BRANCH}

if [ -d "/app/build/${batch}" ]; then
  echo "Folder exists."
else
  mkdir "/app/build/${batch}"
fi

rm -rf /app/build/"${batch}"/*
cp start.sh /app/build/"${batch}"/ # java包的启动脚本
cp Dockerfile /app/build/"${batch}"/ # docker镜像打包时的文件
cp deployment.yml /app/build/"${batch}"/ # k8s部署文件
cp .dockerignore /app/build/"${batch}"/ # docker镜像打包时过滤文件
cp docker.sh /app/build/"${batch}"/ # docker 镜像打包脚本

# shellcheck disable=SC2039
batchArr=("admin-biz:admin"
          "notice-biz:notice"
          "log-biz:log"
          "custom-query-biz:custom-query"
#          "workflow:visual"
          "monitor:visual"
#          "code-gen:visual"
          "sentinel-dashboard:visual"
          "code-gen-test-mysql:visual"
          "sentinel-dashboard-pro:visual"
          "string3:field3")
batch=${DRONE_COMMIT_BRANCH}

ignoreArr=("monitor","sentinel-dashboard-pro","sentinel-dashboard")
if [[ "${ignoreArr[*]}" =~ "${batch}" ]]; then
  echo "exist";
  cp -r /app/build/skywalking-agent /app/build/$DRONE_COMMIT_BRANCH/skywalking-agent
  rm -rf /app/build/$DRONE_COMMIT_BRANCH/skywalking-agent/*
else
  echo "does not exist";
  cp -r /app/build/skywalking-agent /app/build/$DRONE_COMMIT_BRANCH/skywalking-agent
  cp agent.config /app/build/$DRONE_COMMIT_BRANCH/skywalking-agent/config/agent.config
fi



if [ "$batch" != 'gateway' ]; then
  rm -rf /app/build/$DRONE_COMMIT_BRANCH/skywalking-agent/plugins/apm-spring-cloud-gateway-3.x-plugin-8.13.0.jar
fi

path="$batch"
for item in "${batchArr[@]}"
do
  if [[ "$item" == "$batch:"* ]]; then
    path="${item##*:}/$path"
    echo "字符串变量存在于数组中，对应的字段值为 $path"
    break
  fi
done
cp common/common-log/src/main/resources/logback-dev.xml "${path}"/src/main/resources
cp common/common-log/src/main/resources/logback-pro.xml "${path}"/src/main/resources
echo "****************************************************************************"
ls "${path}"/src/main/resources
mvn clean package -DskipTests=true # 应用打包命
cd "$path"
cp target/"${batch}".jar /app/build/"${batch}"/"${batch}".jar
