# Maintainer (arch): Fabian Bornschein <fabiscafe-at-mailbox-dot-org>
# Maintainer (arch): Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>
# Contributor: Andreas Grapentin <andreas@grapentin.org>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: Isaac David <isacdaavid@at@isacdaavid@dot@info>
# Contributor: Freemor <freemor@freemor.ca>
# Contributor: bill-auger <bill-auger@programmer.net>
# Maintainer: Parabola Hackers <dev@lists.parabola.nu>


# parabola changes and rationale:
# privacy:
#   - removed libgweather->geocode-glib-2 support
#   - removed libgdata support
#   - removed google support
# technical:
#   - pinned to 'icu' sodep
#   - build from versioned source-ball, rather than VCS sources
#   - build in build(), rather than in package()


pkgbase=evolution-data-server
pkgname=(
  evolution-data-server
  libedataserverui4
  evolution-data-server-docs
)
pkgver=3.50.1
pkgrel=2
[[ "${CARCH}" == i686 ]] && pkgver=3.46.2 pkgrel=3.0
pkgrel+=.nonprism2
pkgdesc="Unified contacts, tasks and calendar backend"
pkgdesc+=", without google, libgweather, and libgdata support"
url="https://wiki.gnome.org/Apps/Evolution"
arch=(x86_64)
arch+=(armv7h i686)
depends=(
  gtk3
  krb5
  libcanberra
  libgoa
  libgweather-4
  libical
  libphonenumber
  libsecret
  nss
  sqlite
  webkit2gtk-4.1
)
depends=(${depends[*]/libgweather-4/})
makedepends=(
  boost
  cmake
  git
  gobject-introspection
  gperf
  gtk-doc
  ninja
  vala
  webkitgtk-6.0
)
makedepends=( ${makedepends[*]/git/} )
checkdepends=(db)
license=(GPL)
source=(https://gitlab.gnome.org/GNOME/${pkgname}/-/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz)
sha256sums=('8859b3f25dffbef13fb0117b0c6812ad54805bd168933a6ce5c4aedb5e740cfc')
[[ "${CARCH}" == i686 ]] && sha256sums=('34de49ab682b9c9d176b842476a6682bf0ca1bd23e992239bfd2d14cc3cf86e2')


## dependency tweaks ##

case "${CARCH}" in
i686)
  #depends+=( glib2=2.74.0 ) # Package dependency requirement 'glib-2.0 >= 2.74.0' could not be satisfied. If you want to disable GObject introspection, please use -DENABLE_INTROSPECTION=OFF

  makedepends=( ${makedepends[*]/webkitgtk-6.0/webkit2gtk-4.1} webkit2gtk-5.0 ) # arch32 does not have 'webkitgtk-6.0'

  # i686 FTBS (python is inconsistent): ModuleNotFoundError: No module named 'giscanner._giscanner'
  ;;
esac


prepare() {
  cd ${pkgname}-${pkgver}
}

build() {
  local cmake_options=(
    -DCMAKE_INSTALL_PREFIX=/usr
    -DLIBEXEC_INSTALL_DIR=/usr/lib
    -DSYSCONF_INSTALL_DIR=/etc
    -DENABLE_GTK_DOC=ON
    -DENABLE_INTROSPECTION=ON
    -DENABLE_VALA_BINDINGS=ON
    -DWITH_GWEATHER4=ON
    -DWITH_LIBDB=OFF
    -DWITH_PHONENUMBER=ON
  )
  cmake_options+=(-Wno-dev) # suppress noise
  local privacy_options=( -DENABLE_GOOGLE=OFF                 \
                          -DENABLE_GOOGLE_AUTH=OFF            \
                          -DENABLE_UOA=OFF                    \
                          -DHAVE_LIBGDATA=OFF                 \
                          -DWITH_GWEATHER4=OFF                \
                          -DENABLE_WEATHER=OFF                \
                          -DWITH_GOOGLE_CLIENT_ID='NONE'      \
                          -DWITH_GOOGLE_CLIENT_SECRET='NONE'  \
                          -DWITH_OUTLOOK_CLIENT_ID='NONE'     \
                          -DWITH_OUTLOOK_CLIENT_SECRET='NONE' \
                          -DWITH_YAHOO_CLIENT_ID='NONE'       \
                          -DWITH_YAHOO_CLIENT_SECRET='NONE'   )
  [[ "${CARCH}" == i686 ]] && privacy_options+=( -DWITH_PHONENUMBER=OFF ) # libphonenumber cannot be used.  Use -DWITH_PHONENUMBER=PATH to specify the library prefix, or -DWITH_PHONENUMBER=OFF to disable it.
  cmake -S ${pkgname}-${pkgver} -B build -G Ninja "${cmake_options[@]}" ${privacy_options[*]}

  cd build
  ninja # arch recipe builds in package()
}

check() {
  local exclude_tests=()
  # tests likely to fail
  [[ "${CARCH}" != armv7h ]] ||
  exclude_tests=( test-book-client-e164-param      \
                  test-book-client-view-operations )

  cd build
  ctest --output-on-failure --repeat until-pass:42 ${exclude_tests[*]/#/-E }
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_evolution-data-server() {
  provides=(
    libcamel-1.2.so
    libebackend-1.2.so
    libebook-1.2.so
    libebook-contacts-1.2.so
    libecal-2.0.so
    libedata-book-1.2.so
    libedata-cal-2.0.so
    libedataserver-1.2.so
    libedataserverui-1.2.so
  )
  depends+=(libicui18n.so libicuuc.so)

  DESTDIR="$pkgdir" cmake --install build

  cd $pkgdir

  _pick ui4 usr/include/evolution-data-server/libedataserverui4
  _pick ui4 usr/lib/libedataserverui4-1.0.so*
  _pick ui4 usr/lib/pkgconfig/libedataserverui4-1.0.pc
  _pick ui4 usr/lib/girepository-1.0/EDataServerUI4-1.0.typelib
  _pick ui4 usr/share/gir-1.0/EDataServerUI4-1.0.gir
  _pick ui4 usr/share/vala/vapi/libedataserverui4-1.0.*

  _pick docs usr/share/gtk-doc
}

package_libedataserverui4() {
  pkgdesc+=" - GTK 4 UI library"
  depends=(
    "evolution-data-server=$pkgver-$pkgrel"
    libgtk-4.so
    libwebkitgtk-6.0.so
  )
  provides=(libedataserverui4-1.0.so)

  mv ui4/* "$pkgdir"
}

package_evolution-data-server-docs() {
  pkgdesc+=" - documentation"
  depends=()

  mv docs/* "$pkgdir"
}

