#!/bin/bash

all_image_list_txt="all-cmii-image-list.txt"  # 需要修改版本
gzip_image_list_txt="all-gzip-image-list.txt" # 一般不需要修改
oss_prefix_url="https://oss.demo.uavcmlc.com/cmlc-installation"
local_gzip_path="/root/octopus-image"

DockerRegisterDomain="172.22.106.77:8033" # 需要根据实际修改
HarborAdminPass=V2ryStr@ngPss              # 需要跟第一脚本中的密码保持一致

print_green() {
  echo -e "\033[32m${1}\033[0m"
  echo ""
}

print_red() {
  echo -e "\033[31m${1}\033[0m"
  echo ""
}

Download_Load_Tag_Upload() {
  print_green "[DLTU] - start !"
  while [[ $# -gt 0 ]]; do
    case "$1" in
    rke)
      #          print_green "download rke "
      local_gzip_path="$local_gzip_path/rke13014"
      mkdir -p ${local_gzip_path}
      oss_prefix_url="$oss_prefix_url/rke13014/"
      dltu
      shift # past argument
      ;;
    middle)
      local_gzip_path="$local_gzip_path/middle"
      mkdir -p $local_gzip_path
      oss_prefix_url="$oss_prefix_url/middle/"
      dltu
      shift # past argument
      ;;
    cmii)
      local_gzip_path="$local_gzip_path/xauas22"
      mkdir -p $local_gzip_path
      oss_prefix_url="$oss_prefix_url/xauas22/"
      dltu
      shift # past argument
      ;;
    *)
      # unknown option
      print_red "bad arguments"
      ;;
    esac
  done

}

dltu() {
  print_green "download all image name list and gzip file list!"
  cd $local_gzip_path || exit

  rm $all_image_list_txt
  rm $gzip_image_list_txt

  wget "$oss_prefix_url$all_image_list_txt"
  wget "$oss_prefix_url$gzip_image_list_txt"

  docker login -u admin -p ${HarborAdminPass} ${DockerRegisterDomain}
  echo ""
  while IFS= read -r i; do
    [ -z "${i}" ] && continue
    echo "download gzip file =>: $oss_prefix_url${i}"
    if wget "$oss_prefix_url${i}" >/dev/null 2>&1; then
      echo "Gzip file download success : ${i}"
      image_full_name=$(docker load -i ${i} | head -n1 |awk -F': ' '{print $2}')

      app_name=$(echo "$image_full_name" | sed 's|.*/||g')
      echo "extract short name is $app_name"

      if echo $image_full_name | grep -q "rancher"
      then
        print_green "tag image to => $DockerRegisterDomain/rancher/$app_name"
        docker tag ${image_full_name} $DockerRegisterDomain/rancher/$app_name
        docker push $DockerRegisterDomain/rancher/$app_name
      else
        print_green "tag image to => $DockerRegisterDomain/cmii/$app_name"
        docker tag ${image_full_name} $DockerRegisterDomain/cmii/$app_name
        docker push $DockerRegisterDomain/cmii/$app_name
      fi

    else
      print_red "Gzip file download FAILED : ${i}"
    fi
    echo "-------------------------------------------------"
  done <"${gzip_image_list_txt}"
  shift

}

Load_Tag_Upload(){
  print_green "[LTU] - start to load image from offline !"

  while [[ $# -gt 0 ]]; do
    case "$1" in
    rke)
      #          print_green "download rke "
      local_gzip_path="$local_gzip_path/rke13014"
      mkdir -p ${local_gzip_path}
      oss_prefix_url="$oss_prefix_url/rke13014/"
      ltu
      shift # past argument
      ;;
    middle)
      local_gzip_path="$local_gzip_path/middle"
      mkdir -p $local_gzip_path
      oss_prefix_url="$oss_prefix_url/middle/"
      ltu
      shift # past argument
      ;;
    cmii)
      local_gzip_path="$local_gzip_path/cmii"
      mkdir -p $local_gzip_path
      oss_prefix_url="$oss_prefix_url/cmii/"
      ltu
      shift # past argument
      ;;
    *)
      # unknown option
      print_red "bad arguments"
      ;;
    esac
  done

}

ltu(){
 all_file_list=$(find $local_gzip_path -type f -name "*.tar.gz")

  for file in $all_file_list; do
    echo "offline gzip file is => : $file"
    image_full_name=$(docker load -i ${file} | head -n1 |awk -F': ' '{print $2}')

    docker login -u admin -p ${HarborAdminPass} ${DockerRegisterDomain}

    app_name=$(echo "$image_full_name" | sed 's|.*/||g')
    echo "extract short name is $app_name"

    if echo $image_full_name | grep -q "rancher"
    then
      print_green "tag image to => $DockerRegisterDomain/rancher/$app_name"
      docker tag ${image_full_name} $DockerRegisterDomain/rancher/$app_name
      docker push $DockerRegisterDomain/rancher/$app_name
    else
      print_green "tag image to => $DockerRegisterDomain/cmii/$app_name"
      docker tag ${image_full_name} $DockerRegisterDomain/cmii/$app_name
      docker push $DockerRegisterDomain/cmii/$app_name
    fi
  done
}


test(){
 app_name=$(echo "nginx:latest" | sed 's|.*/||g')
 echo "extract short name is $app_name"
}

# test
Download_Load_Tag_Upload "rke"

# Load_Tag_Upload "cmii"