SUMMARY="Open Source Computer Vision Library"
DESCRIPTION="A collection of algorithms and sample code for various \
computer vision problems."
HOMEPAGE="https://opencv.org/"
COPYRIGHT="2000-2016, Intel Corporation
	2009-2011, Willow Garage Inc.
	2009-2016, NVIDIA Corporation
	2010-2013, Advanced Micro Devices, Inc.
	2015-2016, OpenCV Foundation
	2015-2016, Itseez Inc."
LICENSE="BSD (3-clause)"
REVISION="13"
SOURCE_URI="https://github.com/opencv/opencv/archive/$portVersion.tar.gz"
SOURCE_FILENAME="opencv-$portVersion.tar.gz"
CHECKSUM_SHA256="4eef85759d5450b183459ff216b4c0fa43e87a4f6aa92c8af649f89336f002ec"
SOURCE_DIR="opencv-$portVersion"

ARCHITECTURES="all !x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
fi

portVersionCompat="$portVersion compat >= ${portVersion%%.*}"
libVersion="$portVersion"
libVersionCompat="$libVersion compat >= ${libVersion%.*}"

pythonVersion="3.7"

PROVIDES="
	opencv3$secondaryArchSuffix = $portVersionCompat
	lib:libopencv_calib3d$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_core$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_dnn$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_features2d$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_flann$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_highgui$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_imgcodecs$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_imgproc$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_ml$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_objdetect$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_photo$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_shape$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_stitching$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_superres$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_video$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_videoio$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_videostab$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libgdal$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libGLU$secondaryArchSuffix
	lib:libgphoto2$secondaryArchSuffix
	lib:libgstapp_1.0$secondaryArchSuffix
	lib:libgstreamer_1.0$secondaryArchSuffix
	lib:libgstvideo_1.0$secondaryArchSuffix
	lib:libhalf_2_4$secondaryArchSuffix
	lib:libharfbuzz$secondaryArchSuffix
	lib:libhdf5$secondaryArchSuffix
	lib:libiex_2_4$secondaryArchSuffix
	lib:libilmimf_2_4$secondaryArchSuffix
	lib:libilmthread_2_4$secondaryArchSuffix
	lib:libimath_2_4$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libjasper$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:liblapack$secondaryArchSuffix
	lib:libopenblas$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libQt5Concurrent$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Test$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libswscale$secondaryArchSuffix
	lib:libswresample$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libwebp$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_tools="
	opencv3${secondaryArchSuffix}_tools = $portVersionCompat
	cmd:opencv_annotation$commandSuffix = $portVersion
	cmd:opencv_createsamples$commandSuffix = $portVersion
	cmd:opencv_interactive_calibration$commandSuffix = $portVersion
	cmd:opencv_traincascade$commandSuffix = $portVersion
	cmd:opencv_version$commandSuffix = $portVersion
	cmd:opencv_visualisation$commandSuffix = $portVersion
	cmd:opencv_waldboost_detector$commandSuffix = $portVersion
	"
REQUIRES_tools="
	haiku$secondaryArchSuffix
	opencv3$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:cblas$secondaryArchSuffix
	devel:eigen$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
#	devel:libcaffe$secondaryArchSuffix
	devel:libexif$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libgdal$secondaryArchSuffix >= 26
#	devel:libgflags$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libglib_2.0$secondaryArchSuffix
	devel:libglog$secondaryArchSuffix
	devel:libGLU$secondaryArchSuffix
	devel:libgphoto2$secondaryArchSuffix
	devel:libgstapp_1.0$secondaryArchSuffix
	devel:libgstreamer_1.0$secondaryArchSuffix
	devel:libgstvideo_1.0$secondaryArchSuffix
	devel:libharfbuzz$secondaryArchSuffix
	devel:libhdf5$secondaryArchSuffix
	devel:libilmimf_2_4$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libjasper$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:liblapack$secondaryArchSuffix
	devel:libopenblas$secondaryArchSuffix
	devel:libopencl_headers$secondaryArchSuffix
	devel:liborc_0.4$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
