# Locates GLM headers.
#
# Uses:
# GLM_FETCH_CONTENT (OFF by default): Will use fetch content if ON. This will download and compile the GLM's git repository instead of using locally installed files.
# GLM_ROOT_DIR (optional, only if GLM_FETCH_CONTENT is OFF): Lets you determine the directory GLM is in. If not set, it will try to find the GLM headers in common directories. This directory should contain the `glm` folder with `glm.hpp` and other GLM headers.
# GLM_FETCH_CONTENT_VERSION (optional, only if GLM_FETCH_CONTENT is ON): Lets you select the GLM version if you use fetch content.
#
# Defines:
# GLM_FOUND: true if the headers were found successfully.
# GLM_HEADERS (only if GLM_FOUND is true): the directory containing the GLM source including the 'glm/glm.hpp' file.

option(GLM_FETCH_CONTENT "Use fetch content to download GLM" OFF)

if(NOT GLM_FETCH_CONTENT)
	# Non Fetch Content
	set(GLM_ENV_ROOT_DIR "$ENV{GLM_ROOT_DIR}")
	if(GLM_ROOT_DIR)
		# GLM headers directory is provided by the user
		set(GLM_SEARCH_DIRECTORIES
				"${GLM_ROOT_DIR}"
				)
	elseif(GLM_ENV_ROOT_DIR)
		# GLM headers directory is provided by an environment variable
		set(GLM_SEARCH_DIRECTORIES
				"${GLM_ENV_ROOT_DIR}"
				)
	else()
		# Look for GLM headers in common include directories
		set(GLM_SEARCH_DIRECTORIES
				"/usr/include"
				"/usr/local/include"
				"/${CMAKE_SOURCE_DIR}/deps/include"
				"C:/Program Files (x86)/glm"
				)
	endif()
	find_path(GLM_INCLUDE_DIR "glm/glm.hpp" PATHS ${GLM_SEARCH_DIRECTORIES})
else()
	# Fetch Content
	if(NOT GLM_FETCH_CONTENT_VERSION)
		# Set GLM version if not set by user
		set(GLM_FETCH_CONTENT_VERSION 0.9.9.8)
	endif()
	include(FetchContent)
	FetchContent_Declare(
			GLM
			GIT_REPOSITORY	https://github.com/g-truc/glm.git
			GIT_TAG			${GLM_FETCH_CONTENT_VERSION}
	)
	if(NOT glm_POPULATED)
		FetchContent_Populate(GLM)
	endif()
	set(GLM_INCLUDE_DIR "${glm_SOURCE_DIR}")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLM DEFAULT_MSG GLM_INCLUDE_DIR)
if(GLM_FOUND)
	set(GLM_HEADERS ${GLM_INCLUDE_DIR})
endif()
