# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Pavel Borzenkov <pavel@voidptr.ru>
# Contributor: aksr <aksr at t-com dot me>

pkgname=criu
pkgver=3.18
pkgrel=3.1
pkgdesc='Utilities to checkpoint and restore processes in userspace'
arch=(x86_64 powerpc64le)
url='https://criu.org'
license=('GPL2' 'LGPL2.1')
depends=(
  'libbsd'
  'libnet'
  'libnl'
  'protobuf-c'
  'python-protobuf'
  'gnutls'
  'nftables'
)
makedepends=(
  'git'
  'xmlto'
  'asciidoc'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
)
# can't run tests due to privilege escalation
# https://github.com/checkpoint-restore/criu/issues/434
#checkdepends=('libaio' 'python-yaml')
options=('!buildflags' '!lto')
_commit='4c1a2ac41bb80843c927d2fde8f2ff4186f8d278'
source=(
  "$pkgname::git+https://github.com/checkpoint-restore/criu#commit=$_commit"
  'no-python-pip.patch'
  'no-recompile-on-install.patch'
  'no-amdgpu-manpage.patch'
)
b2sums=('SKIP'
        'd83da0ce0222c1aea1fc0c97bbf8a40f3cd5a6b5d55ee973b64f97bd9769df265b148e89cee8ee6564f065adc00552b511904f322555ac659b735933d42a9a64'
        'e4b7c4831fa513d602c73e377847705240a6a42ee1986effd10a589784bd0ad818032ff8283c1f9fd17cb7ddf3204e4a932796a1df816afc30a0e594c92b50f6'
        '9c713724e8f6b062f7a09e34555d31e5aa0315db6308b7527835484eaad8dbf5deac5c66521bf5a819462d5f38c64f6602ba421f7bbb73180a3b05189816c8f6')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # do not invoke pip
  patch -p1 -i "$srcdir/no-python-pip.patch"

  # prevent recompilation with `make install`
  patch -p1 -i "$srcdir/no-recompile-on-install.patch"

  # do not install amdgpu_plugin manpage
  patch -p1 -i "$srcdir/no-amdgpu-manpage.patch"
}

build() {
  cd "$pkgname"

  make


  # build python wheel
  cd crit

  export CRIU_VERSION_MAJOR="${pkgver%%.*}"
  export CRIU_VERSION_MINOR="${pkgver##*.}"

  python -m build --wheel --no-isolation
}

#check() {
#  cd "$pkgname"
#
#  make test
#}

package() {
  cd "$pkgname"

  # install python wheel
  python -m installer --destdir="$pkgdir" crit/dist/*.whl

  # rest of the application
  make \
    DESTDIR="$pkgdir" \
    PREFIX=/usr \
    SBINDIR=/usr/bin \
    LIBDIR=/usr/lib \
    LIBEXECDIR=/usr/lib \
    install

  # remove empty directories
  rm -rf \
    "$pkgdir/usr/include/compel/common/asm" \
    "$pkgdir/var"
}
