#!/bin/bash

build_profile=b2b12

iap_username=iap
iap_password=IAPAb123456!
iap_url=$iap_username@132.122.1.162

ytp_username=yyy
ytp_password='QeY\!68\)4nH1'
ytp_url=ftp://$ytp_username:$ytp_password@132.121.122.15:2222

upload_username=AxhEbscwsJDbYMH2
upload_password=cYxg3b4PtWoVD5SjFayWxtnSVsjzRsg4
upload_url=http://$upload_username:$upload_password@132.126.207.124:36800/file/upload

root_path=$(dirname $(cd $(dirname $0);pwd))

function upload() {
  source_file_path=$(realpath $1)
  file_name=$(basename $source_file_path)
  echo "↪ Source md5: $(md5sum $source_file_path | awk '{print $1}')"
  echo "↪ Uploading $source_file_path"
  curl $upload_url/$file_name -T $source_file_path
  echo "↪ Upload ytp success"
  echo "↪ Download: curl $upload_url/file/download/$file_name -o $file_name"
  echo "↪ Delete source"
  rm $source_file_path
}

function upload_ytp() {
  source_file_path=$(realpath $1)
  file_name=$(basename $source_file_path)
  echo "↪ Source md5: $(md5sum $source_file_path | awk '{print $1}')"
  echo "↪ Uploading $source_file_path ↪ /tmp/$file_name"
  sshpass -p $iap_password scp $source_file_path $iap_url:/tmp
  echo "↪ Upload 162 success"
  target_md5=$(sshpass -p $iap_password ssh -o 'StrictHostKeyChecking no' $iap_url "md5sum /tmp/$file_name | awk '{print \$1}'")
  echo "↪ Target md5: $target_md5"
  echo "↪ Command: sshpass -p $iap_password ssh -o 'StrictHostKeyChecking no' $iap_url \"curl --retry 5 $ytp_url -T /tmp/$file_name\""
  sshpass -p $iap_password ssh -o 'StrictHostKeyChecking no' $iap_url "curl --retry 5 $ytp_url -T /tmp/$file_name"
  echo "↪ Upload ytp success"
  echo "↪ Download: curl $ytp_url/$file_name -o $file_name"
}

function joining {
  local d=${1-} f=${2-}
  if shift 2; then
    printf %s "$f" "${@/#/$d}"
  fi
}

function deploy() {
  mvn -pl $(joining , $@) clean deploy -D skipTests -P local -s ~/.m2/settings-development.xml
}

function package() {
  mvn -pl $(joining , $@) clean package -D skipTests -P local -s ~/.m2/settings-development.xml -P $build_profile
}
