# - Find GD
# Find the native GD includes and library
# This module defines
# GD_INCLUDE_DIR, where to find gd.h, etc.
# GD_LIBRARIES, the libraries needed to use GD.
# GD_FOUND, If false, do not try to use GD.
# also defined, but not for general use are
# GD_LIBRARY, where to find the GD library.
# GD_SUPPORTS_PNG, GD_SUPPORTS_JPEG, GD_SUPPORTS_GIF, test
# support for image formats in GD.

SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

# try using gdlib-config utility
#~ 	FIND_PROGRAM(GD_CONFIG NAMES gdlib-config DOC "Path to gd_config utility")

	IF(GD_CONFIG)	
		MESSAGE(STATUS "Using gdlib-config utility to find GD")
		
		EXEC_PROGRAM(${GD_CONFIG}
			ARGS "--version"
			OUTPUT_VARIABLE GD_CONFIG_VERSION)

		IF(${GD_CONFIG_VERSION} MATCHES "^[A-Za-z]+[ ](.*)$")
			STRING(REGEX REPLACE "^[A-Za-z]+[ ](.*)$" "\\1" GD_VERSION "${GD_CONFIG_VERSION}")
		ENDIF()

		EXEC_PROGRAM(${GD_CONFIG}
			ARGS "--includedir"
			OUTPUT_VARIABLE GD_CONFIG_INCLUDEDIR)  

		EXEC_PROGRAM(${GD_CONFIG}
			ARGS "--libdir"
			OUTPUT_VARIABLE GD_CONFIG_LIBDIR)

		EXEC_PROGRAM(${GD_CONFIG}
			ARGS "--features"
			OUTPUT_VARIABLE GD_CONFIG_FEATURES)      
	ELSE()
		SET(GD_VERSION "unknown")
	ENDIF()

# search for gd.h header
	FIND_PATH(GD_INCLUDE_DIR gd.h
		${GD_CONFIG_INCLUDEDIR}
		/usr/local/include
		/usr/include
		)

# search for GD library
	if(WIN32 AND NOT CYGWIN)
		SET(GD_NAMES ${GD_NAMES} bgd)
	else(WIN32)
		SET(GD_NAMES ${GD_NAMES} gd)
	endif(WIN32 AND NOT CYGWIN)

	FIND_LIBRARY(GD_LIBRARY
		NAMES ${GD_NAMES}
		PATHS
			${GD_CONFIG_LIBDIR}
			/usr/lib64 
			/usr/lib 
			/usr/local/lib
		)

# check if header and library found
	IF (GD_LIBRARY AND GD_INCLUDE_DIR)
		SET(GD_LIBRARIES ${GD_LIBRARY})
		SET(GD_FOUND "YES")
	ELSE (GD_LIBRARY AND GD_INCLUDE_DIR)
		SET(GD_FOUND "NO")
	ENDIF (GD_LIBRARY AND GD_INCLUDE_DIR)

# if header and library were found
IF (GD_FOUND)
#~ 	IF (WIN32 AND NOT CYGWIN)
#~ 		SET(GD_SUPPORTS_PNG ON)
#~ 		SET(GD_SUPPORTS_JPEG ON)
#~ 		SET(GD_SUPPORTS_GIF ON)
#~ 		get_filename_component(GD_LIBRARY_DIR ${GD_LIBRARY} PATH)
#~ 	ELSE (WIN32 AND NOT CYGWIN)

	# check for GD features
	IF(GD_CONFIG_FEATURES)
		# use GD_CONFIG_FEATURES 
		STRING(REGEX MATCHALL "[A-Za-z_]+"   GD_FEATURES ${GD_CONFIG_FEATURES})		
	ELSE()
		# check GD features manually
		INCLUDE(CheckLibraryExists)
		GET_FILENAME_COMPONENT(GD_LIB_PATH ${GD_LIBRARY} PATH)
		GET_FILENAME_COMPONENT(GD_LIB ${GD_LIBRARY} NAME)

		# check if GD supports PNG
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImagePng" "${GD_LIB_PATH}" GD_SUPPORTS_PNG)
		IF (GD_SUPPORTS_PNG)
