#!/bin/bash


function print_usage() {
    echo
    cat <<EOF
Usage:
    bash updateimagetag.sh --help
    bash updateimagetag.sh -e inbiz -v v6.5.0.0 -m cluster
   
注：此脚本只适用于全量升级，批量替换应用服务tag，只替换tag，不提供导入镜像功能。

Options:
    -h, --help                   show usage g
    -e                   升级产品，ecm|inbiz|edrms|km|udc，必须参数
    -v                 升级产品版本， 必须选项
    -m                 升级产品为单机部署或者集群部署，standalone|cluster ,必须参数
EOF
}


function updatetag() {

oldtag=$(cat /opt/${MODE}/app/*.yaml /opt/${MODE}/edrms/*.yaml /opt/${MODE}/inbiz/*.yaml |grep "image:"|awk -F '/' '{print $3}')


for tag in ${oldtag};do
  newtag=$(echo $tag|awk -F ':' '{print $1}'):$PROJECT-$VERSION
  if [[ ${newtag} == weboffice* ]];then
     continue
  fi
     sed -i "s#${tag}#${newtag}#g"  /opt/$MODE/**/*.yaml
  if [ ${MODE} == "standalone" ];then
     sed -i "s#${tag}#${newtag}#g"  /opt/$MODE/app/nas/*.yaml
     sed -i "s#${tag}#${newtag}#g"  /opt/$MODE/app/s3/*.yaml
  fi
done

}

if [ "$1"x == x ]; then
    echo "没有获取参数"
    print_usage
    exit
fi

while true; do
    case "$1" in
    -h | --help)
        case "$2" in
        "" | -*)
            print_usage
            exit
            ;;
        esac
        ;;
    -e | --env)
        case "$2" in
        "" | -*)
            print_usage
            exit
            ;;
        esac
        PROJECT=$2
        shift 2
        ;;
    -v)
        case "$2" in
        "" | -*)
            print_usage
            exit
            ;;
        esac
        VERSION=$2
        shift 2
        ;;
    -m)
        case "$2" in
        "" | -*)
            print_usage
            exit
            ;;
        esac
        MODE=$2
        shift 2
        ;;
    *)
        break
        ;;
    esac
done
updatetag
