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-2020, 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.
	2019-2020, Xperience AI"
LICENSE="BSD (3-clause)"
REVISION="5"
SOURCE_URI="https://github.com/opencv/opencv/archive/$portVersion.tar.gz"
SOURCE_FILENAME="opencv-$portVersion.tar.gz"
CHECKSUM_SHA256="e27fe5b168918ab60d58d7ace2bd82dd14a4d0bd1d3ae182952c2113f5637513"
SOURCE_URI_2="https://github.com/opencv/opencv_contrib/archive/$portVersion.tar.gz"
SOURCE_FILENAME_2="opencv_contrib-$portVersion.tar.gz"
CHECKSUM_SHA256_2="12c3b1ddd0b8c1a7da5b743590a288df0934e5cef243e036ca290c2e45e425f5"

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
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

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

PROVIDES="
	opencv$secondaryArchSuffix = $portVersionCompat
	lib:libopencv_alphamat$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_aruco$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_bgsegm$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_bioinspired$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_calib3d$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_ccalib$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_core$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_cvv$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_datasets$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_dnn_objdetect$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_dnn_superres$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_dnn$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_dpm$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_face$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_features2d$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_flann$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_freetype$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_fuzzy$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_gapi$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_hdf$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_hfs$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_highgui$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_img_hash$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_imgcodecs$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_imgproc$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_intensity_transform$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_line_descriptor$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_mcc$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_ml$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_objdetect$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_optflow$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_phase_unwrapping$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_photo$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_plot$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_quality$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_rapid$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_reg$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_rgbd$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_saliency$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_shape$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_stereo$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_stitching$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_structured_light$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_superres$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_surface_matching$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_text$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_tracking$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_video$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_videoio$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_videostab$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_xfeatures2d$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_ximgproc$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_xobjdetect$secondaryArchSuffix = $libVersionCompat
	lib:libopencv_xphoto$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
#	lib:libceres$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libgdal$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libglog$secondaryArchSuffix
	lib:libGLU$secondaryArchSuffix
	lib:libgphoto2$secondaryArchSuffix
	lib:libgstapp_1.0$secondaryArchSuffix
	lib:libgstreamer_1.0$secondaryArchSuffix
	lib:libgstvideo_1.0$secondaryArchSuffix
	lib:libharfbuzz$secondaryArchSuffix
	lib:libhdf5$secondaryArchSuffix
#	lib:libilmimf$secondaryArchSuffix #openexr
	lib:libintl$secondaryArchSuffix
	lib:libjasper$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:liblapack$secondaryArchSuffix
	lib:libopenblas$secondaryArchSuffix
	lib:libopenjp2$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libQt5Concurrent$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Test$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libswscale$secondaryArchSuffix
	lib:libtesseract$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libwebp$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	opencv${secondaryArchSuffix}_devel = $portVersionCompat
	devel:libopencv_alphamat$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_aruco$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_bgsegm$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_bioinspired$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_calib3d$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_ccalib$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_core$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_cvv$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_datasets$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_dnn_objdetect$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_dnn_superres$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_dnn$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_dpm$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_face$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_features2d$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_flann$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_freetype$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_fuzzy$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_gapi$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_hdf$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_hfs$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_highgui$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_img_hash$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_imgcodecs$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_imgproc$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_intensity_transform$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_line_descriptor$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_mcc$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_ml$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_objdetect$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_optflow$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_phase_unwrapping$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_photo$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_plot$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_quality$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_rapid$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_reg$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_rgbd$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_saliency$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_shape$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_stereo$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_stitching$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_structured_light$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_superres$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_surface_matching$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_text$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_tracking$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_video$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_videoio$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_videostab$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_xfeatures2d$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_ximgproc$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_xobjdetect$secondaryArchSuffix = $libVersionCompat
	devel:libopencv_xphoto$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	opencv$secondaryArchSuffix == $portVersion base
	"

PROVIDES_python="
	opencv${secondaryArchSuffix}_python = $portVersionCompat
	"
REQUIRES_python="
	haiku$secondaryArchSuffix
	opencv$secondaryArchSuffix == $portVersion base
	"

PROVIDES_tools="
	opencv${secondaryArchSuffix}_tools = $portVersionCompat
	cmd:opencv_annotation$commandSuffix = $portVersion
	cmd:opencv_interactive_calibration$commandSuffix = $portVersion
	cmd:opencv_version$commandSuffix = $portVersion
	cmd:opencv_visualisation$commandSuffix = $portVersion
	cmd:opencv_waldboost_detector$commandSuffix = $portVersion
	cmd:setup_vars_opencv4.sh$commandSuffix = $portVersion
	"
