#!/bin/bash

# 记录开始时间
start=$(date +%s)
# 默认参数
service="manage-front"
env="dev"

show_help() {
  echo "Usage: $0 [-s service] [-e environment]"
  echo
  echo "Options:"
  echo "  -s  Service to deploy (manage-front, manage, front, all, system default: manage-front)"
  echo "  -e  Environment (e.g., dev, test, default: dev)"
  echo "  -h  Show this help message"
}

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

echo "Deploy service: $service, environment: $env"

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

valid_envs=("dev" "test" "test1")
if [[ ! " ${valid_envs[@]} " =~ " $env " ]]; then
  echo "Invalid environment: $env" >&2
  echo "Valid environments are: ${valid_envs[*]}"
  exit 1
fi

# 根据环境设置 IP 地址
if [ "$env" == "dev" ]; then
  remote_ip="172.20.32.197"
elif [ "$env" == "test" ]; then
  remote_ip="172.20.32.185"
elif [ "$env" == "test1" ]; then
  remote_ip="172.20.32.235"
else
  echo "Invalid environment - $env"
  exit 1
fi

baseDir=/home/somuns/ci4s
tag=$(date +'%Y%m%d%H%M')
remote_deploy_dir=/home/deploy/manage-platform

# 构建镜像函数
build_image() {
  local dockerfile=$1
  local image=$2
  cd ${baseDir}/k8s/dockerfiles
  docker build -t ${image} -f ${dockerfile} .
  if [ $? -ne 0 ]; then
    echo "Build ${image} image fail"
    exit 1
  fi
  docker push ${image}
}

# 复制和替换 YAML 文件函数
prepare_yaml() {
  local yaml_file=$1
  local image=$2

  placeholder="\${${yaml_file%.yaml}-image}"
  cd ${baseDir}/k8s/template-yaml
  cp -rf ${yaml_file} deploy/
  cd deploy/
  sed -i "s|${placeholder}|${image}|g" ${yaml_file}
  if [ $? -ne 0 ]; then
    echo "Replace ${image} image fail"
    exit 1
  fi

  # 建立远程目录并备份文件
  ssh root@$remote_ip "mkdir -p ${remote_deploy_dir} && if [ -f ${remote_deploy_dir}/${yaml_file} ]; then mv ${remote_deploy_dir}/${yaml_file} ${remote_deploy_dir}/${yaml_file}.bak; fi"
  if [ $? -ne 0 ]; then
    echo "Failed to create remote directory or backup ${yaml_file}"
    exit 1
  else
    echo "Successfully created remote directory and backup ${yaml_file}"
  fi

  scp ${baseDir}/k8s/template-yaml/deploy/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
  if [ $? -ne 0 ]; then
    echo "Failed to copy ${yaml_file}"
    exit 1
  else
    echo "Successfully copied ${yaml_file}"
  fi
}

# 部署服务函数
deploy_service() {
  local yaml_file=$1
  ssh root@$remote_ip "kubectl apply -n argo -f ${remote_deploy_dir}/${yaml_file}"
  if [ $? -ne 0 ]; then
    echo "Failed to deploy ${yaml_file}"
    exit 1
  else
    echo "Successfully deployed ${yaml_file}"
  fi
}

deploy_nacos() {
     local yaml_file=$1
     scp ${baseDir}/k8s/template-yaml/${yaml_file} root@$remote_ip:${remote_deploy_dir}/${yaml_file}
     deploy_service ${yaml_file}
}

build_and_deploy() {
  local dockerfile=$1
  local image=$2
  local yaml_file=$3

  build_image ${dockerfile} ${image}
  prepare_yaml ${yaml_file} ${image}
  deploy_service ${yaml_file}
}

if [ "$service" == "front" ]; then
  build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}"  "k8s-12front.yaml"
fi

# 构建和部署 manage 服务
if [ "$service" == "manage" ]; then
  build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}"  "k8s-7management.yaml"
fi

if [ "$service" == "auth" ]; then
  #部署认证中心
  build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}"  "k8s-5auth.yaml"
fi

if [ "$service" == "gateway" ]; then
  #部署网关
  build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}"  "k8s-4gateway.yaml"
fi

if [ "$service" == "system" ]; then
  #部署系统服务
  build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}"  "k8s-6system.yaml"
fi

# 构建和部署 front 服务
if [ "$service" == "manage-front" ]; then
  build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}"  "k8s-12front.yaml"
  build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}"  "k8s-7management.yaml"
fi


if [ "$service" == "all" ]; then
  #部署前端
  build_and_deploy "nginx-dockerfile" "172.20.32.187/ci4s/ci4s-front:${tag}"  "k8s-12front.yaml"
  #部署管理平台
  build_and_deploy "managent-dockerfile" "172.20.32.187/ci4s/ci4s-managent:${tag}"  "k8s-7management.yaml"
  #部署认证中心
  build_and_deploy "auth-dockerfile" "172.20.32.187/ci4s/ci4s-auth:${tag}"  "k8s-5auth.yaml"
  #部署网关
  build_and_deploy "gateway-dockerfile" "172.20.32.187/ci4s/ci4s-gateway:${tag}"  "k8s-4gateway.yaml"
  #部署系统服务
  build_and_deploy "system-dockerfile" "172.20.32.187/ci4s/ci4s-system:${tag}"  "k8s-6system.yaml"
  #部署配置中心
  deploy_nacos "k8s-3nacos.yaml"
fi


# 记录结束时间
end=$(date +%s)
echo "部署成功, 耗时: $((end-start))秒"
