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

pkgname="msys2-w32api-runtime"
pkgver=9.0.0.6158.1c773877
pkgrel=1
pkgdesc="Win32 API import libs for MSYS2 toolchain"
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=('msys2-devel')
depends=('msys2-w32api-headers')
options=('staticlibs' '!strip' '!buildflags')
makedepends=('git' 'mingw-w64-cross-gcc')
_commit='1c773877f4a13c8bd7bfb8da80e1e8761a889f51'
source=('mingw-w64'::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('SKIP')

MINGW_CHOST=${CARCH}-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
}

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

  ./configure \
  --prefix=/crt \
  --build=${CHOST} \
  --host=${MINGW_CHOST} \
  --enable-w32api \
  ${_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
}
