# use git bash
# prereq: id_rsa, authorizes_key in epm/.ssh,
# authorizes_key:sudo chown -R epm /home/epm  sudo chmod -R 700 /home/epm

# hanxun
# bash uploadBuildGenaral_idrsa.sh -LOCAL_BUILD_PATH '.' -UPLOAD_PATH_I '/home/EPM2/UPLOAD' -IP_LIST '10.100.1.55 10.100.1.57' -REL 'master' -NODE_NEW 'false' -PRIVATE_KEY 'true' -SLEEP_INTERVAL 1800

#sunwoda
# bash uploadBuildGenaral_idrsa.sh -LOCAL_BUILD_PATH '.' -UPLOAD_PATH_I '/data_disk/EPM2/UPLOAD' -IP_LIST '172.26.200.90' -REL 'master sunwoda-pg' -NODE_NEW 'true' -PRIVATE_KEY 'true' -SLEEP_INTERVAL 1800 -MDX_IP '172.30.189.29' -MDX_IMAGE 'abad4e9'


# zeiss test
# bash xxx.sh -LOCAL_BUILD_PATH '/data_disk/UPLOAD/sys' -UPLOAD_PATH_I '/opt/app/EPM2/PRD/UPLOAD' -IP_LIST '192.168.124.63:3022' -REL 'master zeiss'
# zeiss prod
# bash xxx.sh -LOCAL_BUILD_PATH '/data_disk/UPLOAD/sys' -UPLOAD_PATH_I '/opt/app/EPM2/UPLOAD' -IP_LIST '192.168.124.63:4022' -REL 'master zeiss'

#oppein
# bash xxx.sh -LOCAL_BUILD_PATH '/data/EPM2/UPLOAD/sys' -UPLOAD_PATH_I '/data/EPM2/UPLOAD' -IP_LIST '10.10.238.133 10.10.238.134 10.10.238.135' -REL 'master'

# qbj test
# bash xxx.sh -LOCAL_BUILD_PATH '/data_disk/UPLOAD/sys' -UPLOAD_PATH_I '/EPM2/UPLOAD' -IP_LIST '192.168.124.147:3025' -REL 'master' -PRIVATE_KEY '/home/epm/.ssh/id_rsa_pri' -REMOTE_USER 'lhwu'
 
set -x
dateStr=`date`
echo "start ${dateStr}"
curDir=`pwd`
echo $curDir
REMOTE_USER=epm
while [[ $# -gt 0 ]]; do
  COMMAND=$1
  case $COMMAND in
    -MDX_IP)
      MDX_IP=$2 # update mdx
      shift 2
      ;;
    -NODE_NEW) # for node20/16 NODE_NEW true/false
      NODE_NEW=$2
      shift 2
      ;;
    -PRIVATE_KEY)
      PRIVATE_KEY=$2
      shift 2
      ;;
    -REMOTE_USER)
      REMOTE_USER=$2
      shift 2
      ;;
    -UPLOAD_PATH_I)
      UPLOAD_PATH_I=$2
      shift 2
      ;;
    -IP_LIST)
      IP_LIST=$2
      shift 2
      ;;
    -LOCAL_BUILD_PATH)
      LOCAL_BUILD_PATH=$2
      shift 2
      ;;
    -REL) #master;master zeiss
      REL=$2
      shift 2
      ;;
    -SLEEP_INTERVAL)
      SLEEP_INTERVAL=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done
echo "$UPLOAD_PATH_I $IP_LIST $LOCAL_BUILD_PATH $REL"

