# - Find mysqlclient
#
# -*- cmake -*-
#
# Find the native MySQL includes and library
#
#	MySQL_INCLUDE_DIR	- where to find mysql.h, etc.
#	MySQL_LIBRARIES		- List of libraries when using MySQL.
#	MySQL_FOUND			- True if MySQL found.
#	The following can be used as a hint as to where to search:
#	MYSQL_ROOT

IF (MySQL_INCLUDE_DIR AND MySQL_LIBRARIES)
	# Already in cache, be silent
	SET(MySQL_FIND_QUIETLY TRUE)
ENDIF (MySQL_INCLUDE_DIR AND MySQL_LIBRARIES)

# Include dir
IF(MYSQL_ROOT)
	FIND_PATH(MySQL_INCLUDE_DIR
		NAMES mysql.h
		PATHS ${MYSQL_ROOT}/include
		PATH_SUFFIXES mysql
		NO_DEFAULT_PATH
		NO_SYSTEM_ENVIRONMENT_PATH
	)
	FIND_PATH(MySQL_INCLUDE_DIR
		NAMES mysql.h
		PATH_SUFFIXES mysql
	)
ELSE(MYSQL_ROOT)
	FIND_PATH(MySQL_INCLUDE_DIR
		NAMES mysql.h
		PATH_SUFFIXES mysql
	)
ENDIF(MYSQL_ROOT)

# Library
SET(MySQL_NAMES libmysql)
IF(MYSQL_ROOT)
	FIND_LIBRARY(MySQL_LIBRARY
		NAMES ${MySQL_NAMES}
		PATHS ${MYSQL_ROOT}/lib
		PATH_SUFFIXES mysql
		NO_DEFAULT_PATH
		NO_SYSTEM_ENVIRONMENT_PATH
	)
	
	FIND_LIBRARY(MySQL_LIBRARY
		NAMES ${MySQL_NAMES}
		PATH_SUFFIXES mysql
	)
ELSE(MYSQL_ROOT)
	FIND_LIBRARY(MySQL_LIBRARY
		NAMES ${MySQL_NAMES} mysqlclient_r mysqlclient
		PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64
		PATH_SUFFIXES mysql
	)
ENDIF(MYSQL_ROOT)

IF (MySQL_INCLUDE_DIR AND MySQL_LIBRARY)
	SET(MySQL_FOUND TRUE)
	SET( MySQL_LIBRARIES ${MySQL_LIBRARY} )
ELSE (MySQL_INCLUDE_DIR AND MySQL_LIBRARY)
	SET(MySQL_FOUND FALSE)
	SET( MySQL_LIBRARIES )
ENDIF (MySQL_INCLUDE_DIR AND MySQL_LIBRARY)


# handle the QUIETLY and REQUIRED arguments and set MySQL_FOUND to TRUE if
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(MySQL DEFAULT_MSG MySQL_LIBRARY MySQL_INCLUDE_DIR)

IF(MySQL_FOUND)
	SET( MySQL_LIBRARY_RELEASE ${MySQL_LIBRARY} )
	SET( MySQL_LIBRARY_DEBUG ${MySQL_LIBRARY} )
	SET( MySQL_LIBRARIES ${MySQL_LIBRARY_RELEASE} ${MySQL_LIBRARY_DEBUG} )
ELSE(MySQL_FOUND)
	SET( MySQL_LIBRARIES )
ENDIF(MySQL_FOUND)

MARK_AS_ADVANCED(
	MySQL_LIBRARY_DEBUG
	MySQL_LIBRARY_RELEASE
	MySQL_INCLUDE_DIR
	)
