# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>

pkgbase=unicorn
pkgname=('unicorn' 'python-unicorn' 'ruby-unicorn-engine')
pkgver=1.0.3
pkgrel=1
pkgdesc='Lightweight, multi-platform, multi-architecture CPU emulator framework based on QEMU'
url='https://www.unicorn-engine.org'
arch=('x86_64')
license=('GPL2')
makedepends=('python' 'python-setuptools' 'ruby' 'ruby-rdoc')
checkdepends=('cmocka')
options=('staticlibs' '!emptydirs')
source=(https://github.com/unicorn-engine/unicorn/archive/${pkgver}/${pkgbase}-${pkgver}.tar.gz)
sha512sums=('10f1b5194358c65bc94723eaafd7b4167fee18f96a4810f46f8c78ed1ef5584546e66e8c910fa4eeadd791fcd73edea68f898e337081409fd188b4090b78a7ca')
b2sums=('130d494d03aa9aa1b27e6806856eb36c1871af6e8b89aef9136baa6ca8b726ba2eab778488185e96c47bf3771dd8bce19bf4fd5090888d28433a6129c827d88c')

prepare() {
  cd ${pkgbase}-${pkgver}
  sed 's|-g ||g' -i qemu/configure
  sed 's|UNICORN_DEBUG ?= yes|UNICORN_DEBUG ?= no|g' -i config.mk
  sed 's|VERSION = "1.0.1"|VERSION = "'${pkgver}'"|' -i bindings/ruby/unicorn_gem/lib/unicorn_engine/version.rb
}

build() {
  cd ${pkgbase}-${pkgver}
  export unicorn_LDFLAGS="${LDFLAGS}"
  export UNICORN_CFLAGS="${CFLAGS} ${CPPFLAGS}"
  export UNICORN_QEMU_FLAGS="--extra-ldflags=\"$LDFLAGS\""
  make
  (cd bindings
    python const_generator.py python
    python const_generator.py ruby
  )
  (cd bindings/python
    python setup.py build
  )
  (cd bindings/ruby/unicorn_gem
    gem build unicorn-engine.gemspec
  )
}

check() {
  cd ${pkgbase}-${pkgver}
  # Tests are not supported right now, hope they get some love soon
  # make test
}

package_unicorn() {
  depends=('glibc')
  provides=('libunicorn.so')
  cd ${pkgbase}-${pkgver}
  make DESTDIR="${pkgdir}" install
  install -Dm 644 samples/*.c -t "${pkgdir}/usr/share/doc/${pkgname}/samples"
}

package_python-unicorn() {
  depends=('python' 'unicorn')
  cd ${pkgbase}-${pkgver}/bindings/python
  python setup.py install --root="${pkgdir}" -O1 --skip-build
  rm -r "${pkgdir}"/usr/lib/python*/site-packages/unicorn/lib
  install -Dm 644 sample* shellcode.py -t "${pkgdir}/usr/share/doc/${pkgname}/samples"
}

package_ruby-unicorn-engine() {
  depends=('ruby' 'unicorn')
  replaces=('ruby-unicorn')
  cd ${pkgbase}-${pkgver}/bindings/ruby/unicorn_gem

  local _gemdir="$(gem env gemdir)"
  gem install --ignore-dependencies --no-user-install -i "${pkgdir}${_gemdir}" \
    -n "${pkgdir}/usr/bin" ${pkgbase}-*.gem -- \
    --with-opt-include="${srcdir}/${pkgbase}-${pkgver}/include" \
    --with-opt-lib="${srcdir}/${pkgbase}-${pkgver}"

  install -Dm 644 ../sample* -t "${pkgdir}/usr/share/doc/${pkgname}/samples"

  cd "${pkgdir}/${_gemdir}"
  rm -r cache gems/unicorn-engine-${pkgver}/ext \
    extensions/*/*/unicorn-engine-${pkgver}/{mkmf.log,gem_make.out}
}

# vim: ts=2 sw=2 et:
