# $Id: PKGBUILD 139251 2015-09-06 04:15:23Z anatolik $
# Maintainer: Anatol Pomozov <anatol.pomozov@gmail.com>
# Contributor: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Sigmund Lahn <sigmund@lahn.no>

pkgname=rethinkdb
pkgver=2.1.3
_tag=rethinkdb-$pkgver
pkgrel=1
pkgdesc='Distributed powerful and scalable NoSQL database'
arch=(i686 x86_64)
url='http://www.rethinkdb.com/'
license=(AGPL)
depends=(protobuf ncurses curl icu)
makedepends=(boost python2 wget)
backup=(etc/rethinkdb/instances.d/default.conf)
install=rethinkdb.install
options=(!emptydirs)
source=(
  http://download.rethinkdb.com/dist/$_tag.tgz
  rethinkdb-tmpfile.conf
  rethinkdb.service
)
sha256sums=('8dbfbd89e1053eda7acde7e0bb9c4ceb64c5371ceb9b3e16019e6fc60776890f'
            '656d3a42e75d087e723f71aa320fdd91cbbb82071ef72eb11fd3e4a619b429a4'
            'e56bffa2b9ebc3a00ef566ab2be0719a633c89d961a2461dfa2d9ffdb258c1a2')

prepare() {
  cd $_tag

  sed \
    -e 's|#!/usr/bin/python|#!/usr/bin/python2|' \
    -e 's|#!/usr/bin/env python|#!/usr/bin/env python2|' \
    -i scripts/*.py external/v8_*/tools/*.py
  sed -e 's|exec python|exec python2|g' -i external/v8_*/build/gyp/gyp
  sed -e 's|\bpython\b|python2|g' -i external/v8_*/{Makefile,*/*.gyp,*/*.gypi,*/*/*.gyp}
}

build() {
  cd $_tag
  export PYTHON=/usr/bin/python2
  ./configure --fetch v8 --fetch jemalloc --dynamic all --enable-precompiled-web --prefix=/usr --sysconfdir=/etc
  make ALLOW_WARNINGS=1
}

check() {
  cd $_tag

  # these tests are flaky and extremely slow
  # make build/release/rethinkdb-unittest
  # ./build/release/rethinkdb-unittest --gtest_filter=-RDBBtree.*:RDBInterrupt.*
  # some tests might be flaky on btrfs filesystem
}

package() {
  cd $_tag
  make DESTDIR="$pkgdir" install

  install -Dm644 "$srcdir"/rethinkdb-tmpfile.conf "$pkgdir"/usr/lib/tmpfiles.d/rethinkdb.conf
  install -Dm644 "$srcdir"/rethinkdb.service "$pkgdir"/usr/lib/systemd/system/rethinkdb@.service

  # create 'default' database instance
  mv "$pkgdir"/etc/rethinkdb/default.conf.sample "$pkgdir"/etc/rethinkdb/instances.d/default.conf
  sed -e 's|# directory=/var/lib/rethinkdb|directory=/var/lib/rethinkdb|' \
      -i "$pkgdir"/etc/rethinkdb/instances.d/default.conf

  # Arch uses systemd, no need for init.d scripts
  rm -r "$pkgdir"/etc/init.d
}
