#!/bin/bash

export base=$HOME/build/git/MTP20_IF

export GOROOT=/home/pub/pathhome/go1.14.4.linux-amd64
export GOPATH=/home/pub/go:/home/pub/build/git/websocket2tcp:/home/pub/build/git/OpenAccount_WebAPI/WebAPI
export GOBIN=$GOPATH/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
export PATH=$PATH:$GOROOT/bin:$GOBIN

export PKG_CONFIG_PATH=/usr/lib64/pkgconfig
export LD_LIBRARY_PATH=/opt/instantclient_11_2/

function build()
{
  local src=$1
  local oldpwd=`pwd`
  cd $src

  echo "Before switching: "
  git branch
  git reset --hard
  git clean -fdx
  git pull --rebase
  git checkout $branch_name
  echo "After switching: "
  git branch 

  if [ $? -ne 0  ]; then
    echo "git update err:1"
    exit 1
  fi
  
  cd $src
  #sh $HOME/build/scripts/xml_to_j2.sh openaccount_api $src/src/OpenAccountApi/
  echo "path:"$src
  go build -o bin/QueryService
  if [ $? -ne 0  ]; then
    echo "go build err: 2"
    exit 2
  fi
  #cp -r config bin/
  mkdir -p bin/config
  cp config/config.xml bin/config

  local datestr=`date +%Y%m%d%H%M%S`
  
  cd $src/bin/

  local zipfile=mtp2_queryservice_r20_${datestr}.zip
  local rsync_ip=192.168.30.153
  local rsync_project=mtp2.0_release
  
  zip -r $zipfile ./
    
  rsync -avh $src/bin/${zipfile} $rsync_ip::build/$rsync_project/${zipfile}
  echo "Storage path: \\\\$rsync_ip\\share\\build\\$rsync_project\\$zipfile"
  
  rm -rf $src/bin
}

#######################main function#########################
build $base
if [ $? -ne 0  ]; then
    exit -1
fi


