# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=winpthreads
_mingw_suff=mingw-w64-cross
pkgname="${_mingw_suff}-${_realname}-git"
replaces=("${_mingw_suff}-${_realname}")
provides=("${_mingw_suff}-${_realname}")
conflicts=("${_mingw_suff}-${_realname}")
pkgver=9.0.0.6158.1c773877
pkgrel=1
pkgdesc="MinGW-w64 winpthreads library for cross-compiler"
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
makedepends=("git" "${_mingw_suff}-gcc" "${_mingw_suff}-binutils" "${_mingw_suff}-crt" "${_mingw_suff}-headers")
depends=("${_mingw_suff}-crt")
optdepends=()
backup=()
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs' '!buildflags')
_commit='1c773877f4a13c8bd7bfb8da80e1e8761a889f51'
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-Define-__-de-register_frame_info-in-fake-libgcc_s.patch)
sha256sums=('SKIP'
            'd9e8af81682d9bf70e3d87506f51156cec61260b810a234bce861cb2eb3a5919')

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

pkgver() {
  cd "$srcdir/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd "$srcdir/mingw-w64"
  git am "${srcdir}"/0001-Define-__-de-register_frame_info-in-fake-libgcc_s.patch
  cd "${srcdir}"/mingw-w64/mingw-w64-libraries/winpthreads
  autoreconf -vfi
}

build() {
  for _target in ${_targets}; do
    mkdir -p ${srcdir}/winpthreads-build-${_target} && cd ${srcdir}/winpthreads-build-${_target}
    ${srcdir}/mingw-w64/mingw-w64-libraries/winpthreads/configure \
      --prefix=/opt/${_target} \
      --build=${CHOST} \
      --host=${_target} \
      --enable-static \
      --enable-shared
    make
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/winpthreads-build-${_target}
    make DESTDIR=${pkgdir} install
  done
}
