# $Id: PKGBUILD 144600 2015-10-21 21:55:50Z foutrelis $
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: Mathias Stearn <mathias@10gen.com>
# Contributor: Alec Thomas

pkgname=mongodb
pkgver=3.0.7
pkgrel=2
pkgdesc='A high-performance, open source, schema-free document-oriented database'
arch=('i686' 'x86_64')
url='http://www.mongodb.org'
license=('AGPL3')
depends=('pcre' 'snappy' 'openssl' 'libsasl' 'boost-libs') # 'libyaml' 'yaml-cpp' 'v8' 'gperftools')
makedepends=('scons' 'readline' 'ncurses' 'libpcap' 'boost' 'clang')
checkdepends=('python2-pymongo' 'python2-yaml')
optdepends=('libpcap: needed for mongosniff'
            'mongodb-tools: mongoimport, mongodump, mongotop, etc')
backup=('etc/mongodb.conf')
install=mongodb.install
source=("http://downloads.mongodb.org/src/mongodb-src-r${pkgver}.tar.gz"
        'fix-compile-error-in-clang-with-m32-and-64.patch'
        'mongodb.conf' 'mongodb.service')
sha512sums=('183691c5dc137ea4b86d53d24d2935fd66ed8de60fbf5eca0a0a39c159be1f29043cd655cde1a213a98a76b2b3e6369dbccf790b02a1cc4b622c067ad3c0c91b'
            '94e5b61a81eb3d907f34430cec1fb3bccc2ee5730c8dd33d00adb26fc144b672e605516cc2a83c4ca4aad2a9d67643fa42dd8c6fe4f1f07e1336901cc17ea9ec'
            '05dead727d3ea5fe8af1a3c3888693f6b3e2b8cb7f197a5d793352e10d2c524e96c9a5c55ad2e88c1114643a9612ec0b26a2574b48a5260a9b51ec8941461f1c'
            '177251404b2e818ae2b546fe8b13cb76e348c99e85c7bef22a04b0f07b600fd515a309ede50214f4198594388a6d2b31f46e945b9dae84aabb4dfa13b1123bb9')

prepare() {
  cd mongodb-src-r${pkgver}

  # https://jira.mongodb.org/browse/SERVER-17461
  # https://github.com/gperftools/gperftools/commit/772a686c4545
  patch -d src/third_party/gperftools-2.2 -Np1 <../fix-compile-error-in-clang-with-m32-and-64.patch
}

_scons_args=(
  --use-system-pcre
  --use-system-snappy
  --use-sasl-client
  --use-system-boost
  --variant-dir=build
  --ssl
  --c++11=on
  --disable-warnings-as-errors
  --cc=clang --cxx=clang++
  LIBS=atomic
)
# --use-system-v8 (Doesn't compile)
# --use-system-yaml (Doesn't compile)
# --use-system-tcmalloc (Disabled as upstream suggests in https://jira.mongodb.org/browse/SERVER-17447?focusedCommentId=841890&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-841890)
# LIBS=atomic is a workaround for https://jira.mongodb.org/browse/SERVER-17447

build() {
  export SCONSFLAGS="$MAKEFLAGS"

  cd mongodb-src-r${pkgver}

  scons all "${_scons_args[@]}"
}

check() {
  # All tests disabled for 3.0.7

  export SCONSFLAGS="$MAKEFLAGS"

  cd mongodb-src-r${pkgver}

  # TODO: jstests
  # python2 buildscripts/resmoke.py --dbpathPrefix="$srcdir"

  scons dbtest "${_scons_args[@]}"
  # python2 buildscripts/resmoke.py --set suite dbtest

  scons unittests "${_scons_args[@]}"
  # python2 buildscripts/resmoke.py --set suite unittests
}

package() {
  export SCONSFLAGS="$MAKEFLAGS"

  cd mongodb-src-r${pkgver}

  scons install --prefix="$pkgdir/usr" "${_scons_args[@]}"

  install -Dm644 "$srcdir/mongodb.conf" "$pkgdir/etc/mongodb.conf"
  install -Dm644 "$srcdir/mongodb.service" "$pkgdir/usr/lib/systemd/system/mongodb.service"
  install -dm700 "$pkgdir/var/lib/mongodb"
  install -dm755 "$pkgdir/var/log/mongodb"
}
