# Maintainer (arch): Jaroslav Lichtblau <svetlemodry@archlinux.org>
# Contributor: Vesa Kaihlavirta <vegai@iki.fi>
# Maintainer: bill-auger <bill-auger@programmer.net>
# Contributer: Wael Karram <wael@waelk.tech>


# parabola changes and rationale:
# - repalce qt5-webengine with qt5-webkit (for URL previews)
# - set duckduckgo as default search engine
# - pin to strongly-coupled dependencies


pkgbase=quassel
pkgname=('quassel-core'
         'quassel-client'
         'quassel-client-qt'
         'quassel-monolithic'
         'quassel-monolithic-qt'
         'quassel-common')
pkgver=0.14.0
pkgrel=3
pkgrel+=.parabola1
pkgdesc="Next-generation distributed IRC client"
arch=('x86_64')
arch+=('armv7h' 'i686')
url="https://quassel-irc.org/"
license=('GPL')
makedepends=('qt5-base' 'qt5-tools' 'qt5-webengine' 'qca-qt5'
             'qt5-multimedia' 'knotifyconfig5' 'sonnet5' 'libldap' 'cmake'
             'extra-cmake-modules' 'python' 'hicolor-icon-theme' 'boost')
makedepends=( ${makedepends[*]/qt5-webengine/qt5-webkit} )
source=(https://quassel-irc.org/pub/$pkgbase-$pkgver.tar.bz2
        $pkgbase.service
        $pkgbase.sysusers
        $pkgbase.tmpfiles
        $pkgbase.conf)
source+=(9000-search-duck.patch)
sha256sums=('bbd46ca429c1518480904c9e2b0ab3a0412fe197f3ce4879974e06ed3a0093a3'
            '5dbe20290f3361b9b7a74a52905137e76b656976febf2d31082a2276f9dcde7f'
            '3c72441a99e2668c6a8a751fa07beeb44f937576c8a1b5f615e4a55f841d93d9'
            '2afd4340c7713f6533e5d175a86b28fd118ecd907776c2b10925d1a4fb31cdca'
            'f3031ea8217e01ba42cea14606169e3e27affa5918968ffd5a03c21ae92fe2b8')
sha256sums+=('e1dcb63b3b4e85b3c70d2a1788dd20271608dfe715bf23487f7b492989b2b3ec')


prepare() {
  cd $pkgbase-$pkgver

  # parabola patches
  echo "applying 9000-search-duck.patch"
  patch -Np1 -i ../9000-search-duck.patch
}

_build() (
  cmake -H$pkgbase-$pkgver -Bbuild-$1 \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DENABLE_SHARED=OFF \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -Wno-suggest-override" \
    "${@:2}" \
    -Wno-dev
  cmake --build build-$1
)

_install() {
  DESTDIR="${pkgdir}" cmake --build build-$1 --target install
}

build() {
  _build core \
    -DWANT_QTCLIENT=OFF \
    -DWANT_MONO=OFF \
    -DWITH_BUNDLED_ICONS=OFF \
    -DWITH_WEBKIT=OFF \
    -DWITH_WEBENGINE=OFF

  _build monolithic \
    -DWANT_CORE=OFF \
    -DWANT_QTCLIENT=OFF \
    -DWITH_WEBKIT=ON \
    -DWITH_WEBENGINE=OFF \
    -DWITH_KDE=ON

  _build monolithic-qt \
    -DWANT_CORE=OFF \
    -DWANT_QTCLIENT=OFF \
    -DWITH_WEBKIT=OFF \
    -DWITH_WEBENGINE=OFF \
    -DCMAKE_DISABLE_FIND_PACKAGE_dbusmenu-qt5=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Qt5Multimedia=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Phonon4Qt5=ON

  _build client \
    -DWANT_CORE=OFF \
    -DWANT_MONO=OFF \
    -DWITH_WEBKIT=ON \
    -DWITH_WEBENGINE=OFF \
    -DWITH_KDE=ON

  _build client-qt \
    -DWANT_CORE=OFF \
    -DWANT_MONO=OFF \
    -DWITH_WEBKIT=OFF \
    -DWITH_WEBENGINE=OFF \
    -DCMAKE_DISABLE_FIND_PACKAGE_dbusmenu-qt5=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Qt5Multimedia=ON \
    -DCMAKE_DISABLE_FIND_PACKAGE_Phonon4Qt5=ON
}

