#!/bin/bash

# 约定内容

NeedUploadFolder=99
IsUpload=99
UploadImageName=""
ImageListName=""
ImageTarName=""
ImageName=""
ImageTag=""
InnerIPv4CIDR=""
InnerIPv6CIDR=""
InnerIPv4=""
InnerIPv6=""
CmiiImagePrefix="harbor.cdcyy.com.cn/cmii/"
OSS_URL="https://oss.demo.uavcmlc.com:18000/cmlc-installation/tmp/"
Custom_Client_Harbor="REPLACE:8033"

#######################################
# description
# Globals:
#   ImageName
#   ImageTag
#   ImageTarName
#   RANDOM
#   UploadImageName
# Arguments:
#  None
#######################################
get_image_tar_name() {

  # harbor.cdcyy.com.cn/cmii/cmii-uav-oauth:4.1.6
  ImageName=$(echo $UploadImageName | cut -d ":" -f1 | cut -d"/" -f3)
  ImageTag=$(echo $UploadImageName | cut -d ":" -f2)
  local currentDate=$(date +'%Y-%m-%d')
  local random_number=$((RANDOM % 900 + 100))

  ImageTarName="$ImageName=$ImageTag=$currentDate=$random_number.tar.gz"
}

#######################################
# description
# Globals:
#   ImageName
#   ImageTag
#   ImageTarName
#   UploadImageName
# Arguments:
#  None
#######################################
get_image_name_from_tar() {

  # cmii-uav-oauth_4.1.6-0918_20230918_123.tar.gz

  ImageName=$(echo $ImageTarName | cut -d "=" -f1)
  ImageTag=$(echo $ImageTarName | cut -d"=" -f2)

  UploadImageName="$ImageName:$ImageTag"
}

# 获取服务器的公网IP地址
get_Internal_IP_CIDR() {

  local interface_prefix=("[[:space:]]eth[0-9]{1,2}" "[[:space:]]ens[0-9]{1,3}" "[[:space:]]eno[0-9]{1,3}" "[[:space:]]enp[0-9]{1,2}")
  local real_interface="eth90"

  for interface in "${interface_prefix[@]}"; do
      echo $(ip link show) | grep -oE ${interface} | head -1
      if [[ $? -eq 0 ]]; then
          real_interface=$(echo $(ip link show) | grep -oE ${interface} | head -1 | cut -d" " -f2)
          echo "当前主机的真实内网网卡为 => [$real_interface]"
          break
    fi
  done

  # 提取IPv4地址（CIDR格式）
  local ipv4_regex="inet[[:space:]](25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/[0-9]{1,2}"

  # 提取IPv6地址（CIDR格式）
  local ipv6_regex="inet6[[:space:]]([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){1,7})\/[0-9]{1,3}"

  # 查找IPv4地址
  local inner_ipv4=$(echo $(ip addr show $real_interface) | grep -oE $ipv4_regex | cut -d" " -f2)
  InnerIPv4CIDR=$inner_ipv4
  echo "Interface: $real_interface, IPv4 Address: $inner_ipv4"

   # 查找IPv6地址
  local inner_ipv6=$(echo $(ip addr show $real_interface) | grep -oE $ipv6_regex | cut -d" " -f2)
  InnerIPv6CIDR=$inner_ipv6
  echo "Interface: $real_interface, IPv4 Address: $inner_ipv6"

}

#######################################
# description
# Globals:
#   InnerIPv4
#   InnerIPv4CIDR
#   InnerIPv6
#   InnerIPv6CIDR
# Arguments:
#  None
#######################################
get_Internal_IP() {

  get_Internal_IP_CIDR

  InnerIPv4=$(echo $InnerIPv4CIDR | cut -d "/" -f1)
  InnerIPv6=$(echo $InnerIPv6CIDR | cut -d "/" -f1)

  echo "服务器的内网IPv4地址为 $InnerIPv4"
  echo "服务器的内网IPv6地址为 $InnerIPv6"

}

