# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=capstone
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-capstone"
         "${MINGW_PACKAGE_PREFIX}-python-capstone")
pkgver=5.0.6
pkgrel=2
pkgdesc='Lightweight multi-platform, multi-architecture disassembly framework (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.capstone-engine.org/'
msys2_repository_url='https://github.com/capstone-engine/capstone'
msys2_references=(
  'purl: pkg:pypi/capstone'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!strip')
source=("https://github.com/capstone-engine/capstone/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "mingw_python.patch")
sha256sums=('240ebc834c51aae41ca9215d3190cc372fd132b9c5c8aa2d5f19ca0c325e28f9'
            '7021aa9ac55a55b174e9f36d11657e110fcde0c3f57536f1c24d91dcdb34ef0a')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/mingw_python.patch"
}

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

  # Does not define CAPSTONE_EXPORT for static library (#17779)
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-static"

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-shared"

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

package_capstone() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-shared"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/docs/README \
    -t "${pkgdir}"/${MINGW_PREFIX}/share/doc/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.TXT \
    -t "${pkgdir}"/${MINGW_PREFIX}/share/licenses/${_realname}
}

package_python-capstone() {
  depends=("${MINGW_PACKAGE_PREFIX}-capstone"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${_realname}-${pkgver}/bindings/python"

  LIBCAPSTONE_PATH="${srcdir}/build-${MSYSTEM}-shared/libcapstone.dll" \
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation

  LIBCAPSTONE_PATH="${srcdir}/build-${MSYSTEM}-shared/libcapstone.dll" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.TXT \
    -t "${pkgdir}"/${MINGW_PREFIX}/share/licenses/python-${_realname}
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
