# Template file for 'gnuplot'
pkgname=gnuplot
version=6.0.2
revision=1
configure_args="--with-readline=builtin
 --with-gpic --with-metapost --with-metafont"
hostmakedepends="pkg-config lua52 zlib-devel libX11-devel
 cairo-devel pango-devel"
makedepends="zlib-devel libX11-devel gd-devel lua52-devel
 cairo-devel pango-devel"
depends="gnuplot-common>=${version}_${revision}"
short_desc="Command-line driven graphing utility"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="gnuplot"
homepage="http://www.gnuplot.info/"
distfiles="${SOURCEFORGE_SITE}/gnuplot/gnuplot/${version}/gnuplot-${version}.tar.gz"
checksum=f68a3b0bbb7bbbb437649674106d94522c00bf2f285cce0c19c3180b1ee7e738

subpackages="gnuplot-common"
if [ -z "$CROSS_BUILD" ]; then
	hostmakedepends+=" qt6-base qt6-tools"
	makedepends+=" wxWidgets-gtk3-devel
	 qt6-base-devel qt6-svg-devel qt6-qt5compat-devel"
	subpackages+=" gnuplot-wx gnuplot-qt"
fi

alternatives="gnuplot:gnuplot:/usr/bin/gnuplot-x11"

CXXFLAGS="-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"

pre_configure() {
	if [ "$CROSS_BUILD" ]; then
		sed -i 's|^\(SUBDIRS =.*\)demo |\1|' Makefile.in
		sed -i 's|^\(SUBDIRS =.*\)docs |\1|' Makefile.in
	fi

	mkdir -p x11
	mv * x11 || true
	if [ -z "$CROSS_BUILD" ]; then
		cp -a x11 wx
		cp -a x11 qt
	fi
}
do_configure() {
	if [ -z "$CROSS_BUILD" ]; then
		cd ${wrksrc}/x11
		./configure ${configure_args} --disable-wxwidgets --without-qt
		cd ${wrksrc}/wx
		./configure ${configure_args} --without-qt ac_cv_path_WX_CONFIG=wx-config-gtk3 LIBS="-lX11"
		cd ${wrksrc}/qt
		./configure ${configure_args} --with-qt --disable-wxwidgets
	else
		cd ${wrksrc}/x11
		./configure ${configure_args} --disable-wxwidgets --without-qt
	fi
}
do_build() {
	if [ -z "$CROSS_BUILD" ]; then
		make -C wx ${makejobs}
		make -C qt ${makejobs}
	fi
	make -C x11 ${makejobs}
}
do_install() {
	# install x11 and rename binary
	make -C x11 DESTDIR=${DESTDIR} install
	mv ${DESTDIR}/usr/bin/gnuplot{,-x11}
	if [ -z "$CROSS_BUILD" ]; then
		# install qt and rename binary
		make -C qt DESTDIR=${DESTDIR} install
		mv ${DESTDIR}/usr/bin/gnuplot{,-qt}
		# install wx and rename binary
		make -C wx DESTDIR=${DESTDIR} install
		mv ${DESTDIR}/usr/bin/gnuplot{,-wx}
	else
		find -name '*.o' -delete
		make -C x11/docs DESTDIR=${DESTDIR} \
			CC="$BUILD_CC" CFLAGS="$BUILD_CFLAGS" \
			LDFLAGS="$BUILD_LDFLAGS" install
	fi
	vlicense x11/Copyright LICENSE
}

gnuplot-common_package() {
	short_desc+=" - common files"
	pkg_install() {
		vmove usr/share/gnuplot/${version%.*}/PostScript
		vmove usr/share/gnuplot/${version%.*}/app-defaults
		vmove usr/share/gnuplot/${version%.*}/js
		vmove usr/share/gnuplot/${version%.*}/lua
		vmove usr/share/gnuplot/${version%.*}/*.gp
		vmove usr/share/gnuplot/${version%.*}/gnuplot.gih
		vmove usr/share/gnuplot/${version%.*}/gnuplotrc
		vmove usr/share/licenses/gnuplot
		vmove usr/share/man/man1/gnuplot.1
	}
}
gnuplot-wx_package() {
	depends="gnuplot-common>=${version}_${revision}"
	short_desc+=" - wxWidgets frontend"
	alternatives="gnuplot:gnuplot:/usr/bin/gnuplot-wx"
	pkg_install() {
		vmove usr/bin/gnuplot-wx
	}
}
gnuplot-qt_package() {
	depends="gnuplot-common>=${version}_${revision}"
	short_desc+=" - Qt frontend"
	alternatives="gnuplot:gnuplot:/usr/bin/gnuplot-qt"
	pkg_install() {
		vmove usr/bin/gnuplot-qt
		vmove usr/libexec/gnuplot/${version%.*}/gnuplot_qt
		vmove usr/share/gnuplot/${version%.*}/qt
	}
}