#	devel:libprotobuf$secondaryArchSuffix
	devel:libQt5Concurrent$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5Gui$secondaryArchSuffix
	devel:libQt5Test$secondaryArchSuffix
	devel:libQt5Widgets$secondaryArchSuffix
	devel:libswscale$secondaryArchSuffix
	devel:libswresample$secondaryArchSuffix
#	devel:libtesseract$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix >= 5
	devel:libwebp$secondaryArchSuffix >= 7
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
#	cmd:ant
	cmd:cmake
	cmd:doxygen
#	cmd:f2py2
	cmd:f2py${pythonVersion}
	cmd:gcc$secondaryArchSuffix
#	cmd:javac
#	cmd:jni
	cmd:ld$secondaryArchSuffix
	cmd:make
#	cmd:matlab # maybe later
	cmd:pkg_config$secondaryArchSuffix
#	cmd:python2
	cmd:python${pythonVersion}
	"

defineDebugInfoPackage opencv3$secondaryArchSuffix \
	$libDir/libopencv_calib3d.so.$libVersion \
	$libDir/libopencv_core.so.$libVersion \
	$libDir/libopencv_dnn.so.$libVersion \
	$libDir/libopencv_features2d.so.$libVersion \
	$libDir/libopencv_flann.so.$libVersion \
	$libDir/libopencv_highgui.so.$libVersion \
	$libDir/libopencv_imgcodecs.so.$libVersion \
	$libDir/libopencv_imgproc.so.$libVersion \
	$libDir/libopencv_ml.so.$libVersion \
	$libDir/libopencv_objdetect.so.$libVersion \
	$libDir/libopencv_photo.so.$libVersion \
	$libDir/libopencv_shape.so.$libVersion \
	$libDir/libopencv_stitching.so.$libVersion \
	$libDir/libopencv_superres.so.$libVersion \
	$libDir/libopencv_video.so.$libVersion \
	$libDir/libopencv_videoio.so.$libVersion \
	$libDir/libopencv_videostab.so.$libVersion

BUILD()
{
	mkdir -p build_haiku && cd build_haiku

	local ARCH_SUBDIR=
	if [ -n "$secondaryArchSuffix" ]; then
		ARCH_SUBDIR="$secondaryArchSuffix/"
	fi

	cmake .. \
		-DWITH_GDAL=ON \
		-DWITH_IPP=OFF \
		-DWITH_OPENGL=OFF \
		-DWITH_QT=ON \
		-DWITH_EIGEN=ON \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_SKIP_RPATH=ON \
		-DENABLE_PIC=OFF \
		-DOPENCV_ENABLE_NONFREE=ON \
		-DCMAKE_INSTALL_PREFIX:PATH="$prefix" \
		-DOPENCV_OTHER_INSTALL_PATH:PATH="$dataDir" \
		-DOpenBLAS_INCLUDE_DIR="$portPackageLinksDir/openblas${secondaryArchSuffix}_devel/develop/headers/$ARCH_SUBDIR" \
		-DOpenBLAS_LIB="$portPackageLinksDir/openblas${secondaryArchSuffix}_devel/develop/lib/$ARCH_SUBDIR"

	export DISABLE_ASLR=1

	make $jobArgs
}

INSTALL()
{
	cd build_haiku

	make install

	if [ -n "$secondaryArchSuffix" ]; then
		mkdir -p $libDir
		mv $prefix/lib/*.so* $libDir
		mv $prefix/lib/python* $libDir
	fi

	prepareInstalledDevelLibs libopencv_calib3d \
		libopencv_core \
		libopencv_dnn \
		libopencv_features2d \
		libopencv_flann \
		libopencv_highgui \
		libopencv_imgcodecs \
		libopencv_imgproc \
		libopencv_ml \
		libopencv_objdetect \
		libopencv_photo \
		libopencv_shape \
		libopencv_stitching \
		libopencv_superres \
		libopencv_video \
		libopencv_videoio \
		libopencv_videostab

	rm -rf $prefix/{share,include}
	rm -rf $dataDir/OpenCV
	rm -rf $prefix/lib/pkgconfig
	rm -rf $libDir/cmake $developDir

	# tools package
	packageEntries tools \
		$prefix/bin
}
