set(PSEMEK_CONVERT_MESH_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH "Path to convert-mesh tool")

function(psemek_add_model TARGET)
	if(PSEMEK_PACKAGE_MODE)
		if(NOT (TARGET ${TARGET}))
			return()
		endif()
	endif()

	get_target_property(_OUT_DIR ${TARGET} BINARY_DIR)

	while(ARGN)
		list(GET ARGN 0 _FILE)
		list(GET ARGN 1 _MODEL_NAME)
		list(GET ARGN 2 _RESOURCE_NAME)
		list(REMOVE_AT ARGN 0 1 2)

		get_filename_component(_NAME_PREFIX ${_RESOURCE_NAME} DIRECTORY)

		file(MAKE_DIRECTORY "${_OUT_DIR}/meshes/${_NAME_PREFIX}")

		set(_OUT_FILE "${_OUT_DIR}/meshes/${_RESOURCE_NAME}")

		set(_SCRIPT "${PSEMEK_CONVERT_MESH_DIR}/bin/convert-mesh.py")

		add_custom_command(OUTPUT "${_OUT_FILE}"
			COMMAND blender --background "${_FILE}" --python "${_SCRIPT}" --python-exit-code 1 -- "${_MODEL_NAME}" "${_OUT_FILE}"
			DEPENDS "${_FILE}" "${_SCRIPT}"
			WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
		)

		psemek_add_resources(${TARGET} "${_OUT_FILE}" ${_RESOURCE_NAME})
	endwhile()
endfunction()
