#!/bin/sh

source /etc/profile

## 应用名称
APP_NAME=cctp-flowable

## 镜像版本
VERSION=3.0-SNAPSHOT

## 公司名称
corporation=gzrdc

NACOS_SPACE=UI-DEV
NACOS_URL=158.1.12.237:8848
JVM=256M
PROFILE=dev

## 镜像仓库配置
DOCKER_REGISTRY=harbor.hzbtest:8443

##dockerfile 文件名配置
FILE_NAME=Dockerfile

while getopts "a:v:d:f:n:u:s:p:" arg
do
  case $arg in
    a)
      echo "set APP_NAME=$OPTARG"
      APP_NAME=$OPTARG
      ;;
    v)
      echo "VERSION=$OPTARG"
      VERSION=$OPTARG
      ;;
    d)
      echo "set DOCKER_REGISTRY=$OPTARG"
      DOCKER_REGISTRY=$OPTARG
      ;;
    f)
      echo "set FILE_NAME=$OPTARG"
      FILE_NAME=$OPTARG
      ;;
    n)
      echo "set NACOS_SPACE=$OPTARG"
      NACOS_SPACE=$OPTARG
      ;;
    u)
      echo "set NACOS_URL=$OPTARG"
      NACOS_URL=$OPTARG
      ;;
    s)
      echo "set JVM=$OPTARG"
      JVM=$OPTARG
      ;;
    p)
      echo "set PROFILE=$OPTARG"
      PROFILE=$OPTARG
      ;;
    ?)
      echo "unkown argument: $? $OPTARG "
      exit 1
      ;;
  esac
done


TAG_NAME=${DOCKER_REGISTRY}/${corporation}/${APP_NAME}
## deployment yaml
DEPLOYMENT_YAML=${APP_NAME}.deployment.yaml

shift $((OPTIND-1))

clean_Container() {
  ## 获取运行 容器ID
  RUN_ID=$(docker ps |grep ${APP_NAME}|awk '{print $1}')

  ## 先停止容器
  if [ "${RUN_ID}" = "" ]; then
    echo "Container ${APP_NAME} is not starting "
  else
    ## 停止 docker容器
    docker stop ${RUN_ID}
    echo "Container ${APP_NAME} , ID: ${RUN_ID} is stopped   "
  fi

  ## 获取已停止 容器ID
  STOP_ID=$(docker ps -a |grep ${APP_NAME}|awk '{print $1}')
  ## 删除容器
  if [ "${STOP_ID}" = "" ]; then
    echo "Container ${APP_NAME} is not exist"
  else
    ## 移除 docker容器
    docker rm -v ${STOP_ID}
    echo "Remove container ${APP_NAME} , ID: ${STOP_ID}  "
  fi

}

clean_Image() {
  ## 获取  镜像ID
  IMG_ID=$(docker images |grep ${APP_NAME}|awk '{print $3}')

  if [ "${IMG_ID}" = "" ]; then
    echo "Image ${TAG_NAME} is not exist "
  else
    ## 移除 docker镜像
    docker rmi ${TAG_NAME}
    echo "Remove image ${TAG_NAME} , ID: ${IMG_ID}  "
  fi
}

get_agent(){
  ## 获取agent 文件
  mkdir -p target/dist/trace-otel/
  wget -P target/dist/trace-otel/ http://devops.hzbtest:38081/repository/raw-distribution/rdc/uts/core/hzb-otel-javaagent.jar
  wget -P target/dist/trace-otel/ http://158.1.0.78:8081/repository/raw-distribution/rdc/uts/core/trace-otel.properties
  sed -i "s?SUBSYS_ZZZ?SUBSYS_ATU_FLOWABLE?g" target/dist/trace-otel/trace-otel.properties
  ## 添加jacoco的agent
  cp -r /home/northking/agent/jacoco-0.8.7 target/dist/
}

for command in $*
do
  echo "execute : ${command}"

  case "${command}" in
    clean)
      clean_Container
      clean_Image
    ;;
    build)
      $0 clean
      mvn --update-snapshots clean package
      get_agent
      ## 创建镜像
      docker build --build-arg SIZE=${JVM} --build-arg PROFILE=${PROFILE} --build-arg SPACE=${NACOS_SPACE} --build-arg URL=${NACOS_URL} -f  ${FILE_NAME}  -t ${TAG_NAME} target/
    ;;
    debug)
      clean_Container

      docker run -itd --net=host --privileged=true --name ${APP_NAME} ${TAG_NAME}
      sleep 2
      ## 输出容器日志
      RUN_ID=$(docker ps -a |grep ${APP_NAME}|awk '{print $1}')
      docker logs -f -t ${RUN_ID}
    ;;
    push)
      docker push ${TAG_NAME}
      docker tag ${TAG_NAME} ${TAG_NAME}:${VERSION}
      docker push ${TAG_NAME}:${VERSION}
      docker rmi -f ${TAG_NAME}
      docker rmi -f ${TAG_NAME}:${VERSION}
    ;;
    pull)
      $0 clean
      docker pull ${TAG_NAME}
    ;;
    deploy)
      kubectl delete -f ${DEPLOYMENT_YAML}
      sleep 2
      kubectl apply -f ${DEPLOYMENT_YAML}
      sleep 2
    ;;
    logs)
      for pod in $(kubectl -n cctp get pod |grep ${APP_NAME}|awk '{print $1}')
      do
        echo "get logs by pod=${pod}"
        kubectl -n cctp logs -f --tail=200 --follow=false ${pod}
      done
    ;;
   *)
   echo "Usage: $0 {clean|build|debug|push|deploy|logs}"
   exit 1
  esac
done

exit 0
