# Maintainer: luadebug lin@sz.cn.eu.org

_realname=libmem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.0.5
pkgrel=1
pkgdesc='Advanced Game Hacking Library for C, Modern C++'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/rdbo/libmem/'
msys2_repository_url='https://github.com/rdbo/libmem/'
license=('spdx:AGPL-3.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-capstone"
             "${MINGW_PACKAGE_PREFIX}-keystone")
source=("https://github.com/rdbo/libmem/archive/refs/tags/${pkgver}.tar.gz"
        "0002-debundle-dependencies.patch"
        "0003-add-pc-in.patch")
sha256sums=('9693d38b17b000b06cd9fbaff72f4e0873d3cf219a6e99a20bb90cf98a7b562d'
            'cc2d5bf81ef06639a5d983e68a29c3a6198db9cd1290ae267798db983e4f8da0'
            'b44fe15184007d640aa67b067a5acd501f84ba4963b79ac255bf4db1749b7f22')
noextract=("${pkgver}.tar.gz")

prepare() {
  tar -xzf "${srcdir}/${pkgver}.tar.gz" -C "${srcdir}" --exclude="${_realname}-${pkgver}/compile_commands.json" || true
  cd "${srcdir}/${_realname}-${pkgver}"
  rm "PreLoad.cmake"
  patch -p1 -i "${srcdir}"/0002-debundle-dependencies.patch
  patch -p1 -i "${srcdir}"/0003-add-pc-in.patch
}

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

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-int-conversion -Wno-incompatible-pointer-types"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DLIBMEM_BUILD_STATIC=OFF \
      -DPACKAGE_VERSION="${pkgver}" \
      "${extra_config[@]}" \
      "../${_realname}-${pkgver}"

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