#~ 			SET(GD_SUPPORTS_PNG "Yes")
			SET(GD_FEATURES "GD_PNG")
			find_package(PNG)
			IF (PNG_FOUND)
				SET(GD_LIBRARIES ${GD_LIBRARIES} ${PNG_LIBRARIES})
				SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${PNG_INCLUDE_DIR})
			ELSE (PNG_FOUND)
				SET(GD_SUPPORTS_PNG "NO")
			ENDIF (PNG_FOUND)
		ENDIF (GD_SUPPORTS_PNG)

		# check if GD supports JPEG
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImageJpeg" "${GD_LIB_PATH}" GD_SUPPORTS_JPEG)
		IF (GD_SUPPORTS_JPEG)
#~ 			SET(GD_SUPPORTS_JPEG "Yes")
			SET(GD_FEATURES ${GD_FEATURES} "GD_JPEG")
			find_package(JPEG)
				IF (JPEG_FOUND)
					SET(GD_LIBRARIES ${GD_LIBRARIES} ${JPEG_LIBRARIES})
					SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${JPEG_INCLUDE_DIR})
				ELSE (JPEG_FOUND)
					SET(GD_SUPPORTS_JPEG "NO")
				ENDIF (JPEG_FOUND)
		ENDIF (GD_SUPPORTS_JPEG)

		# check if GD supports GIF
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImageGif" "${GD_LIB_PATH}" GD_SUPPORTS_GIF)
		IF (GD_SUPPORTS_GIF)
#~ 			SET(GD_SUPPORTS_GIF "Yes")
			SET(GD_FEATURES ${GD_FEATURES} "GD_GIF")
			find_package(GIF)
				IF (GIF_FOUND)
					SET(GD_LIBRARIES ${GD_LIBRARIES} ${GIF_LIBRARIES})
					SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${GIF_INCLUDE_DIR})
				ELSE (GIF_FOUND)
					SET(GD_SUPPORTS_GIF "NO")
				ENDIF (GIF_FOUND)
		ENDIF (GD_SUPPORTS_GIF)

		# check if GD supports FreeType
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdFontCacheSetup" "${GD_LIB_PATH}" GD_SUPPORTS_FREETYPE)
		IF (GD_SUPPORTS_FREETYPE)
#~ 			SET(GD_SUPPORTS_GIF "Yes")
			SET(GD_FEATURES ${GD_FEATURES} "GD_FREETYPE")
			FIND_PACKAGE(Freetype)
				IF (FREETYPE_FOUND)
					SET(GD_LIBRARIES ${GD_LIBRARIES} ${FREETYPE_LIBRARIES})
					SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${FREETYPE_INCLUDE_DIRS})
				ELSE ()
					SET(GD_SUPPORTS_FREETYPE "NO")
				ENDIF()
		ENDIF()

		# check if GD supports XPM
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdImageCreateFromXbm" "${GD_LIB_PATH}" GD_SUPPORTS_XPM)
		IF (GD_SUPPORTS_XPM)
#~ 			SET(GD_SUPPORTS_GIF "Yes")
			SET(GD_FEATURES ${GD_FEATURES} "GD_XPM")
			FIND_PACKAGE(XPM)
				IF (XPM_FOUND)
					SET(GD_LIBRARIES ${GD_LIBRARIES} ${XPM_LIBRARIES})
					SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${XPM_INCLUDE_DIRS})
				ELSE ()
					SET(GD_SUPPORTS_XPM "NO")
				ENDIF()
		ENDIF()

		# check if GD supports FontConfig
		CHECK_LIBRARY_EXISTS("${GD_LIBRARY}" "gdFTUseFontConfig" "${GD_LIB_PATH}" GD_SUPPORTS_FONTCONFIG)
		IF (GD_SUPPORTS_FONTCONFIG)