#######################################
# description
# Globals:
#   _m_opts
# Arguments:
#  None
#######################################
_math() {
  _m_opts="$@"
  printf "%s" "$((_m_opts))"
}

# 前文内容

while [[ $# -gt 0 ]]; do
    case "$1" in
        -fu | --fileupload)
        NeedUploadFolder=1
        ImageListName=${2}
        shift # past argument
        ;;
        -fd | --filedownload)
        NeedUploadFolder=2
        ImageListName=${2}
        shift # past argument
        ;;
        -u | --upload)
        IsUpload=1
        UploadImageName="${2}"
        shift # past argument
        ;;
        -d | --download)
        IsUpload=2
        ImageTarName="${2}"
        shift # past argument
        ;;
        -h | --harbor)
        Custom_Client_Harbor="${2}"
        shift # past argument
        ;;
        *)
                # unknown option
        ;;
  esac
    shift # past argument or value
done

#######################################
# description
# Globals:
#   ImageTarName
#   UploadImageName
# Arguments:
#  None
#######################################
Download_Compress_OSS_Image() {

  # upload
    echo "【上传】 - 需要处理的镜像名称为 => $UploadImageName"
    echo ""
    echo "【上传】 - 开始下载镜像！"
    echo ""
    if docker pull "${UploadImageName}" >/dev/null 2>&1; then
        echo "下载-镜像下载成功！ => $UploadImageName"
    else
        if docker inspect "${UploadImageName}" >/dev/null 2>&1; then
          echo "下载-镜像已经存在 => $UploadImageName"
    else
          echo ""
          echo "下载-镜像下载 失败！ => $UploadImageName"
          echo "下载-镜像下载 失败！ => $UploadImageName"
          echo ""
          return 233
    fi
    fi
    echo ""

    get_image_tar_name
    echo "【上传】 - 将要把镜像压缩为 => $ImageTarName"
    docker save $UploadImageName | gzip --stdout >${ImageTarName}
    echo "【上传】 - 压缩成功 ！ $(ls | grep ${ImageTarName})"
    echo""

    echo "【上传】 - 开始上传至OSS中!"
    mc cp ./${ImageTarName} demo/cmlc-installation/tmp/
    echo "【上传】 - 上传OSS成功 => $(mc ls demo/cmlc-installation/tmp/ | grep ${ImageTarName})"
    echo""

}
#######################################
# description
# Globals:
#   CmiiImagePrefix
#   ImageTarName
#   InnerIPv4
#   OSS_URL
#   UploadImageName
#   custom_prefix
# Arguments:
#  None
# Returns:
#   <unknown> ...
#######################################
UnCompress_Upload_Harbor() {

  echo "【下载】 - 需要处理的压缩包名称为 => $ImageTarName"

    echo "【下载】 - 开始下载 ↓"
    wget "$OSS_URL$ImageTarName"
    if [[ $? -ne 0 ]]; then
        echo "【下载】- 下载镜像失败！退出！"
        return
  fi
    echo ""

    echo "【下载】 - 开始解压压缩包 "
    docker load <"${ImageTarName}"
    echo ""

    get_Internal_IP
    echo "【下载】 - 开始上传镜像到本地的镜像仓库中 => $InnerIPv4:8033 "
    custom_prefix="$InnerIPv4:8033/cmii/"
    echo ""

    get_image_name_from_tar
    echo "【下载】 - 解析得到的 私有镜像地址为 => $custom_prefix$UploadImageName"
    docker tag "$CmiiImagePrefix${UploadImageName}" "$custom_prefix$UploadImageName"

    echo "【下载】 - 开始上传镜像到私有仓库 ↓"
    docker login -u admin -p V2ryStr@ngPss "$InnerIPv4:8033"
    echo ""
    echo "【下载】 - 开始推送到私有仓库！ "
    docker push "$custom_prefix$UploadImageName"
    echo ""
    echo ""

}

