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

pkgname="msys2-w32api-runtime"
pkgver=13.0.0.r0.gde62c283f
pkgrel=1
pkgdesc="Win32 API import libs for MSYS2 toolchain"
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
depends=('msys2-w32api-headers')
options=('staticlibs' '!strip' '!buildflags')
makedepends=('git' 'autotools')
if [[ "${CARCH}" == 'i686' ]]; then
  makedepends+=('mingw-w64-cross-mingw32-gcc')
else
  makedepends+=('mingw-w64-cross-mingw64-gcc')
fi
_commit='de62c283f3383bd825c98518cb0a42ba071d4ac2'
source=('mingw-w64'::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('be06c383a44f9ccb0da7884e72b3ecffb503e2bddc10ce2b7eb1e0fa9fa08d6f')
msys2_references=(
  'cygwin: w32api-runtime'
  '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
}

build() {
  cd ${srcdir}/mingw-w64/mingw-w64-crt

  local _libs=
  case ${CARCH} in
    i686)
      _libs="--enable-lib32 --disable-lib64"
    ;;
    x86_64)
      _libs="--enable-lib64 --disable-lib32"
    ;;
  esac

  unset CC CXX

  ./configure \
  --prefix=/crt \
  --build=${CHOST} \
  --host=${CARCH}-w64-mingw32 \
  --enable-w32api \
  --disable-dependency-tracking \
  ${_libs} \
  CFLAGS='-I/usr/include/w32api' \
  CXXFLAGS='-I/usr/include/w32api' \
  CPPFLAGS='-I/usr/include/w32api'

  make
}

package() {
  cd ${srcdir}/mingw-w64/mingw-w64-crt

  make DESTDIR=${pkgdir} install
  mv ${pkgdir}/crt ${pkgdir}/usr
}
