# Maintainer: Martell Malone < martell malone at g mail dot com >
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=go
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.16
pkgrel=1
pkgdesc="Go Lang (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
url="https://www.golang.org/"
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip')
source=("https://storage.googleapis.com/golang/go${pkgver}.src.tar.gz"
        0010-go1.7-proper-go-binary-path.patch
        0011-add-GO_BUILD_VERBOSE.patch)
sha256sums=('7688063d55656105898f323d90a79a39c378d86fe89ae192eb3b7fc46347c95a'
            'af2c10300635ac84e6d870cb40a6481ccb57487fb7a6efad781f20088bec4de8'
            'c7f9a39a4b29668276d5836d2c1e1d5c8017c6dd66b976f519e9f5e85383ab85')
noextract=(go${pkgver}.src.tar.gz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  tar -xzf ${srcdir}/go${pkgver}.src.tar.gz -C ${srcdir} || true
  mv "${srcdir}"/${_realname} "${srcdir}"/${_realname}-${pkgver}

  cd "${srcdir}/${_realname}-${pkgver}"
#  apply_patch_with_msg \
#    0010-go1.7-proper-go-binary-path.patch
#    0011-add-GO_BUILD_VERBOSE.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}/src
  #export GOROOT_BOOTSTRAP=/`echo ${MSYSTEM} | tr '[:upper:]' '[:lower:]'`/lib/go
  #export GOROOT="${srcdir}/${_realname}-${pkgver}"
  #export GOBIN="${GOROOT}/bin"
  #export GOPATH="${srcdir}/"
  export GOROOT_FINAL=${MINGW_PREFIX}/lib/go
  export GO_CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  export CFLAGS="-D__USE_MINGW_ANSI_STDIO=1"
  export GO_BUILD_VERBOSE=1

  # I have no idea why we need this to be run from a completely seperate
  # MSDOS shell, but it does. Without this, the build exits with no info
  # about what went wrong.

  rm -f /tmp/bootstrap_go.bat
  echo "SET GOROOT_BOOTSTRAP=$(cygpath -w $MINGW_PREFIX/lib/go)"     > /tmp/bootstrap_go.bat
  echo "cd $(cygpath -w $PWD)"                                      >> /tmp/bootstrap_go.bat
  echo "start /wait \"\" cmd.exe /c .\\make.bat"                    >> /tmp/bootstrap_go.bat
  /tmp/bootstrap_go.bat

  # ./make.bat

}

check() {
  cd "${_realname}-${pkgver}"

  test_text="Hello MSYS2!"

  rm -f /tmp/test_main.go
  echo "package main"                    > /tmp/test_main.go
  echo "import \"fmt\""                 >> /tmp/test_main.go
  echo "func main() {"                  >> /tmp/test_main.go
  echo "fmt.Println(\"${test_text}\")"  >> /tmp/test_main.go
  echo "}"                              >> /tmp/test_main.go

  ./bin/gofmt.exe -w /tmp/test_main.go
  ./bin/go.exe build -o /tmp/test_result.exe /tmp/test_main.go
  output=$(/tmp/test_result.exe)

  if [[ "$output" != "$test_text" ]]; then
    echo "Output \"${output}\" does not match test-text \"${test_text}\""
    exit 1
  fi

  rm -f /tmp/test_main.go /tmp/test_result.exe
}

package() {
  cd "${_realname}-${pkgver}"

  export GOROOT="${srcdir}/${_realname}-${pkgver}"
  export GOBIN="${GOROOT}/bin"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/"{bin,lib/go,lib/go/doc,lib/go/src,lib/go/site/src,share/licenses/go,share/go}

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/go"
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/go/LICENSE"

  cp -rf bin "${pkgdir}${MINGW_PREFIX}"
  cp -rf bin pkg src lib misc api test "${pkgdir}${MINGW_PREFIX}/lib/go"
  cp -r doc/* "${pkgdir}${MINGW_PREFIX}/lib/go/doc/"

  install -Dm644 VERSION "${pkgdir}${MINGW_PREFIX}/lib/go/VERSION"

  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/go/pkg/bootstrap" "${pkgdir}${MINGW_PREFIX}/lib/go/pkg/tool/*/api"

  # TODO: Figure out if really needed
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/go/pkg/obj/go-build/*

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/go/LICENSE"
 
  # install profile script 
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/etc/profile.d
  echo "export GOROOT=${MINGW_PREFIX}/lib/go" > "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.sh
  cp "${pkgdir}${MINGW_PREFIX}"/etc/profile.d/go.{sh,zsh}
}

# Please leave this debugging stuff here until someone gets to the bottom of the problem why ./make.bat fails.

# pushd ~/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/src
# (export GOROOT_BOOTSTRAP=C:/msys64/mingw64/lib/go export CC_FOR_TARGET=gcc ; export GOROOT=/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4 ; export GOBIN=C:/msys64/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/bin ; export GOARCH=amd64 ; export GOOS=windows ; export GOHOSTARCH=amd64 ; export GOHOSTOS=windows ; export GOTOOLDIR=/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/pkg/tool/windows_amd64 ; PATH=/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/bin:/mingw64/bin:$PATH .\\cmd\\dist\\dist bootstrap -a -v=5)

# From MSDOS:
# pushd C:\Users\ray\msys2\MINGW-packages\mingw-w64-go\src\go-1.7.4\src
# set GOROOT_BOOTSTRAP=C:/msys64/mingw64/lib/go
# set CC_FOR_TARGET=gcc
# set GOROOT=C:/msys64/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4
# set GOBIN=C:/msys64/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/bin
# set GOARCH=amd64
# set GOOS=windows
# set GOHOSTARCH=amd64
# set GOHOSTOS=windows
# set GOTOOLDIR=C:/msys64/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/pkg/tool/windows_amd64
# set "PATH=C:/msys64/home/ray/msys2/MINGW-packages/mingw-w64-go/src/go-1.7.4/bin;C:/msys64/mingw64/bin;%PATH%"
# .\cmd\dist\dist bootstrap -a -v=5