REQUIRES_tools="
	haiku$secondaryArchSuffix
	opencv$secondaryArchSuffix == $portVersion base
	numpy${secondaryArchSuffix}_python3
	cmd:python3
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:cblas$secondaryArchSuffix
	devel:eigen$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
#	devel:libcaffe$secondaryArchSuffix
#	devel:libceres$secondaryArchSuffix
	devel:libexif$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libgdal$secondaryArchSuffix
#	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$secondaryArchSuffix #openexr
	devel:libintl$secondaryArchSuffix
	devel:libjasper$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:liblapack$secondaryArchSuffix
	devel:liblept$secondaryArchSuffix
	devel:libopenblas$secondaryArchSuffix
	devel:libopencl_headers$secondaryArchSuffix
	devel:libopenjp2$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:libtesseract$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libwebp$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
#	cmd:ant
	cmd:cmake
	cmd:doxygen
	numpy${secondaryArchSuffix}_python3
	cmd:gcc$secondaryArchSuffix
#	cmd:javac
#	cmd:jni
	cmd:ld$secondaryArchSuffix
	cmd:make
#	cmd:matlab # maybe later
	cmd:pkg_config$secondaryArchSuffix
	cmd:python3
	"

defineDebugInfoPackage opencv$secondaryArchSuffix \
	$libDir/libopencv_alphamat.so.$libVersion \
	$libDir/libopencv_aruco.so.$libVersion \
	$libDir/libopencv_bgsegm.so.$libVersion \
	$libDir/libopencv_bioinspired.so.$libVersion \
	$libDir/libopencv_calib3d.so.$libVersion \
	$libDir/libopencv_ccalib.so.$libVersion \
	$libDir/libopencv_core.so.$libVersion \
	$libDir/libopencv_cvv.so.$libVersion \
	$libDir/libopencv_datasets.so.$libVersion \
	$libDir/libopencv_dnn_objdetect.so.$libVersion \
	$libDir/libopencv_dnn_superres.so.$libVersion \
	$libDir/libopencv_dnn.so.$libVersion \
	$libDir/libopencv_dpm.so.$libVersion \
	$libDir/libopencv_face.so.$libVersion \
	$libDir/libopencv_features2d.so.$libVersion \
	$libDir/libopencv_flann.so.$libVersion \
	$libDir/libopencv_freetype.so.$libVersion \
	$libDir/libopencv_fuzzy.so.$libVersion \
	$libDir/libopencv_gapi.so.$libVersion \
	$libDir/libopencv_hdf.so.$libVersion \
	$libDir/libopencv_hfs.so.$libVersion \
	$libDir/libopencv_highgui.so.$libVersion \
	$libDir/libopencv_img_hash.so.$libVersion \
	$libDir/libopencv_imgcodecs.so.$libVersion \
	$libDir/libopencv_imgproc.so.$libVersion \
	$libDir/libopencv_intensity_transform.so.$libVersion \
	$libDir/libopencv_line_descriptor.so.$libVersion \
	$libDir/libopencv_mcc.so.$libVersion \
	$libDir/libopencv_ml.so.$libVersion \
	$libDir/libopencv_objdetect.so.$libVersion \
	$libDir/libopencv_optflow.so.$libVersion \
	$libDir/libopencv_phase_unwrapping.so.$libVersion \
	$libDir/libopencv_photo.so.$libVersion \
	$libDir/libopencv_plot.so.$libVersion \
	$libDir/libopencv_quality.so.$libVersion \
	$libDir/libopencv_rapid.so.$libVersion \
	$libDir/libopencv_reg.so.$libVersion \
	$libDir/libopencv_rgbd.so.$libVersion \
	$libDir/libopencv_saliency.so.$libVersion \
	$libDir/libopencv_shape.so.$libVersion \
	$libDir/libopencv_stereo.so.$libVersion \
	$libDir/libopencv_stitching.so.$libVersion \
	$libDir/libopencv_structured_light.so.$libVersion \
	$libDir/libopencv_superres.so.$libVersion \
	$libDir/libopencv_surface_matching.so.$libVersion \
	$libDir/libopencv_text.so.$libVersion \
	$libDir/libopencv_tracking.so.$libVersion \
	$libDir/libopencv_video.so.$libVersion \
	$libDir/libopencv_videoio.so.$libVersion \
	$libDir/libopencv_videostab.so.$libVersion \
	$libDir/libopencv_xfeatures2d.so.$libVersion \
	$libDir/libopencv_ximgproc.so.$libVersion \
	$libDir/libopencv_xobjdetect.so.$libVersion \
	$libDir/libopencv_xphoto.so.$libVersion

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

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

	pythonVersion=$(python3 --version 2>&1 | sed 's/Python //' | head -c3)

	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		$cmakeDirArgs \
		-DBUILD_TESTS=OFF \
		-DBUILD_PERF_TESTS=OFF \
		-DCMAKE_INSTALL_BINDIR=$commandBinDir \
		-DCMAKE_INSTALL_LIBEXECDIR=$libDir \
		-DWITH_GDAL=ON \
		-DWITH_IPP=OFF \
		-DWITH_OPENGL=OFF \
		-DWITH_QT=ON \
		-DWITH_EIGEN=ON \
		-DWITH_OPENEXR=ON \
		-DWITH_FFMPEG=ON \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_SKIP_RPATH=ON \
		-DENABLE_PIC=OFF \
		-DOPENCV_GENERATE_PKGCONFIG=YES \
		-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" \
		-DOPENCV_PYTHON3_INSTALL_PATH="$prefix/lib/python$pythonVersion/vendor-packages" \
		-DPYTHON3_NUMPY_INCLUDE_DIRS:PATH="`finddir B_SYSTEM_LIB_DIRECTORY`/python${pythonVersion}/vendor-packages/numpy/core/include" \
		-DOPENCV_EXTRA_MODULES_PATH="$sourceDir2/opencv_contrib-$portVersion/modules" \
		-DEigen3_DIR=`finddir B_SYSTEM_DATA_DIRECTORY`/eigen3/cmake

	make $jobArgs
}