if [[ $NeedUploadFolder == 1 ]]; then
    # 上传的是一个目录
    echo "【上传】 - 上传的文件目录为 => ${ImageListName}"

    currentDate=$(date +'%Y-%m-%d')
    random_number=$((RANDOM % 900 + 100))
    tar_image_txt_file="$currentDate-$random_number.txt"
    echo "【上传】 - 复制文件目录为临时文件 =>  $tar_image_txt_file"
    touch $tar_image_txt_file
    echo ""
    echo ""

    while IFS= read -r i; do
      [ -z "${i}" ] && continue

      UploadImageName=${i}
      Download_Compress_OSS_Image

      # 写入压缩文件名称
      echo "$ImageTarName" >>"$tar_image_txt_file"

      echo "-------------------------------------------------"
  done   <"${ImageListName}"
    echo ""

    echo "【上传】 - 上传压缩文件名称列表至OSS中 ！"
    mc cp ./"$tar_image_txt_file" demo/cmlc-installation/tmp/
    echo "【上传】 - 上传OSS成功 => $(mc ls demo/cmlc-installation/tmp/ | grep "${tar_image_txt_file}")"
    echo ""

    echo "【上传】 - 请在目标Master主机执行如下命令 ↓↓↓↓↓↓"
    echo ""
    echo ""
    echo "source <(curl -sL https://b2.107421.xyz/image-sync.sh) -fd "$OSS_URL$tar_image_txt_file""
    echo ""
    echo ""

elif [[ $NeedUploadFolder == 2 ]]; then
    echo "【下载】- 需要下载的压缩包文件为 => $ImageListName"
    wget ${ImageListName}

    tar_image_txt_file=$(echo ${ImageListName} | cut -d"/" -f6)
    image_count=0

    while IFS= read -r i; do
      [ -z "${i}" ] && continue

      ImageTarName=${i}
      UnCompress_Upload_Harbor
      image_count="$(_math "$image_count" + 1)"

      echo "-------------------------------------------------"
    done   <"${tar_image_txt_file}"
    echo ""

     echo "【下载】 - 请等待流程结束 ↓"
    _sleep_c="500"
    for (( i=1; i<$image_count; i++ )) ; do
        _sleep_c="$(_math "$_sleep_c" + 500)"
    done
    echo "【下载】 - 共有 【 $image_count 】个镜像， 等待上传结束 => $_sleep_c 秒"
    while [ "$_sleep_c" -ge "0" ]; do
      printf "\r      \r"
       printf -- "%b" "$_sleep_c"
      _sleep_c="$(_math "$_sleep_c" - 1)"
      sleep 1
    done
    printf "\r"

fi

if [[ $IsUpload == 1 ]]; then
    Download_Compress_OSS_Image
    echo "【上传】 - 请在目标Master主机执行如下命令 ↓↓↓↓↓↓"
    echo ""
    echo ""
    echo "source <(curl -sL https://b2.107421.xyz/image-sync.sh) -d $ImageTarName"
    echo ""
    echo ""
    Client_Harbor_Address="$Custom_Client_Harbor/cmii/$ImageName:$ImageTag"
    echo "【上传】 - 手动命令执行如下， 目标镜像全程地址为 => $Client_Harbor_Address"
    echo ""
    echo "wget $OSS_URL$ImageTarName && docker load < $ImageTarName && docker tag ${UploadImageName} $Client_Harbor_Address && docker push $Client_Harbor_Address"
    echo ""
    echo ""
elif [[ $IsUpload == 2 ]];then
    # download
    UnCompress_Upload_Harbor

    echo "【下载】 - 请等待流程结束 ↓"
    _sleep_c="500"
    while [ "$_sleep_c" -ge "0" ]; do
      printf "\r      \r"
      printf -- "%b" "$_sleep_c"
      _sleep_c="$(_math "$_sleep_c" - 1)"
      sleep 1
    done
    printf "\r"
fi
