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

_realname=winpthreads
_mingw_suff=mingw-w64-cross
pkgbase="${_mingw_suff}-${_realname}"
_targetpkgs=("${_mingw_suff}-ucrt64-${_realname}" "${_mingw_suff}-mingw32-${_realname}" "${_mingw_suff}-mingw64-${_realname}")
pkgname=("${_mingw_suff}-${_realname}" "${_targetpkgs[@]}")
pkgver=13.0.0.r1.gb45abfec4
pkgrel=1
pkgdesc="MinGW-w64 winpthreads library for cross-compiler"
arch=('i686' 'x86_64')
url="https://www.mingw-w64.org/"
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-libraries/winpthreads"
license=('custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
makedepends=("git" "${_mingw_suff}-gcc" "${_mingw_suff}-binutils" "${_mingw_suff}-crt" "${_mingw_suff}-headers" 'autotools')
optdepends=()
backup=()
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs' '!buildflags')
_commit='b45abfec4e116b33620de597b99b1f0af3ab6a6a'
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=('38cb5bc2209c942f76acad3c5d7083782b1cceb64878ba24a4c876ea053d40b9'
            '2e779bcc60a1422b23e0cfdb5c0f6851f2382592bb4675d08a6bdef78d5e5c10')
msys2_references=(
  'archlinux: mingw-w64-winpthreads'
  'cpe: cpe:/a:mingw-w64:mingw-w64'
)

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

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

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

_build() {
  _target=$1
  mkdir -p ${srcdir}/winpthreads-build-${_target} && cd ${srcdir}/winpthreads-build-${_target}
  unset CC
  ${srcdir}/mingw-w64/mingw-w64-libraries/winpthreads/configure \
    --prefix=/opt/${_target} \
    --build=${CHOST} \
    --host=${_target} \
    --enable-static \
    --enable-shared
  make
}

_package() {
  _target=$1
  cd ${srcdir}/winpthreads-build-${_target}
  make DESTDIR=${pkgdir} install
}

build() {
  for _pkg in "${_targetpkgs[@]}"; do
    case "$_pkg" in
      "${_mingw_suff}-ucrt64-${_realname}") _build "x86_64-w64-mingw32ucrt" ;;
      "${_mingw_suff}-mingw32-${_realname}") _build "i686-w64-mingw32" ;;
      "${_mingw_suff}-mingw64-${_realname}") _build "x86_64-w64-mingw32" ;;
    esac
  done
}

package_mingw-w64-cross-winpthreads() {
  pkgdesc+=" (meta package, for backward compatibility)"
  depends=("${_targetpkgs[@]}")
  provides=("${_mingw_suff}-${_realname}-git")
  conflicts=("${_mingw_suff}-${_realname}-git")
  replaces=("${_mingw_suff}-${_realname}-git")
}

package_mingw-w64-cross-ucrt64-winpthreads() {
  depends=("${_mingw_suff}-ucrt64-crt")
  conflicts=("${_mingw_suff}-${_realname}<=12.0.0.r0.g819a6ec2e-1")

  _package "x86_64-w64-mingw32ucrt"
}

package_mingw-w64-cross-mingw32-winpthreads() {
  depends=("${_mingw_suff}-mingw32-crt")
  conflicts=("${_mingw_suff}-${_realname}<=12.0.0.r0.g819a6ec2e-1")

  _package "i686-w64-mingw32"
}

package_mingw-w64-cross-mingw64-winpthreads() {
  depends=("${_mingw_suff}-mingw64-crt")
  conflicts=("${_mingw_suff}-${_realname}<=12.0.0.r0.g819a6ec2e-1")

  _package "x86_64-w64-mingw32"
}
