# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=drmingw
pkgbase="mingw-w64-${_realname}"
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgver=0.9.11
pkgrel=2
pkgdesc="Just-in-Time (JIT) debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/jrfonseca/drmingw'
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git")
options=(!strip)
source=("${_realname}::git+https://github.com/jrfonseca/drmingw.git#tag=${pkgver}"
        0001-install-files.patch
        0002-drmingw-allow-posix-threads.patch)
sha256sums=('ba5b2e99d1e215ddfbc9fc038e77901a705fcbf7a8b241c0bd40c4ea636df431'
            '14b86049462b5c2ac505f4fbde1a49386ad918fd596e1c6d62bde5831eff15e6'
            '4faa655ed0edc371e64565fcd9e801fda59fd28150cf5b62a566927fa53e4144')

prepare() {
  cd "${srcdir}/${_realname}"
  git submodule update --init --depth=1
  git apply "${srcdir}/0001-install-files.patch"
  git apply "${srcdir}/0002-drmingw-allow-posix-threads.patch"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  mv -v "${pkgdir}"${MINGW_PREFIX}/bin/addr2line{,-drmingw}.exe

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  mv -v "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/LICENSE* \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
}
