#!/bin/sh

if [ "$help" = "1" ]
then
	cat << EOF
OpenCV options:

  --enable-opencv         - This module must be explicitly enabled due to a multi-threading issue:
                            https://github.com/mltframework/mlt/issues/274

  The Tracker filter requires OpenCV >= 3.1.0 and the tracking contrib module.

EOF

else
    for i in "$@"
	do
	    case $i in
		    --enable-opencv ) opencv_enabled=1;;
		esac
	done

    if [ "$opencv_enabled" != "1" ]; then
	    echo "- not explicitly enabled: disabling"
		touch ../disable-opencv
		exit 0
	fi

	opencvname=opencv
	pkg-config "$opencvname"
	if [ $? -ne 0 ]
	then
		# Try v4, they renamed
		opencvname=opencv4
	fi

	pkg-config --atleast-version=3.1.0 "$opencvname"

	if [ $? -eq 0 ]
	then
                result=`pkg-config --libs "$opencvname" | grep "opencv_tracking"`
                if [ -z "$result" ]
                then
                        echo "- OpenCV tracking contrib module NOT found, disabling OpenCV modules"
                        touch ../disable-opencv
                        exit 0
                else
                        echo "CFLAGS += $(pkg-config --cflags "$opencvname")" >> config.mak
                        echo "LDFLAGS += $(pkg-config --libs "$opencvname")" >> config.mak
                fi

		pkg-config --atleast-version=4.0.0 "$opencvname"
		if [ $? -eq 0 ]
		then
			echo "CXXFLAGS += -std=c++11" >> config.mak
		fi
	else
	    echo "- OpenCV >= 3.1.0 NOT found: disabling"
		touch ../disable-opencv
		exit 0
	fi

	exit 0
fi