function uploadBuild() {
    dateStr=`date`
    echo "uploadBuild ${dateStr}"
    if [[ $MDX_IP ]];then #sunwoda
       echo 'upload mdx'
       if [[ $MDX_IMAGE ]];then
          appTarfile=server_$MDX_IMAGE.tar
       else
          imageFile='mdx_build_image'
          rm -f $imageFile
          curl --user epm:Wang_2023 https://build.epm2demo.cn/cicd_cache/update/$imageFile --output $imageFile
          appTarfile=server_`cat $imageFile`.tar
       fi
       echo "appTarfile $appTarfile"
       
       if [[ -f ${appTarfile} && -f ${appTarfile}.info ]];then
          echo 'skip download'
        else
          echo 'begin download'
          curl --user epm:Wang_2023 https://build.epm2demo.cn/cicd_cache/mdx/server/$appTarfile --output $appTarfile
          curl --user epm:Wang_2023 https://build.epm2demo.cn/cicd_cache/mdx/server/$appTarfile.info --output $appTarfile.info
        fi
     
          echo "uploading ip: $MDX_IP file:${UPLOAD_PATH_I}/${appTarfile}"
          remoteCmd="test -f ${UPLOAD_PATH_I}/${appTarfile}"
          if [[ $PRIVATE_KEY = 'true' ]];then
            ssh -o StrictHostKeyChecking=no -i $curDir/id_rsa epm@${MDX_IP} $remoteCmd
          else
            sshpass -p Wang%2023 ssh epm@${MDX_IP} $remoteCmd
          fi            
          cmdResult=$?
          echo "cmdResult $cmdResult"
          if [[ $cmdResult = 0 ]];then
            echo 'exist, skip upload'
          else
            if [[ $PRIVATE_KEY = 'true' ]];then
              scp -o StrictHostKeyChecking=no -i $curDir/id_rsa -o StrictHostKeyChecking=no $appTarfile $appTarfile.info epm@${MDX_IP}:${UPLOAD_PATH_I}
            else
              sshpass -p Wang%2023 scp -o StrictHostKeyChecking=no $appTarfile $appTarfile.info epm@${MDX_IP}:${UPLOAD_PATH_I}
            fi              
            echo "uploaded tar $appTarfile(info) to ${MDX_IP},$UPLOAD_PATH_I"          
          fi
    fi
    for brch in $REL;
    do
        echo "download $brch build"
        if [[ $brch == 'master' ]];then
          bp1=$MASTER_BUILD_URL
          image1='cons_build_image'
          UPLOAD_PATH=${UPLOAD_PATH_I}/dev
          if [[ $REL == 'master' ]];then
            UPLOAD_PATH=${UPLOAD_PATH_I}/sys #only upload master
          fi
        else
          bp1=$REL_BUILD_URL
          image1="cons_${brch}_build_image"
          UPLOAD_PATH=${UPLOAD_PATH_I}/sys
        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`-${buildPostFix}.tar
        echo "appTarfile $appTarfile"
        if [[ -f ${appTarfile} && -f ${appTarfile}.info ]];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.info --output $appTarfile.info
        fi

        for ip in $IP_LIST;
        do        
            if [[ $ip =~ ':' ]];then
              ipi=`echo $ip | awk -F ':' '{print $1}'`
              porti=`echo $ip | awk -F ':' '{print $2}'`
            else
              ipi=$ip
              porti=22
            fi
            echo "build info: branch $brch ip: $ipi  port: $porti "
            echo "uploading ip: $ip file:${UPLOAD_PATH}/${appTarfile}"
            remoteCmd="test -f ${UPLOAD_PATH}/${appTarfile}"
            if [[ $PRIVATE_KEY ]];then
              ssh -o StrictHostKeyChecking=no -p $porti -i $PRIVATE_KEY $REMOTE_USER@${ipi} $remoteCmd
            else
              sshpass -p Wang%2023 ssh -p $porti epm@${ipi} $remoteCmd
            fi            
            cmdResult=$?
            echo "cmdResult $cmdResult"
            if [[ $cmdResult = 0 ]];then
              echo 'exist, skip upload'
            else
              if [[ $PRIVATE_KEY ]];then
                #ssh -p $porti -i $curDir/id_rsa epm@${ipi} $remoteCmd
                scp -o StrictHostKeyChecking=no -P $porti -i $PRIVATE_KEY -o StrictHostKeyChecking=no $appTarfile $appTarfile.info $REMOTE_USER@${ipi}:${UPLOAD_PATH}
              else
                sshpass -p Wang%2023 scp  -P $porti -o StrictHostKeyChecking=no $appTarfile $appTarfile.info epm@${ipi}:${UPLOAD_PATH}
              fi              
              echo "uploaded tar $appTarfile(info) to ${ipi},$UPLOAD_PATH"          
            fi        
        done        
    done
}

MASTER_BUILD_URL='cicd_cache_v20' #default
REL_BUILD_URL='cicd_release_v20' #default
buildPostFix='node20'
if [[ $NODE_NEW == 'false' ]];then
    echo 'use node16'
    MASTER_BUILD_URL='cicd_cache'
    REL_BUILD_URL='cicd_release'
    buildPostFix='node16'
else
    echo 'use node20'
fi

cd $LOCAL_BUILD_PATH

while(( 1 ))
do
   uploadBuild
   if [[ $SLEEP_INTERVAL ]];then      
      sleep $SLEEP_INTERVAL  
   else
      echo 'end'
      break
   fi   
done