# Locates PhysFS library and headers.
#
# Uses:
# PHYSFS_FETCH_CONTENT (OFF by default): Will use fetch content if ON. This will download and compile the SDL's git repository instead of using locally installed files.
# PHYSFS_HEADERS_DIR (optional, only if PHYSFS_FETCH_CONTENT is OFF): Lets you determine the directory containing PhysFS headers. If not set, it will try to find the PhysFS headers in common directories. This directory should directly contain the PhysFS headers including `SDL.h`. WARNING: This SHOULD NOT be the root directory.
# PHYSFS_FETCH_CONTENT_VERSION (optional, only if PHYSFS_FETCH_CONTENT is ON): Lets you select the PhysFS version if you use fetch content.
#
# Defines:
# PHYSFS_FOUND: true if both the library and headers were found successfully.
# PHYSFS_HEADERS (only if PHYSFS_FOUND is true): the directory containing the PhysFS headers including the `physfs.h` file.

option(PHYSFS_FETCH_CONTENT "Use fetch content to download PhysFS" OFF)

if(NOT PHYSFS_FETCH_CONTENT)
	# Non Fetch Content
	set(PHYSFS_ENV_DIR "$ENV{PHYSFS_DIR}")
	if(PHYSFS_HEADERS_DIR)
		# physfs headers directory is provided by the user
		set(PHYSFS_SEARCH_DIRECTORIES
				"${PHYSFS_HEADERS_DIR}"
				)
	elseif(PHYSFS_ENV_DIR)
		# physfs headers directory is provided by an environment variable
		set(PHYSFS_SEARCH_DIRECTORIES
				"${PHYSFS_ENV_DIR}"
				"${PHYSFS_ENV_DIR}/src"
				)
	else()
		# Look for physfs headers in common include directories
		set(PHYSFS_SEARCH_DIRECTORIES
				"/usr/include/physfs"
				"/usr/local/include/physfs"
				"/${CMAKE_SOURCE_DIR}/deps/include/physfs"
				"C:/Program Files (x86)/physfs"
				)
	endif()
	find_path(PHYSFS_FOUND_INCLUDE_DIR "physfs.h" PATHS ${PHYSFS_SEARCH_DIRECTORIES})
else()
	# Fetch Content
	if(NOT PHYSFS_FETCH_CONTENT_VERSION)
		# Set PhysFS version if not set by user
		set(PHYSFS_FETCH_CONTENT_VERSION release-3.0.2)
	endif()
	include(FetchContent)
	FetchContent_Declare(
			PHYSFS
			GIT_REPOSITORY	https://github.com/criptych/physfs
			GIT_TAG			${PHYSFS_FETCH_CONTENT_VERSION}
	)
	FetchContent_MakeAvailable(PHYSFS)
	#message(WARNING ${physfs_SOURCE_DIR})
	set(PHYSFS_FOUND_INCLUDE_DIR "${physfs_SOURCE_DIR}/src")
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PhysFS DEFAULT_MSG PHYSFS_FOUND_INCLUDE_DIR)
if(PHYSFS_FOUND)
	set(PHYSFS_HEADERS ${PHYSFS_FOUND_INCLUDE_DIR})
endif()
