# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=tools
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
provides=("${_mingw_suff}-${_realname}-git")
conflicts=("${_mingw_suff}-${_realname}-git")
replaces=("${_mingw_suff}-${_realname}-git")
pkgdesc="MinGW-w64 headers for cross-compiler"
pkgver=12.0.0.r0.g819a6ec2e
pkgrel=1
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
makedepends=('git' 'autotools' "${_mingw_suff}-gcc" "${_mingw_suff}-binutils")
options=('!strip' '!libtool' '!emptydirs' '!buildflags')
_commit='819a6ec2ea87c19814b287e21d65e0dc7f05abba'
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('41878f44fe3b6c347fc99ab8a90d384f61bd3ae25cfb2a62a7e24d4f0d14f720')
msys2_references=(
  # doesn't exist in arch, this is the next best thing
  'archlinux: mingw-w64-headers'
)
_tools="gendef genidl genpeimg" #widl

_targets="x86_64-w64-mingw32ucrt i686-w64-mingw32 x86_64-w64-mingw32"

pkgver() {
  cd "${srcdir}/mingw-w64"

  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/mingw-w64"
}

build() {
  for _target in ${_targets}; do
    msg "Configuring ${_target} tools"
    for cur in ${_tools}; do
      mkdir -p ${srcdir}/${cur}-${_target} && cd ${srcdir}/${cur}-${_target}
      unset CC
      ${srcdir}/mingw-w64/mingw-w64-tools/${cur}/configure \
        --build=${CHOST} \
        --host=${_target} \
        --prefix=/opt/${_target}
      
      make
    done
  done
}

package() {
  for _target in ${_targets}; do
    msg "Installing ${_target} tools"
    for cur in ${_tools}; do
      cd ${srcdir}/${cur}-${_target}
      make DESTDIR=${pkgdir} install
    done
  done
}
