FILE(GLOB TESTABLE_PYTHON_SAMPLES */*.py)

FIND_PACKAGE(Python3 REQUIRED COMPONENTS Interpreter)

SET(DV_PROCESSING_BIN_DIR ${PROJECT_BINARY_DIR}/python)

ADD_CUSTOM_TARGET(
	CompileAllPythonSamples
	COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DV_PROCESSING_BIN_DIR} ${Python3_EXECUTABLE} -m compileall
			${CMAKE_CURRENT_SOURCE_DIR}
	WORKING_DIRECTORY ${DV_PROCESSING_BIN_DIR})
ADD_DEPENDENCIES(CompileAllPythonSamples dv_processing)

SET(SAMPLE_RUN_IGNORE "")

# List of python samples that are expected to FAIL while running
LIST(APPEND SAMPLE_RUN_IGNORE "event_stream_slicing/sample2.py")
LIST(APPEND SAMPLE_RUN_IGNORE "event_stream_slicing/sample3.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample2.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample3.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample4.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample5.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample6.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample8.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample9.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample10.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample11.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample12.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample13.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample16.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample17.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample18.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample19.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample20.py")
LIST(APPEND SAMPLE_RUN_IGNORE "reading_data/sample21.py")
LIST(APPEND SAMPLE_RUN_IGNORE "writing_data/sample2.py")
LIST(APPEND SAMPLE_RUN_IGNORE "accumulators/sample6.py")
LIST(APPEND SAMPLE_RUN_IGNORE "accumulators/sample7.py")
LIST(APPEND SAMPLE_RUN_IGNORE "accumulators/sample8.py")
LIST(APPEND SAMPLE_RUN_IGNORE "accumulators/sample9.py")
LIST(APPEND SAMPLE_RUN_IGNORE "accumulators/sample10.py")
LIST(APPEND SAMPLE_RUN_IGNORE "camera_geometry/sample1.py")
LIST(APPEND SAMPLE_RUN_IGNORE "depth_estimation/sample0.py")
LIST(APPEND SAMPLE_RUN_IGNORE "depth_estimation/sample1.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_detection/sample0.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_detection/sample1.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_detection/sample2.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_tracking/sample0.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_tracking/sample1.py")
LIST(APPEND SAMPLE_RUN_IGNORE "feature_tracking/sample2.py")

SET(COUNTER 0)
SET(PYTHON_DOC_TARGETS "")
FOREACH(PYTHON_SAMPLE ${TESTABLE_PYTHON_SAMPLES})
	SET(IGNORED -1)
	FOREACH(IGNORED_FILE ${SAMPLE_RUN_IGNORE})
		STRING(FIND ${PYTHON_SAMPLE} ${IGNORED_FILE} IGNORED)
		IF(${IGNORED} GREATER_EQUAL "0")
			BREAK()
		ENDIF()
	ENDFOREACH()

	IF(${IGNORED} GREATER_EQUAL "0")
		CONTINUE()
	ENDIF()

	GET_FILENAME_COMPONENT(DIRECTORY_PATH ${PYTHON_SAMPLE} DIRECTORY)
	GET_FILENAME_COMPONENT(DIRNAME ${DIRECTORY_PATH} NAME)
	GET_FILENAME_COMPONENT(SAMPLE_NAME ${PYTHON_SAMPLE} NAME)

	SET(CURRENT_TARGET_NAME docs-python-sample-${DIRNAME}-${SAMPLE_NAME}-${COUNTER})

	ADD_CUSTOM_TARGET(
		${CURRENT_TARGET_NAME}
		COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${DV_PROCESSING_BIN_DIR} ${Python3_EXECUTABLE} ${PYTHON_SAMPLE}
		WORKING_DIRECTORY ${DV_PROCESSING_BIN_DIR}
		SOURCES ${PYTHON_SAMPLE})
	ADD_DEPENDENCIES(${CURRENT_TARGET_NAME} dv_processing)
	MATH(EXPR COUNTER "${COUNTER} + 1")
	LIST(APPEND PYTHON_DOC_TARGETS ${CURRENT_TARGET_NAME})
ENDFOREACH()

# Create a target that runs python code samples
ADD_CUSTOM_TARGET(all-docs-python-samples)
ADD_DEPENDENCIES(all-docs-python-samples CompileAllPythonSamples ${PYTHON_DOC_TARGETS})
