# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Eric Bélanger <eric@archlinux.org>

pkgbase=wxwidgets
pkgname=(wxwidgets-common
         wxwidgets-gtk3
         wxwidgets-qt5)
pkgver=3.2.5
pkgrel=1
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url='https://wxwidgets.org'
license=(custom:wxWindows)
makedepends=(cmake
             git
             glu
             gst-plugins-base
             libmspack
             libnotify
             nanosvg
             qt5-base
             sdl2
             webkit2gtk-4.1)
source=(git+https://github.com/wxWidgets/wxWidgets#tag=v$pkgver)
sha256sums=('d0f477627d87f67e5085c60d813df726979be248db3699a76b7c4d6987e226b0')

prepare() {
  cd wxWidgets
  
  git cherry-pick -n ed510012 # Fix undefined symbols in Qt build
  git cherry-pick -n 8ea22b5e # Avoid crash with GTK3 if console program is using a GUI wxApp
}

build() {
  cmake -B build-gtk3 -S wxWidgets \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DwxBUILD_TOOLKIT=gtk3 \
    -DwxUSE_OPENGL=ON \
    -DwxUSE_REGEX=sys\
    -DwxUSE_ZLIB=sys \
    -DwxUSE_EXPAT=sys \
    -DwxUSE_LIBJPEG=sys \
    -DwxUSE_LIBPNG=sys \
    -DwxUSE_LIBTIFF=sys \
    -DwxUSE_LIBLZMA=sys \
    -DwxUSE_NANOSVG=sys \
    -DwxUSE_LIBMSPACK=ON \
    -DwxUSE_PRIVATE_FONTS=ON \
    -DwxUSE_GTKPRINT=ON
  cmake --build build-gtk3

  cmake -B build-qt5 -S wxWidgets \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DwxBUILD_TOOLKIT=qt \
    -DwxUSE_OPENGL=ON \
    -DwxUSE_REGEX=sys \
    -DwxUSE_ZLIB=sys \
    -DwxUSE_EXPAT=sys \
    -DwxUSE_LIBJPEG=sys \
    -DwxUSE_LIBPNG=sys \
    -DwxUSE_LIBTIFF=sys \
    -DwxUSE_LIBLZMA=sys \
    -DwxUSE_NANOSVG=sys \
    -DwxUSE_LIBMSPACK=ON \
    -DwxUSE_PRIVATE_FONTS=ON
  cmake --build build-qt5

# Run configure to generate the Makefile, cmake doesn't install translations
  cd wxWidgets
  ./configure --prefix=/usr --disable-tests
  make -C locale allmo
}

package_wxwidgets-common() {
  pkgdesc='Common libraries and headers for wxwidgets'
  depends=(curl
           gcc-libs
           glib2
           glibc
           expat
           libsecret
           pcre2
           xz
           zlib)
  conflicts=(wxgtk-common)
  replaces=(wxgtk-common)

  DESTDIR="$pkgdir" cmake --install build-gtk3
  rm -r "$pkgdir"/usr/{bin/wx-config,lib/{cmake,wx,libwx_gtk*}}
  install -Dm644 wxWidgets/wxwin.m4 -t "$pkgdir"/usr/share/aclocal
# Install translations
  make DESTDIR="$pkgdir" -C wxWidgets locale_install  

  install -Dm644 wxWidgets/docs/licence.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

package_wxwidgets-gtk3() {
  pkgdesc='GTK+3 implementation of wxWidgets API for GUI'
  depends=(bash
           cairo
           fontconfig
           gcc-libs
           gdk-pixbuf2
           glib2
           glibc
           gst-plugins-bad-libs
           gstreamer
           gtk3
           libglvnd
           libjpeg-turbo
           libnotify
           libmspack
           libpng
           libsm # cmake target
           libtiff
           libx11
           libxkbcommon
           libxtst
           pango
           sdl2
           wayland
           wxwidgets-common)
  optdepends=('webkit2gtk-4.1: for webview support')
  conflicts=(wxgtk3)
  provides=(wxgtk3
            wxwidgets)
  replaces=(wxgtk3)

  DESTDIR="$pkgdir" cmake --install build-gtk3
  rm -r "$pkgdir"/usr/{include,lib/libwx_base*,bin/wxrc*}
  
  install -Dm644 wxWidgets/docs/licence.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

package_wxwidgets-qt5() {
  pkgdesc='Qt5 implementation of wxWidgets API for GUI'
  depends=(bash
           gcc-libs
           glibc
           libglvnd
           libjpeg-turbo
           libmspack
           libpng
           libtiff
           qt5-base
           sdl2
           wxwidgets-common)
  provides=(wxwidgets)

  DESTDIR="$pkgdir" cmake --install build-qt5
  rm -r "$pkgdir"/usr/{include,lib/libwx_base*,bin/wxrc*}
  mv "$pkgdir"/usr/bin/wx-config{,-qt} # Conflicts with wx-gtk3
# Rename cmake files for coinstallability
  mv "$pkgdir"/usr/lib/cmake/wxWidgets{,Qt}
  for _f in "$pkgdir"/usr/lib/cmake/wxWidgetsQt/*; do
    mv $_f $(dirname $_f)/$(basename $_f | sed -e 's/wxWidgets/wxWidgetsQt/')
  done

  install -Dm644 wxWidgets/docs/licence.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