INSTALL()
{
	cd build_haiku

	make install

	prepareInstalledDevelLibs libopencv_alphamat \
		libopencv_aruco \
		libopencv_bgsegm \
		libopencv_bioinspired \
		libopencv_calib3d \
		libopencv_ccalib \
		libopencv_cvv \
		libopencv_datasets \
		libopencv_dnn_objdetect \
		libopencv_dnn_superres \
		libopencv_dpm \
		libopencv_face \
		libopencv_freetype \
		libopencv_fuzzy \
		libopencv_hdf \
		libopencv_hfs \
		libopencv_img_hash \
		libopencv_intensity_transform \
		libopencv_line_descriptor \
		libopencv_mcc \
		libopencv_optflow \
		libopencv_phase_unwrapping \
		libopencv_plot \
		libopencv_quality \
		libopencv_rapid \
		libopencv_reg \
		libopencv_rgbd \
		libopencv_saliency \
		libopencv_shape \
		libopencv_stereo \
		libopencv_structured_light \
		libopencv_superres \
		libopencv_surface_matching \
		libopencv_text \
		libopencv_tracking \
		libopencv_videostab \
		libopencv_xfeatures2d \
		libopencv_ximgproc \
		libopencv_xobjdetect \
		libopencv_xphoto \
		libopencv_core \
		libopencv_dnn \
		libopencv_features2d \
		libopencv_flann \
		libopencv_gapi \
		libopencv_highgui \
		libopencv_imgcodecs \
		libopencv_imgproc \
		libopencv_ml \
		libopencv_objdetect \
		libopencv_photo \
		libopencv_stitching \
		libopencv_video \
		libopencv_videoio

	# fixCmake
	sed -i 's,\/include,/'${relativeIncludeDir}',g' \
		$libDir/cmake/opencv4/OpenCVConfig.cmake

	fixPkgconfig

	# fixPkgconfig
	if [ -n "$secondaryArchSuffix" ]; then
		sed -i 's,\/headers/x86,\/headers/x86/opencv4,g' \
			$prefix/$relativeDevelopLibDir/pkgconfig/opencv4.pc
	else
		sed -i 's,\/headers,\/headers/opencv4,g' \
			$prefix/$relativeDevelopLibDir/pkgconfig/opencv4.pc
	fi

	# devel package
	packageEntries devel \
		$developDir \
		$libDir/cmake

	# python package
	packageEntries python \
		$prefix/lib/python*

	# tools package
	packageEntries tools \
		$commandBinDir
}

TEST()
{
	LIBRARY_PATH="$sourceDir/build_haiku/lib${LIBRARY_PATH:+:$LIBRARY_PATH}" \
	make -C build_haiku test $jobArgs
}
