#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=wxwidgets
PKGVERSION=3.0.4
PKGREVISION=1
PKGLICENSE="GPL-2.0-only LGPL-2.0-only"
PKGSUMMARY="toolkit to write native-looking GUI applications"
PKGDOWNLOAD="https://github.com/wxWidgets/wxWidgets/releases/download/v$PKGVERSION/wxWidgets-$PKGVERSION.tar.bz2"
PKGOPTIONS="GTK JPEG LZMA OPENGL NOTIFICATIONS PNG QT TIFF XML XPM ZLIB"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${GTK:=yes}
: ${JPEG:=yes}
: ${LZMA:=yes}
: ${OPENGL:=yes}
: ${NOTIFICATIONS:=yes}
: ${PNG:=yes}
: ${QT:=no}
: ${TIFF:=yes}
: ${XML:=yes}           # Note: XML support through expat library.
: ${XPM:=yes}
: ${ZLIB:=yes}

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
	with_gtk="--with-gtk=3"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="libjpeg-turbo $PKGDEPENDS"
	with_jpeg="--with-libjpeg"
else
	with_jpeg="--without-libjpeg"
fi

if [ "$LZMA" = "yes" ]; then
	PKGDEPENDS="xz $PKGDEPENDS"
	with_lzma="--with-liblzma"
else
	with_lzma="--without-liblzma"
fi

if [ "$NOTIFICATIONS" = "yes" ]; then
	PKGDEPENDS="libnotify $PKGDEPENDS"
	with_notifications="--with-libnotify"
else
	with_notifications="--without-libnotify"
fi

if [ "$OPENGL" = "yes" ]; then
	PKGDEPENDS="mesa $PKGDEPENDS"
	with_opengl="--with-opengl"
else
	with_opengl="--without-opengl"
fi

if [ "$PNG" = "yes" ]; then
	PKGDEPENDS="libpng $PKGDEPENDS"
	with_png="--with-libpng"
else
	with_png="--without-libpng"
fi

if [ "$QT" = "yes" ]; then
	PKGDEPENDS="qtbase $PKGDEPENDS"
	with_qt="--with-qt"
fi

if [ "$TIFF" = "yes" ]; then
	PKGDEPENDS="libtiff $PKGDEPENDS"
	with_tiff="--with-libtiff"
else
	with_tiff="--without-libtiff"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="expat $PKGDEPENDS"
	with_xml="--with-expat"
else
	with_xml="--without-expat"
fi

if [ "$XPM" = "yes" ]; then
	PKGDEPENDS="libxpm $PKGDEPENDS"
	with_xpm="--with-libxpm"
else
	with_xpm="--without-libxpm"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
	with_zlib="--with-zlib"
else
	with_zlib="--without-zlib"
fi

build()
{
	rm -rf wxWidgets-$PKGVERSION
	tar xvf wxWidgets-$PKGVERSION.tar.bz2
	cd wxWidgets-$PKGVERSION

	# --disable-xlocale: does not build (strtol_l).
	# --enable-unicode: this should be default.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--with-cxx=17 \
		--disable-xlocale \
		--enable-unicode \
		$with_gtk \
		$with_jpeg \
		$with_lzma \
		$with_notifications \
		$with_opengl \
		$with_png \
		$with_qt \
		$with_tiff \
		$with_xml \
		$with_xpm \
		$with_zlib
	make
	make install DESTDIR=$DESTDIR

	cd ..
	rm -rf wxWidgets-$PKGVERSION
}
