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

_realname=headers
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgdesc="MinGW-w64 headers for Windows (mingw-w64)"
pkgver=13.0.0.r388.gbb8673e5f
pkgrel=1
_commit='bb8673e5f137300b018d1dcdeab1c28c1d292e97'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.mingw-w64.org/"
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-headers/"
license=('spdx:ZPL-2.1 AND LGPL-2.1-or-later')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=("${MINGW_PACKAGE_PREFIX}-tools"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             'git')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
options=('!strip' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-remove-ireference-boolean.patch)
sha256sums=('8668e7f398b98b493e131fee57500466ab334c3fff0e6cf754e49aee5f223b44'
            'cad48616325b5e70afa4e02424021fc8d54e17dcbf86eae3df990d3500080236')

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-remove-ireference-boolean.patch"
}

build() {
  declare -a _extra_config
  if [[ ${MSYSTEM} == MINGW* ]]; then # MINGW32 & MINGW64
      _extra_config+=("--with-default-win32-winnt=0x601") # Windows 7
  elif [[ ${MSYSTEM} != CLANGARM64 ]]; then # UCR64 & CLANG64
      _extra_config+=("--with-default-win32-winnt=0x603") # Windows 8.1
  else # CLANGARM64
      _extra_config+=("--with-default-win32-winnt=0xA00") # Windows 10
  fi

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _extra_config+=("--with-default-msvcrt=ucrt")
  else
    _extra_config+=("--with-default-msvcrt=msvcrt")
  fi

  mkdir -p "${srcdir}"/headers-${MSYSTEM} && cd "${srcdir}"/headers-${MSYSTEM}

  "${srcdir}"/mingw-w64/mingw-w64-headers/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-sdk=all \
    "${_extra_config[@]}" \
    --enable-idl \
    --without-widl
}

package() {
  cd "${srcdir}"/headers-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_signal.h
  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_time.h
  rm "${pkgdir}"${MINGW_PREFIX}/include/pthread_unistd.h

  install -Dm644 "${srcdir}"/mingw-w64/mingw-w64-headers/ddk/readme.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/ddk-readme.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
