#!/bin/bash

export base=$HOME/build/git/MTP20_ASSISTED

export GOROOT=/home/pub/go
export GOPATH=/home/pub/gowebapi
export GOBIN=$GOPATH/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
export PATH=$PATH:$GOROOT/bin:$GOBIN

export PKG_CONFIG_PATH=/home/pub/pkconfig
export LD_LIBRARY_PATH=/home/pub/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
  
  githash=`git log -n 1 --format=format:"%H"` 
 
  cd $src
  echo `go version`
  echo "go build begin:"`date "+%Y-%m-%d %H:%M:%S"`
  
  echo "path:"$src
  local datestr=`date +%Y%m%d%H%M%S`
  local zipfile=mtp20_assisted_${datestr}.zip

  go build -ldflags "-X 'main._VERSION_=$zipfile' -X 'main._GITHASH_=$githash'" -a -o bin/mtp20_assisted
  if [ $? -ne 0  ]; then
    echo "go build err: 2"
    exit 2
  fi
  echo "go build finish:"`date "+%Y-%m-%d %H:%M:%S"`
  cp -r $src/config.yaml bin/
  cp -r $src/static bin/static
  
  cd $src/bin/

  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"
  echo "quick download: wget -c http://$rsync_ip/share/build/$rsync_project/$zipfile"
  rm -rf $src/bin
}

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


