#!/bin/sh
# use for download docker images from abroad
# learn from: https://github.com/DaoCloud/public-image-mirror?tab=readme-ov-file

####   docker accelerator :  ####
# $ cat /etc/docker/daemon.json 
# {
#   "registry-mirrors": [
#     "https://docker.m.daocloud.io"
#   ]
# }


proxy_site="docker.m.daocloud.io/docker.io/"

Usage() {
  echo "Usage: docker-images-proxy-download.sh  images:tag "
}

if [ $# -eq 0 ];then
   Usage
   exit
else
   args=$1
fi

which docker 
if [ $? -ne 0 ];then
   echo "Please install docker first! exit! "
   exit
fi

docker pull ${proxy_site}$args 
if [ $? -eq 0 ];then
   echo "docker images pull success!" 
   docker tag ${proxy_site}$args $args 
   if [ $(echo ${args} | grep ':'| wc -l) -eq 1 ];then
      args_site=$(echo ${args} | awk -F ':' '{print $1}')
      args_tag=$(echo ${args} | awk -F ':' '{print $2}')
   else
      args_site=${args}
      args_tag="latest"
   fi
   docker images | grep "^${args_site}" | grep "\ ${args_tag}\ "
else
   echo "docker imgaes pull failed!"
fi
