#!/bin/bash

#记录开始时间
start=$(date +%s)

# 默认参数
branch="master"
service="manage-front"


show_help() {
  echo "Usage: $0 [-b branch] [-s service]"
  echo
  echo "Options:"
  echo "  -b  Branch to deploy, default is master"
  echo "  -s  Service to deploy (manage-front, manage, front, all, system, default is manage-front)"
  echo "  -h  Show this help message"
}

# 解析命令行选项
while getopts "b:s:h" opt; do
  case $opt in
    b) branch=$OPTARG ;;
    s) service=$OPTARG ;;
    h) show_help; exit 0 ;;
    \?) echo "Invalid option -$OPTARG" >&2; show_help; exit 1 ;;
  esac
done

echo "branch: $branch"
echo "service: $service"

valid_services=("manage-front" "manage" "front" "all" "auth" "gateway" "system")
if [[ ! " ${valid_services[@]} " =~ " $service " ]]; then
  echo "Invalid service name: $service" >&2
  echo "Valid services are: ${valid_services[*]}"
  exit 1
fi

# 登录到目标环境
baseDir="/home/somuns/ci4s"
cd ${baseDir}

# 创建目录
mkdir -p ${baseDir}/k8s/dockerfiles/jar
mkdir -p ${baseDir}/k8s/dockerfiles/html

compile_front() {
    # 清理前端构建文件
    if [ -d "${baseDir}/react-ui/dist" ]; then
       rm -rf ${baseDir}/react-ui/dist
    fi

    # 编译前端
    docker run -v ${baseDir}:${baseDir} \
      -e http_proxy=http://172.20.32.253:3128 -e https_proxy=http://172.20.32.253:3128 \
      172.20.32.187/tempimagefile/node:18.16.0 ${baseDir}/k8s/build-node.sh
    if [ $? -ne 0 ]; then
        echo "编译失败，请检查代码！"
        exit 1
    fi

    # 复制前端文件
    cp -rf ${baseDir}/react-ui/dist/ ${baseDir}/k8s/dockerfiles/html
    if [ $? -ne 0 ]; then
        echo "复制html文件失败，请检查代码！"
        exit 1
    fi
}

compile_java() {
  param=$1
    # 编译java
  docker run -v ${baseDir}:${baseDir} -v /home/maven:/home/maven \
    -e http_proxy=http://172.20.32.253:3128 -e https_proxy=http://172.20.32.253:3128 \
    172.20.32.187/ci4s/build:v3 ${baseDir}/k8s/build-java.sh $param
  if [ $? -ne 0 ]; then
      echo "编译失败，请检查代码！"
      exit 1
  fi

  # 复制jar包
  if [ "$param" == "manage-front" ] || [ "$param" == "manage" ]; then
    cp -rf ${baseDir}/ruoyi-modules/management-platform/target/management-platform.jar ${baseDir}/k8s/dockerfiles/jar/management-platform.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi
  fi

  if [ "$param" == "gateway" ]; then
    cp -rf ${baseDir}/ruoyi-gateway/target/ruoyi-gateway.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-gateway.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi
  fi

  if [ "$param" == "auth" ]; then
    cp -rf ${baseDir}/ruoyi-auth/target/ruoyi-auth.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-auth.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi
  fi

  if [ "$param" == "system" ]; then
    cp -rf ${baseDir}/ruoyi-modules/ruoyi-system/target/ruoyi-modules-system.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-modules-system.jar
        if [ $? -ne 0 ]; then
            echo "复制jar包失败，请检查代码！"
            exit 1
        fi
  fi

  if [ "$param" == "all" ]; then
    cp -rf ${baseDir}/ruoyi-modules/management-platform/target/management-platform.jar ${baseDir}/k8s/dockerfiles/jar/management-platform.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi

    cp -rf ${baseDir}/ruoyi-modules/ruoyi-system/target/ruoyi-modules-system.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-modules-system.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi

    cp -rf ${baseDir}/ruoyi-auth/target/ruoyi-auth.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-auth.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi

    cp -rf ${baseDir}/ruoyi-gateway/target/ruoyi-gateway.jar ${baseDir}/k8s/dockerfiles/jar/ruoyi-gateway.jar
    if [ $? -ne 0 ]; then
        echo "复制jar包失败，请检查代码！"
        exit 1
    fi
  fi
}

if [ "$service" == "front" ]; then
  # 编译前端
  compile_front
fi

if [ "$service" == "manage-front" ]; then
  # 编译前端
  compile_front
  # 编译java
  compile_java "manage"
fi

if [ "$service" != "manage-front" ] && [ "$service" != "all" ] && [ "$service" != "front" ]; then
  # 编译java
  compile_java $service
fi

if [ "$service" == "all" ]; then
  # 编译前端
  compile_front

  # 编译java
  compile_java "all"
fi


# 记录结束时间
end=$(date +%s)

#计算运行时间
runtime=$((end-start))
echo "编译成功，耗时：$runtime 秒"
