# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: Vitaliy Berdinskikh ur6lad[at]i.ua
# Contributor: Michael Fellinger <m.fellinger@gmail.com>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - patch to disable LTO

pkgname=couchdb
pkgver=3.4.3
pkgrel=1
pkgdesc="Document-oriented database that can be queried and indexed in a MapReduce fashion using JSON"
arch=(x86_64)
url="https://couchdb.apache.org"
license=(Apache-2.0)
depends=(
  gcc-libs
  glibc
  icu
  ncurses
  zlib
)
makedepends=(
  erlang-asn1
  erlang-erts
  erlang-eunit
  erlang-os_mon
  erlang-reltool
  erlang-xmerl
  rebar3
)
checkdepends=(
  elixir
  procps-ng
  python
)
optdepends=('erlang-erts: for weatherreport')
options=(!makeflags)
backup=(
  etc/couchdb/local.ini
  etc/couchdb/vm.args
  etc/default/couchdb
)
source=(
  "https://archive.apache.org/dist/${pkgname}/source/${pkgver}/apache-${pkgname}-${pkgver}.tar.gz"{,.asc}
  couchdb.default
  couchdb.service
  couchdb.sysusers
  couchdb.tmpfiles
  arm.patch
)
sha256sums=('0357511b6fed70e3e64f4e75aa5d7cc2000cb0f264ef301702b1816427f72f20'
            'SKIP'
            '0d21fdd7851b3a6fa099b030cab0e96705b1d5a3a83a4ff1c871814d8742cae9'
            '6ab3d362f3fcd9079036c883dc09dc597584cb627831460f2e0c68a31aa052ae'
            '76404436346167dd19c3eace1164e35e3ac91ffed9bc3c20acb1b76e6d306f43'
            '7331b9675e3668c5f0d632bee5d154c061cc3e60d451bef3dc0d0ab2dadfa006'
            '2da85e0107a9dc6606dc349e7f89d5634749e54890905cd76a062ac53dc205f9')
# https://downloads.apache.org/couchdb/KEYS
validpgpkeys=(
  2EC788AE3F239FA13E82D215CDE711289384AE37 # Joan Touzet (CODE SIGNING KEY) <wohali@apache.org>
  D2B17F9DA23C0A10991AF2E3D9EE01E47852AEE4 # Jan Lehnardt <jan@apache.org>
  0BD7A98499C4AB41C910EE65FC04DFBC9657A78E # Nick Vatamaniuc <vatamane@gmail.com>
)

prepare() {
  cd apache-couchdb-${pkgver}
  sed -i 's|./data|/var/lib/couchdb|' configure

  patch -p1 -i ../arm.patch
}

build() {
  cd apache-couchdb-${pkgver}
  ./configure \
    --js-engine=quickjs \
    --disable-spidermonkey
  make release
}

check() {
  cd apache-couchdb-${pkgver}
  make check
}

package() {
  cd apache-couchdb-${pkgver}
  install -vdm755 "${pkgdir}"/usr/lib
  install -vdm755 "${pkgdir}"/etc/couchdb

  cp -vr rel/couchdb "${pkgdir}"/usr/lib/couchdb
  mv -v "${pkgdir}"/usr/lib/couchdb/etc/{default.ini,local.ini,vm.args} "${pkgdir}"/etc/couchdb/

  cd "${srcdir}"
  install -vDm644 ${pkgname}.default "${pkgdir}"/etc/default/${pkgname}
  install -vDm644 ${pkgname}.service -t "${pkgdir}"/usr/lib/systemd/system/
  install -vDm644 ${pkgname}.tmpfiles "${pkgdir}"/usr/lib/tmpfiles.d/${pkgname}.conf
  install -vDm644 ${pkgname}.sysusers "${pkgdir}"/usr/lib/sysusers.d/${pkgname}.conf

  # Remove some cruft
  rm -vr "${pkgdir}"/usr/lib/couchdb/erts-*/{doc,include,lib,man,src}
  rm -vr "${pkgdir}"/usr/lib/couchdb/etc/
  rm -vr "${pkgdir}"/usr/lib/couchdb/lib/couch-${pkgver}/priv/couch_{ejson_compare,js}
}