#!/bin/sh

source /etc/profile

## 应用名称
APP_NAME=atu-script-engine

## 镜像版本
VERSION=1.0.1-SNAPSHOT
APP_PORT=6001

## 公司名称
corporation=gzrdc

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

##dockerfile 文件名配置

FILE_NAME=Dockerfile

while getopts "f:" arg
do
  case $arg in
    f)
      echo "set FILE_NAME=$OPTARG"
      FILE_NAME=$OPTARG
      ;;
    ?)
      echo "unkown argument: $? $OPTARG "
      exit 1
      ;;
  esac
done

TAG_NAME=${DOCKER_REGISTRY}/${corporation}/${APP_NAME}:${VERSION}

logPath=/home/northking/logs

## 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_ENGINE?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
      get_agent
      ## 创建镜像
      docker build -f  ${FILE_NAME} -t ${TAG_NAME} .
      $0 debug
    ;;
    debug)
      clean_Container
      docker run -itd --net=host --name ${APP_NAME} -p ${APP_PORT}:${APP_PORT} -v ${logPath}/atu-engine:/home/cctp/logs  -v /home/northking/docker/.h2/engine:/home/northking/engine/.h2/engine ${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