#!/bin/bash

BRIGHT_RED='\033[38;5;196m'
BRIGHT_GREEN='\033[38;5;46m'
BRIGHT_YELLOW='\033[38;5;226m'
BRIGHT_BLUE='\033[38;5;21m'
BRIGHT_MAGENTA='\033[38;5;201m'
BRIGHT_CYAN='\033[38;5;51m'
BRIGHT_WHITE='\033[38;5;15m'
RESET='\033[0m' # 颜色重置


version=$1

if [ -z "$1" ]; then
  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [error] ${BRIGHT_RED} 请输入版本,例如:v1.0.0${RESET}"
else
  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] ${BRIGHT_RED}生成latest发布目录${RESET}"

  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] 下载latest镜像"
  docker pull registry.edoc2.com:5000/edoc2v5/autostress:latest
  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] 生成离线镜像包: autostresstest_latest.tar.gz"
  docker save registry.edoc2.com:5000/edoc2v5/autostress:latest>autostresstest_latest.tar
  tar zcf autostresstest_latest.tar.gz autostresstest_latest.tar
  rm -rf ./latest/autostresstest_latest.tar.gz
  mv autostresstest_latest.tar.gz ./latest/autostresstest_latest.tar.gz
  rm -rf autostresstest_latest.tar


  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] ${BRIGHT_RED}生成${version}发布目录${RESET}"

  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] 下载${version}镜像"
  docker pull registry.edoc2.com:5000/edoc2v5/autostress:${version}
  echo -e "[$(date +"%Y-%m-%d %H:%M:%S")] [info] 生成离线镜像包: autostresstest_${version}.tar.gz"
  docker save registry.edoc2.com:5000/edoc2v5/autostress:${version}>autostresstest_${version}.tar
  tar zcf autostresstest_${version}.tar.gz autostresstest_${version}.tar

  if [ -d "./${version}" ]; then
    rm -rf ./${version}/*
  else
    mkdir ./${version}
  fi

  mv autostresstest_${version}.tar.gz ./${version}/autostresstest_${version}.tar.gz
  rm -rf autostresstest_${version}.tar

  cp ./latest/*ml ./${version}/

  sed -i "s#latest#${version}#g" ./${version}/*ml
fi
