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

_realname=crt
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=9.0.0.6246.ae63cde27
pkgrel=1
_commit='ae63cde274dc1d25756df7e730eeb3af01cac687'
pkgdesc='MinGW-w64 CRT for Windows'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://mingw-w64.sourceforge.io/'
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-headers-git")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-binutils")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' 'staticlibs' '!buildflags' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-Allow-to-use-bessel-and-complex-functions-without-un.patch
        0002-DirectX-9-fixes-for-VLC.patch
        0001-headers-add-AV1-support-to-dxva.h.patch)
sha256sums=('SKIP'
            'd641257f7e1469aff89adc33e57702b75fe9667ca035978f890eae1020b6814c'
            '09b1c7b62f666a07609af57e10c2b0ad815b78356f4b0f1fb6d827a1109a0ec7'
            '273ce6dd765f3a6caf3b8a0021f34ee41647ac8e08767e93b06b7d187d430979')

pkgver() {
  cd "${srcdir}/mingw-w64"
  printf "9.0.0.%s.%s" "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd ${srcdir}/mingw-w64

  git am --committer-date-is-author-date "${srcdir}/0001-Allow-to-use-bessel-and-complex-functions-without-un.patch"
  git am --committer-date-is-author-date "${srcdir}/0002-DirectX-9-fixes-for-VLC.patch"
  git am --committer-date-is-author-date "${srcdir}/0001-headers-add-AV1-support-to-dxva.h.patch"
}

build() {
  msg "Building ${MINGW_CHOST} CRT"
  local _crt_configure_args
  case "$CARCH" in
    i686)
      _crt_configure_args="--disable-lib64 --enable-lib32"
    ;;
    x86_64)
      _crt_configure_args="--disable-lib32 --enable-lib64"
    ;;
    armv7)
      _crt_configure_args="--disable-lib32 --disable-lib64 --enable-libarm32"
    ;;
    aarch64)
      _crt_configure_args="--disable-lib32 --disable-lib64 --disable-libarm32 --enable-libarm64"
    ;;
  esac

  local _default_msvcrt=msvcrt
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _default_msvcrt=ucrt
  fi

  [[ -d ${srcdir}/crt-${MINGW_CHOST} ]] && rm -rf ${srcdir}/crt-${MINGW_CHOST}
  mkdir -p ${srcdir}/crt-${MINGW_CHOST} && cd ${srcdir}/crt-${MINGW_CHOST}
  ${srcdir}/mingw-w64/mingw-w64-crt/configure \
    --prefix=${MINGW_PREFIX}/${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-sysroot=${MINGW_PREFIX}/${MINGW_CHOST} \
    --with-default-msvcrt=${_default_msvcrt} \
    --enable-wildcard \
    ${_crt_configure_args}
  make
}

package() {
  msg "Installing ${MINGW_CHOST} crt"
  cd ${srcdir}/crt-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install-strip

  msg "Installing MinGW-w64 licenses"
  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
}
