# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('db' 'libdb' 'libdb-devel' 'db-docs')
pkgver=5.3.28
pkgrel=3
pkgdesc="The Berkeley DB embedded database system"
url="https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/overview/index.html"
license=('custom')
arch=('i686' 'x86_64')
depends=('sh')
source=(http://download.oracle.com/berkeley-db/db-${pkgver}.tar.gz
        #http://download.oracle.com/otn/berkeley-db/db-${pkgver}.tar.gz
        5.3-java.patch
        5.3-tcl.patch
        5.3-vpath.patch
        https://src.fedoraproject.org/rpms/libdb/raw/d6841b3b46a51db98c162347211a5a64d154ea37/f/db-5.3.28-atomic_compare_exchange.patch
        db-5.3.28-msys2.patch)
options=('!makeflags')
sha256sums=('e0a992d740709892e81f9d93f06daf305cf73fb81b545afe72478043172c3628'
            'a925b88e58cff6d4aeed3eecfa2cf4c606f2ed469607d8fd0c5c0a9c4b5a8bd1'
            '2166bac574f60d42fe6dd6c03223d6595accf303375be397e277b38676b8cb08'
            '7614933d2770c2aa6864cd731c114b90a4e4c1a8a988daabd9b6f203ab8f1f21'
            'eb58b5764e16c6f81df8ff80964ef2e071ca5cbb9e24d37f45a831107afb68cc'
            '42ae2995fefbd06853278db1620e8a15dbfd580e1a26c7f26e6d81e734e064cd')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -Np2 -i ${srcdir}/5.3-java.patch
  patch -Np2 -i ${srcdir}/5.3-tcl.patch
  patch -Np2 -i ${srcdir}/5.3-vpath.patch
  patch -Np1 -i ${srcdir}/db-5.3.28-atomic_compare_exchange.patch
  patch -Np1 -i ${srcdir}/db-5.3.28-msys2.patch

  cd dist
  ac_macrodir=aclocal libtoolize --copy --force --install
  ./s_config
}

build() {
  cd "${srcdir}"/$pkgname-${pkgver}/build_unix
  ../dist/configure \
    --prefix=/usr \
    --enable-compat185 \
    --enable-shared \
    --enable-static \
    --enable-dynamic \
    --enable-cxx \
    --enable-dbm \
    --disable-java \
    --disable-tcl \
    --disable-test
  make
  make DESTDIR=${srcdir}/dest docdir=/usr/share/doc/db/html emode=755 fmode=644 install
}

package_db() {
  depends=(libdb=$pkgver)
  groups=('Database')

  mkdir -p ${pkgdir}/usr/{bin,share}

  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
}

package_libdb() {
  depends=('gcc-libs')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libdb-devel() {
  pkgdesc="libdb headers and libraries"
  options=('staticlibs')
  depends=("libdb=${pkgver}")
  groups=('development')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

  # replace unslotted libs with symlinks
  rm -f ${pkgdir}/usr/lib/libdb{_cxx,}.a
  cp -f ${pkgdir}/usr/lib/libdb-${pkgver%.*}.a  ${pkgdir}/usr/lib/libdb.a
  cp -f ${pkgdir}/usr/lib/libdb-${pkgver%.*}.dll.a ${pkgdir}/usr/lib/libdb.dll.a
  cp -f ${pkgdir}/usr/lib/libdb_cxx-${pkgver%.*}.a ${pkgdir}/usr/lib/libdb_cxx.a
  cp -f ${pkgdir}/usr/lib/libdb_cxx-${pkgver%.*}.dll.a ${pkgdir}/usr/lib/libdb_cxx.dll.a
  #cp -f ${pkgdir}/usr/lib/libdb_sql-${pkgver%.*}.a ${pkgdir}/usr/lib/libdb_sql.a
  #cp -f ${pkgdir}/usr/lib/libdb_sql-${pkgver%.*}.dll.a ${pkgdir}/usr/lib/libdb_sql.dll.a
}

package_db-docs() {
  pkgdesc="BerkleyDB documentation"

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
}
