# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=zydis
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=4.1.1
pkgrel=1
pkgdesc="Fast and lightweight x86/x86-64 disassembler and code generation library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/zyantific/zydis"
msys2_references=(
  'cpe: cpe:2.3:a:zyantific:zydis'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-zycore-c")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python"
             'git')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('1e2ad2f00ca369c9be3846cc107068cd66002d4f7f7d835ee6ac3eec679b6a45')

prepare() {
  # fix incorrect encoding of .rc file
  dos2unix "${_realname}"/resources/VersionInfo.rc
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DZYDIS_BUILD_SHARED_LIB=ON \
      -DZYAN_SYSTEM_ZYCORE=ON \
      -DPython_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package_zydis() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p "dest${MINGW_PREFIX}/share/"
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc/ "dest${MINGW_PREFIX}/share/"
}

package_zydis-docs() {
  pkgdesc+=" (Documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# 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
