#for zeiss, 
#master
IP='192.168.124.63'
TEST_DIR='/opt/app/EPM2/PRD/UPLOAD'
PROD_DIR='/opt/app/EPM2/UPLOAD'
LOCAL_BUILD_PATH=$1 #/data_disk/UPLOAD/sys

cd $LOCAL_BUILD_PATH
for brch in master release;
  do
    echo "download $brch build"
    if [[ $brch == 'master' ]];then
      bp1='cicd_cache_v20'
      image1='cons_build_image'
    else
      bp1='cicd_release_v20'
      image1='cons_zeiss_build_image'
    fi
    rm -f cons_build_image
    curl --user epm:Wang_2023 https://build.epm2demo.cn/$bp1/update/$image1 --output cons_build_image
    appTarfile=`cat cons_build_image`-node20.tar
    echo "appTarfile $appTarfile"
    if [[ -f ${appTarfile} && -f ${appTarfile}.md5sum ]];then
      echo 'skip download'
    else
       echo 'begin download'
      curl --user epm:Wang_2023 https://build.epm2demo.cn/$bp1/$appTarfile --output $appTarfile
      curl --user epm:Wang_2023 https://build.epm2demo.cn/$bp1/$appTarfile.md5sum --output $appTarfile.md5sum
    fi

    for env1 in 'test' 'prod';
    do
      if [[ $env1 == 'test' ]];then
        buildPath=$TEST_DIR
        porti=3022
      else
        buildPath=$PROD_DIR
        porti=4022
      fi
      for sysi in 'dev' 'sys';
      do
        echo "build info: branch $brch sys: $sysi env: $env1 "
        if [[ ($sysi == 'sys' && $env1 == 'prod' && $brch == 'master' ) ||  ($sysi == 'dev' && $brch == 'release') ]];then
          echo 'skip upload'
          continue
        fi
        echo "uploading env:$env1 file:${buildPath}/${sysi}/${appTarfile}"
        remoteCmd="test -f ${buildPath}/${sysi}/${appTarfile}"
        sshpass -p Wang%2023 ssh -p $porti epm@${IP} $remoteCmd
        cmdResult=$?
        echo "cmdResult $cmdResult"
        if [[ $cmdResult == 0 ]];then
          echo 'exist, skip upload'
        else
          sshpass -p Wang%2023 scp -o StrictHostKeyChecking=no -P $porti $appTarfile $appTarfile.md5sum epm@$IP:$buildPath/$sysi
          echo "uploaded tar $appTarfile(md5sum) to $env1,$buildPath/${sysi}"          
        fi
      done
    done
    rm -f ${appTarfile} $appTarfile.md5sum
done