# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=unicorn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python-unicorn")
_rc=-rc1
_basever=1.0.2
pkgver=${_basever}${_rc//-/}
pkgrel=1
pkgdesc="A lightweight multi-platform, multi-architecture CPU emulator framework based on QEMU (mingw-w64)"
url='https://www.unicorn-engine.org/index.html'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/unicorn-engine/unicorn/archive/${_basever}${_rc}.tar.gz"
        001-mingw.patch)
sha512sums=('c710b4a6c24a2b51def4a732a2186dd774e111287950f80fccb01cd0a9dc9788302e15bc55146df298412a824123b862ddde3a9de3627d7912dea9bc29ee27d3'
            'cdf7567d143ff8b6918853a4bdf2d29c7fd3724688cd69cf9a111f97e2b68eb5b21cd8ea8d3e2979fa58cad399e78a114b43dec1cec02de85a9f17d4d286b2cb')

prepare() {
  cd "${_realname}-${_basever}${_rc}"
  patch -p1 -i ${srcdir}/001-mingw.patch
  
  sed 's|-O3|-O2|g' -i Makefile qemu/configure
  sed 's|-g ||g' -i qemu/configure
  sed 's|UNICORN_DEBUG ?= yes|UNICORN_DEBUG ?= no|g' -i config.mk
  sed -r 's|(python)$|\13|' -i bindings/python/*.py
}

build() {
  cd "${_realname}-${_basever}${_rc}"
  make clean

  UNICORN_QEMU_FLAGS="--python=${MINGW_PREFIX}/bin/python" \
  make PREFIX="${MINGW_PREFIX}"

  (cd bindings
    ${MINGW_PREFIX}/bin/python const_generator.py python
  )
  (cd bindings/python
    ${MINGW_PREFIX}/bin/python setup.py build
  )
}

package_unicorn() {
  cd "${_realname}-${_basever}${_rc}"

  make DESTDIR="${pkgdir}" PREFIX="${MINGW_PREFIX}" install

  install -Dm644 "${srcdir}/${_realname}-${_basever}${_rc}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${_basever}${_rc}/COPYING.LGPL2" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LGPL2"
  install -Dm644 "${srcdir}/${_realname}-${_basever}${_rc}/COPYING_GLIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_GLIB"
}

package_python-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-unicorn")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  
  cd "${_realname}-${_basever}${_rc}/bindings/python"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/samples"
}

# 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;
