#Packager: Dimitris Tzemos <dijemos@gmail.com>

pkgname=wxGTK3
pkgver=3.2.2.1
pkgrel=1dj
source=("https://github.com/wxWidgets/wxWidgets/archive/v3.2.2.1/wxWidgets-$pkgver.tar.gz")
url="http://wxwidgets.org"
docs=("docs/*")

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"$pkgname (wxWidgets toolkit for GTK)"
"wxWidgets lets developers create applications for Win32, Mac OS X,"
"GTK+, X11, Motif, WinCE, and more using one codebase. It can be used"
"from languages such as C++, Python, Perl, and C#/.NET. Unlike other"
"cross-platform toolkits, wxWidgets applications look and feel native."
"This is because wxWidgets uses the platform's own native controls"
"rather than emulating them. It's also extensive, free, open-source,"
"and mature."
""
"This package is built with GTK+2.0 and compatible with wxPython."
)


build() {
	
# Check for STL
if [ "${STL:-no}" = "yes" ]; then
  stl="--enable-stl"
else
  stl=""
fi

# Check for STL
if [ "${STL:-no}" = "yes" ]; then
  stl="ON"
else
  stl="OFF"
fi

# Check for WebKit
webkit=`pkg-config --exists webkit2gtk-4.0`
if [ "$?" -eq 0 ]; then
  wk="ON"
else
  wk="OFF"
fi

cd $SRC/wxWidgets-$pkgver

sed -i "s|prefix}/lib|prefix}/lib$LIBDIRSUFFIX|" build/cmake/config.cmake

if [ -z "$INTERFACE" ]; then
cmake -B build-gtk3 \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -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_LIBMSPACK=ON \
    -DwxUSE_NANOSVG=OFF \
    -DwxUSE_STL=$stl \
    -DwxUSE_WEBVIEW_WEBKIT=$wk \
    -DwxUSE_PRIVATE_FONTS=ON \
    -DCMAKE_BUILD_TYPE=Release .
cmake --build build-gtk3
DESTDIR="$PKG" cmake --install build-gtk3 || return 1

elif [ "${INTERFACE:-gtk3}" = "qt" ]; then
cmake -B build-qt5 \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -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_LIBMSPACK=ON \
    -DwxUSE_NANOSVG=OFF \
    -DwxUSE_STL=$stl \
    -DwxUSE_WEBVIEW_WEBKIT=$wk \
    -DwxUSE_PRIVATE_FONTS=ON \
    -DCMAKE_BUILD_TYPE=Release .
cmake --build build-qt5
DESTDIR="$PKG" cmake --install build-qt5 || return 1
fi

#Move librarys to their correct location and fix pkgconfig
if [ "$LIBDIRSUFFIX" != "" ]; then
  mv $PKG/usr/lib $PKG/usr/lib${LIBDIRSUFFIX}
  sed -i "s/\/lib\//\/lib${LIBDIRSUFFIX}\//g" \
    $PKG/usr/lib${LIBDIRSUFFIX}/cmake/$pkgname/${pkgname}Config.cmake
  sed -i "s/\/lib\//\/lib${LIBDIRSUFFIX}\//g" \
    $PKG/usr/lib${LIBDIRSUFFIX}/cmake/$pkgname/${pkgname}Targets.cmake
  sed -i "s/\/lib\//\/lib${LIBDIRSUFFIX}\//g" \
    $PKG/usr/lib${LIBDIRSUFFIX}/cmake/$pkgname/${pkgname}Targets-release.cmake
fi

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

install -Dm644 wxwin.m4 -t $PKG/usr/share/aclocal

# wxWidgets 3.2 provides 2 wx-configs, but we will use GTK by default
gtk=gtk3-unicode-3.2
qt=qt-unicode-3.2

if [ "${INTERFACE:-gtk3}" = "qt" ]; then
  ln -sf ../lib${LIBDIRSUFFIX}/wx/config/$qt $PKG/usr/bin/wx-config
else
  ln -sf ../lib${LIBDIRSUFFIX}/wx/config/$gtk $PKG/usr/bin/wx-config
fi

mkdir -p $PKG/usr/doc/$pkgname-$pkgver

# 20220414 bkw: instead of installing the *source* to the doxygen docs,
# actually run doxygen and install the generated HTML docs.
# This is only done if the users asks for it.
# The "|| true" stuff is meant to keep errors in the documentation from
# killing the build. DOT_NUM_THREADS=1 may slow things down a bit on
# well-endowed hosts, but it prevents running out of memory on lesser ones.
# Don't try to generate the API docs if graphviz is missing.
if [ "${APIDOC:-no}" = "yes" ] && which dot &> /dev/null; then
  echo "=== APIDOC=yes and graphviz exists, building API docs"
  cd docs/doxygen
    echo 'DOT_NUM_THREADS = 1' >> Doxyfile
    GENERATE_LATEX=NO doxygen || true
    mv out/html $PKG/usr/doc/$pkgname-$pkgver/api || true
  cd -
else
  echo "=== not building API docs"
fi
rm -rf docs/doxygen

rm -f $PKG/usr/lib${LIBDIRSUFFIX}/*.la
}