package_quassel-core() {
pkgdesc="Next-generation distributed IRC client - core only"
depends=('icu' 'qca' 'libldap')
depends+=(libicuuc.so libicudata.so) # via 'qt5-base' -> libQt5Core.so.N -> libicuuc.so.M -> libicudata.so.M
optdepends=('postgresql: PostgreSQL database support')
backup=(etc/conf.d/quassel)

  _install core

  # Remove unneeded pixmap icons
  rm -r "${pkgdir}"/usr/share

  install -Dm644 $pkgbase.sysusers \
    "${pkgdir}"/usr/lib/sysusers.d/$pkgbase.conf
  install -Dm644 $pkgbase.tmpfiles \
    "${pkgdir}"/usr/lib/tmpfiles.d/$pkgbase.conf
  install -Dm644 $pkgbase.service \
    "${pkgdir}"/usr/lib/systemd/system/$pkgbase.service
  install -Dm644 $pkgbase.conf \
    "${pkgdir}"/etc/conf.d/$pkgbase
}

package_quassel-client() {
pkgdesc="Next-generation distributed IRC client - client only, KDE version"
depends=('quassel-common' 'qt5-base' 'qt5-webengine' 'qt5-multimedia' 'knotifyconfig5')
depends=( ${depends[*]/qt5-webengine/qt5-webkit} )
optdepends=('perl: for builtin /exec scripts')
conflicts=('quassel-client-qt')

  _install client
  install -Dm644 $pkgbase-$pkgver/data/quasselclient.appdata.xml "$pkgdir/usr/share/metainfo/quasselclient.appdata.xml"

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,knotifications5,quassel}
}

package_quassel-client-qt() {
pkgdesc="Next-generation distributed IRC client - client only, Qt version with reduced deps"
depends=('quassel-common' 'qt5-base' 'qt5-svg' 'sonnet5')
optdepends=('perl: for builtin /exec scripts')
conflicts=('quassel-client')
replaces=('quassel-client-small' 'quassel-remote')

  _install client-qt

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,quassel}

  # Ignore package by AppStream to avoid duplicated IDs
  echo 'X-AppStream-Ignore=true' >> "$pkgdir/usr/share/applications/quasselclient.desktop"
}

package_quassel-monolithic() {
pkgdesc="Next-generation distributed IRC client - monolithic, KDE version"
depends=('quassel-common' 'qt5-base' 'qt5-webengine' 'qt5-multimedia' 'qca-qt5'
         'knotifyconfig5' 'libldap')
depends=( ${depends[*]/qt5-webengine/qt5-webkit} )
optdepends=('perl: for builtin /exec scripts'
            'postgresql: PostgreSQL database support')
conflicts=('quassel-monolithic-qt')

  _install monolithic
  install -Dm644 $pkgbase-$pkgver/data/quassel.appdata.xml "$pkgdir/usr/share/metainfo/quassel.appdata.xml"

  # Split quassel-common
  mkdir "$srcdir/quassel-common"
  mv "$pkgdir"/usr/share/{icons,knotifications5,quassel} \
     "$srcdir/quassel-common/"
}

package_quassel-monolithic-qt() {
pkgdesc="Next-generation distributed IRC client - monolithic, Qt version with reduced deps"
depends=('quassel-common' 'qca' 'qt5-base' 'qt5-svg' 'sonnet5')
optdepends=('perl: for builtin /exec scripts'
            'postgresql: PostgreSQL database support')
conflicts=('quassel-monolithic')

  _install monolithic-qt

  # Split quassel-common
  rm -r "$pkgdir"/usr/share/{icons,quassel}

  # Ignore package by AppStream to avoid duplicated IDs
  echo 'X-AppStream-Ignore=true' >> "$pkgdir/usr/share/applications/quassel.desktop"
}

package_quassel-common() {
  pkgdesc="Common files for Quassel"
  depends=('hicolor-icon-theme')

  install -dm755 "$pkgdir/usr/share"
  mv quassel-common/* "$pkgdir/usr/share"
}
