# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=db
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.0.19
pkgrel=4
pkgdesc="The Berkeley DB embedded database system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.oracle.com/technetwork/database/database-technologies/berkeleydb/overview/index.html"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=(http://download.oracle.com/berkeley-db/db-${pkgver}.tar.gz
        mingw.patch)
sha256sums=('2917c28f60903908c2ca4587ded1363b812c4e830a5326aaa77c9879d13ae18e'
            '009ce8f8bf5fd18ce63a1c4d3ee972374d5f968754b028d4017d546755d3de86')

prepare()
{
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/mingw.patch
}

build() {
  CFLAGS+=" -DUNICODE -D_UNICODE"
  CXXFLAGS+=" -DUNICODE -D_UNICODE"
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  # for ucrt
  export ac_cv_func_time=yes
  export ac_cv_func_localtime=yes

  ../${_realname}-${pkgver}/dist/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-compat185 \
    --enable-mingw \
    --enable-shared \
    --enable-static \
    --disable-rpath \
    --enable-cxx \
    --enable-sql \
    --enable-sql-codegen \
    --enable-stl \
    --enable-dbm \
    --disable-tcl \
    --disable-replication \
    --docdir='${prefix}/share/doc/db'

  make LIBSO_LIBS=-lpthread
}

check() {
  cd ${srcdir}/build-${CARCH}
  make cutest
  if [ -x ./cutest.exe ]; then
    ./cutest.exe || true
  fi
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
