#
# Try to find the FreeImage library and include path.
# Once done this will define
#
# FREEIMAGE_FOUND
# FREEIMAGE_INCLUDE_PATH
# FREEIMAGE_LIBRARY
# FREEIMAGE_STATIC_LIBRARY
# FREEIMAGE_DYNAMIC_LIBRARY
#

OPTION(USE_FREEIMAGE_STATIC "Use Static FreeImage Lib" OFF)

SET(FREEIMAGE_SEARCH_PATHS
	~/Library/Frameworks
	/Library/Frameworks
	/usr/local
	/usr
	/sw # Fink
	/opt/local # DarwinPorts
	/opt/csw # Blastwave
	/opt
)

FIND_PATH( FREEIMAGE_INCLUDE_PATH FreeImage.h
  HINTS
  ${FREEIMAGE}
	$ENV{FREEIMAGE}
  PATH_SUFFIXES include/FreeImage include FreeImage
  Dist/x32
  Dist/x64
  PATHS
  ${FREEIMAGE_SEARCH_PATHS}
  DOC "The directory where FreeImage.h resides"
)

SET(PX ${CMAKE_STATIC_LIBRARY_PREFIX})
SET(SX ${CMAKE_STATIC_LIBRARY_SUFFIX})
# Lookup the 64 bit libs on x64
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
	FIND_LIBRARY(FREEIMAGE_DYNAMIC_LIBRARY
    NAMES FreeImage freeimage
		HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
		PATH_SUFFIXES lib64 lib
		lib/x64
    Dist/x64
		x86_64-w64-mingw32/lib
		PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The FreeImage library 64bit"
	)

  FIND_LIBRARY( FREEIMAGE_STATIC_LIBRARY
    NAMES ${PX}FreeImageLIB${SX} ${PX}FreeImage${SX} ${PX}freeimage${SX}
    HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
    PATH_SUFFIXES lib64 lib
		lib/x64
    Dist/x64
		x86_64-w64-mingw32/lib
    PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The static FreeImage library 64bit"
  )

	FIND_FILE(FREEIMAGE_DLL
    NAMES FreeImage.dll freeimage.dll
		HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
		PATH_SUFFIXES lib64 lib
		lib/x64
    Dist/x64
		x86_64-w64-mingw32/lib
		PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The FreeImage DLL 64bit"
	)

# On 32bit build find the 32bit libs
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
	FIND_LIBRARY(FREEIMAGE_DYNAMIC_LIBRARY
    NAMES FreeImage freeimage
		HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
		PATH_SUFFIXES lib
		lib/x86
    Dist/x32
		i686-w64-mingw32/lib
		PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The FreeImage library 32bit"
	)

  FIND_LIBRARY( FREEIMAGE_STATIC_LIBRARY
    NAMES ${PX}FreeImageLIB${SX} ${PX}FreeImage${SX} ${PX}freeimage${SX}
    HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
    PATH_SUFFIXES lib
		lib/x86
    Dist/x32
		i686-w64-mingw32/lib
    PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The static FreeImage library 32bit"
  )

	FIND_FILE(FREEIMAGE_DLL
    NAMES FreeImage.dll freeimage.dll
		HINTS
    ${FREEIMAGE}
  	$ENV{FREEIMAGE}
		PATH_SUFFIXES lib
		lib/x86
    Dist/x32
		i686-w64-mingw32/lib
		PATHS ${FREEIMAGE_SEARCH_PATHS}
    DOC "The FreeImage DLL 32bit"
	)
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)

UNSET(PX)
UNSET(SX)

IF(USE_FREEIMAGE_STATIC)
  MESSAGE(STATUS "Using Static FreeImage Lib")
  ADD_DEFINITIONS(-DFREEIMAGE_LIB)
  SET(FREEIMAGE_LIBRARY ${FREEIMAGE_STATIC_LIBRARY})
ELSE(USE_FREEIMAGE_STATIC)
  MESSAGE(STATUS "Using Dynamic FreeImage Lib")
  REMOVE_DEFINITIONS(-DFREEIMAGE_LIB)
  SET(FREEIMAGE_LIBRARY ${FREEIMAGE_DYNAMIC_LIBRARY})
ENDIF(USE_FREEIMAGE_STATIC)

MARK_AS_ADVANCED(
    FREEIMAGE_DYNAMIC_LIBRARY
    FREEIMAGE_STATIC_LIBRARY
    )
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(FREEIMAGE DEFAULT_MSG
    FREEIMAGE_INCLUDE_PATH FREEIMAGE_LIBRARY)