#~ 			SET(GD_SUPPORTS_GIF "Yes")
			SET(GD_FEATURES ${GD_FEATURES} "GD_FONTCONFIG")
			FIND_PACKAGE(Fontconfig)
				IF (FONTCONFIG_FOUND)
					SET(GD_LIBRARIES ${GD_LIBRARIES} ${FONTCONFIG_LIBRARIES})
					SET(GD_INCLUDE_DIR ${GD_INCLUDE_DIR} ${FONTCONFIG_INCLUDE_DIRS})
				ELSE ()
					SET(GD_SUPPORTS_FONTCONFIG "NO")
				ENDIF()
		ENDIF()

		# Trim the list of include directories
		SET(GDINCTRIM)
		FOREACH(GD_DIR ${GD_INCLUDE_DIR})
			SET(GD_TMP_FOUND OFF)
			FOREACH(GD_TRIMMED ${GDINCTRIM})
				IF ("${GD_DIR}" STREQUAL "${GD_TRIMMED}")
						SET(GD_TMP_FOUND ON)
				ENDIF ("${GD_DIR}" STREQUAL "${GD_TRIMMED}")
			ENDFOREACH(GD_TRIMMED ${GDINCTRIM})
				IF (NOT GD_TMP_FOUND)
					SET(GDINCTRIM "${GDINCTRIM}" "${GD_DIR}")
				ENDIF (NOT GD_TMP_FOUND)
		ENDFOREACH(GD_DIR ${GD_INCLUDE_DIR})
		SET(GD_INCLUDE_DIR ${GDINCTRIM})

		# Generate trimmed list of library directories and list of libraries
		SET(GD_LIBRARY_DIR)		
		FOREACH(GD_LIB ${GD_LIBRARIES})
			GET_FILENAME_COMPONENT(GD_NEXTLIBDIR ${GD_LIB} PATH)
			SET(GD_TMP_FOUND OFF)
			FOREACH(GD_LIBDIR ${GD_LIBRARY_DIR})
				IF ("${GD_NEXTLIBDIR}" STREQUAL "${GD_LIBDIR}")
					SET(GD_TMP_FOUND ON)
				ENDIF ("${GD_NEXTLIBDIR}" STREQUAL "${GD_LIBDIR}")
			ENDFOREACH(GD_LIBDIR ${GD_LIBRARIES})
				IF (NOT GD_TMP_FOUND)
					SET(GD_LIBRARY_DIR "${GD_LIBRARY_DIR}" "${GD_NEXTLIBDIR}")
				ENDIF (NOT GD_TMP_FOUND)
		ENDFOREACH(GD_LIB ${GD_LIBRARIES})
	ENDIF()
	
#~ 	ENDIF (WIN32 AND NOT CYGWIN)
ENDIF (GD_FOUND)

IF (GD_FOUND)
	IF (NOT GD_FIND_QUIETLY)
		MESSAGE(STATUS "Found GD: ${GD_LIBRARY}")
	ENDIF (NOT GD_FIND_QUIETLY)
ELSE (GD_FOUND)
	IF (GD_FIND_REQUIRED)
		MESSAGE(FATAL_ERROR "Could not find GD library")
	ENDIF (GD_FIND_REQUIRED)
ENDIF (GD_FOUND)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GD
	DEFAULT_MSG
	GD_INCLUDE_DIR
	GD_LIBRARIES
	)

MESSAGE(STATUS "GD features: ${GD_FEATURES}")
#~ MESSAGE(STATUS "GD supports PNG: ${GD_SUPPORTS_PNG}")
#~ MESSAGE(STATUS "GD supports JPEG: ${GD_SUPPORTS_JPEG}")
#~ MESSAGE(STATUS "GD supports GIF: ${GD_SUPPORTS_GIF}")

MARK_AS_ADVANCED(
	GD_LIBRARY
	GD_LIBRARIES
	  GD_INCLUDE_DIR
	  GD_LIBRARY_DIR
	  GD_SUPPORTS_PNG
	  GD_SUPPORTS_JPEG
	  GD_SUPPORTS_GIF
)