# Template file for 'plplot'
pkgname=plplot
version=5.15.0
revision=7
build_style=cmake
build_helper="qemu numpy"
configure_args="
 -DPL_FREETYPE_FONT_PATH=/usr/share/fonts/TTF
 -DCMAKE_NATIVE_BINARY_DIR=/usr -DPREBUILT_DOC=ON -DLUA_DIR=/usr/lib/lua/5.3
 -DCMAKE_C_FLAGS=-I${XBPS_CROSS_BASE}/${py3_sitelib}/numpy/_core/include
"
hostmakedepends="pkg-config gcc-fortran swig freefont-ttf
 lua53 python3-numpy python3-setuptools plplot-devel"
makedepends="pango-devel cairo-devel freetype-devel libqhull-devel
 libltdl-devel Haru-devel tk-devel shapelib-devel
 lua53-devel python3-devel python3-numpy"
short_desc="Scientific graphics plotting with math symbols and human languages"
maintainer="Piotr Wójcik <chocimier@tlen.pl>"
license="LGPL-3.0-or-later"
homepage="http://plplot.sourceforge.net"
changelog="https://sourceforge.net/p/plplot/plplot/ci/master/tree/README.cumulated_release?format=raw"
distfiles="${SOURCEFORGE_SITE}/plplot/plplot/${version}%20Source/plplot-${version}.tar.gz"
checksum=b92de4d8f626a9b20c84fc94f4f6a9976edd76e33fb1eae44f6804bdcc628c7b
subpackages="plplot-fortran plplot-lua plplot-python3 plplot-doc plplot-devel libplplot"
python_version=3

build_options="ada gen_tools"
# Requires exporting BUILD_ADA=gcc et al. in setup_pkg
desc_option_ada="Enable Ada bindings"
desc_option_gen_tools="Build files for host required during cross build"

if ! [ "$CROSS_BUILD" ]; then
	build_options_default="gen_tools"
fi

if [ "$build_option_ada" ]; then
	subpackages="plplot-ada $subpackages"
else
	configure_args+=" -DENABLE_ada=OFF"
fi

post_patch() {
	vsed -i cmake/modules/instdirs.cmake -e '/General install/aFORCE'
	# detection requires X server
	vsed -i cmake/modules/tcl-related.cmake -e '/if(NOT PLPLOT_TK_VERSION)/ i set(PLPLOT_TK_VERSION ${PLPLOT_TCL_VERSION} CACHE STRING "Tk version found by wish")'
}

post_configure() {
	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		vsed -i build/plplot_config.h \
			-e '/PTHREAD_MUTEX_RECURSIVE_NP/s/_NP//'
	fi
}

pre_build() {
	if ! [ "$build_option_gen_tools" ]; then
		ln -s /usr/lib/qsastime/deltaT-gen build/lib/qsastime
		ln -s /usr/lib/qsastime/tai-utc-gen build/lib/qsastime
		ln -s /usr/libexec/plhershey-unicode-gen build/include
	fi
}

post_install() {
	if [ "$build_option_gen_tools" ]; then
		vinstall build/lib/qsastime/deltaT-gen.cmake 644 usr/lib/qsastime
		vinstall build/lib/qsastime/deltaT-gen 755 usr/lib/qsastime
		vinstall build/lib/qsastime/tai-utc-gen.cmake 644 usr/lib/qsastime
		vinstall build/lib/qsastime/tai-utc-gen 755 usr/lib/qsastime
		vinstall build/include/ImportExecutables.cmake 644 usr/include
		vinstall build/include/plhershey-unicode-gen 755 usr/libexec
	fi
}

plplot-ada_package() {
	short_desc+=" - ada bindings"
	pkg_install() {
		vmove usr/lib/ada
		vmove "usr/lib/libplplotada*"
		vmove usr/share/ada
	}
}

plplot-fortran_package() {
	short_desc+=" - fortran bindings"
	pkg_install() {
		vmove usr/lib/fortran
		vmove "usr/lib/libplplotfortran*"
	}
}

plplot-lua_package() {
	short_desc+=" - lua bindings"
	pkg_install() {
		vmove usr/lib/lua
	}
}

plplot-python3_package() {
	depends="python3-numpy"
	short_desc+=" - python bindings"
	pkg_install() {
		vmove "$py3_lib"
	}
}

libplplot_package() {
	short_desc+=" - runtime library"
	pkg_install() {
		vmove "usr/lib/*.so.*"
		vmove usr/lib/plplot${version}/drivers
		vmove usr/share/plplot${version}
	}
}

plplot-devel_package() {
	depends="libplplot>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/cmake
		vmove usr/lib/pkgconfig
		vmove "usr/lib/*.so"
		vmove "usr/lib/*.a"
		if [ "$build_option_gen_tools" ]; then
			vmove usr/lib/qsastime
			vmove usr/libexec
		fi
	}
}

plplot-doc_package() {
	short_desc+=" - documentation"
	pkg_install() {
		vmove usr/share/doc
		vmove usr/share/info
		vmove usr/share/man/man3
		vmove usr/share/plplot${version}/examples
	}
}
