# Find CURL
#
# This module defines
#  CURL_FOUND
#  CURL_INCLUDE_DIRS
#  CURL_LIBRARIES
#
# Copyright (c) 2012 I-maginer
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA, or go to
# http://www.gnu.org/copyleft/lesser.txt
#

# On a new cmake run, we do not need to be verbose
IF(CURL_INCLUDE_DIR AND CURL_LIBRARY)
	SET(CURL_FIND_QUIETLY TRUE)
ENDIF()

include(FindPkgMacros)
findpkg_begin(CURL)
create_search_paths(CURL)

# If CURL_ROOT was defined in the environment, use it.
if (NOT CURL_ROOT)
  if(NOT "$ENV{CURL_ROOT}" STREQUAL "")
    set(CURL_ROOT $ENV{CURL_ROOT})
  else()
    set(CURL_ROOT ${CURL_PREFIX_PATH})
  endif()
endif()

# concat all the search paths
IF(CURL_ROOT)
	SET(CURL_INCLUDE_SEARCH_DIRS
	  ${CURL_INCLUDE_SEARCH_DIRS}
	  ${CURL_ROOT}/include
  )
  if (WIN32 AND NOT ANDROID)

    SET(CURL_LIBRARY_SEARCH_RELEASE_DIRS
      ${CURL_LIBRARY_SEARCH_DIRS}
      ${CURL_ROOT}/lib
      ${CURL_ROOT}/lib/Release
    )
    SET(CURL_LIBRARY_SEARCH_DEBUG_DIRS
      ${CURL_LIBRARY_SEARCH_DIRS}
      ${CURL_ROOT}/lib
      ${CURL_ROOT}/lib/Debug
      ${CURL_ROOT}/lib/Release
    )
    SET(CURL_LIBRARY_PREFIX "lib")
    
  else()
    SET(CURL_LIBRARY_SEARCH_RELEASE_DIRS
      ${CURL_LIBRARY_SEARCH_DIRS}
      ${CURL_ROOT}/lib
    )
    SET(CURL_LIBRARY_SEARCH_DEBUG_DIRS
      ${CURL_LIBRARY_SEARCH_DIRS}
      ${CURL_ROOT}/lib
    )
    SET(CURL_LIBRARY_PREFIX "")
  endif()
ENDIF()

message (warning ${CURL_LIBRARY_SEARCH_RELEASE_DIRS})
# log message
IF (NOT CURL_FIND_QUIETLY)
	MESSAGE(STATUS "Checking for CURL library")
ENDIF()

# Search for header files
FIND_PATH(CURL_INCLUDE_DIR curl/curl.h
  PATHS ${CURL_INCLUDE_SEARCH_DIRS})

# Search for libraries files (release mode)
FIND_LIBRARY(CURL_LIBRARY_RELEASE ${CURL_LIBRARY_PREFIX}curl curl
PATHS ${CURL_LIBRARY_SEARCH_RELEASE_DIRS} NO_DEFAULT_PATH)

# Search for libraries files (debug mode)
FIND_LIBRARY(CURL_LIBRARY_DEBUG curld ${CURL_LIBRARY_PREFIX}curld curld ${CURL_LIBRARY_PREFIX}curl curl
PATHS ${CURL_LIBRARY_SEARCH_DEBUG_DIRS} NO_DEFAULT_PATH)

# Configure libraries for debug/release
SET(CURL_INCLUDE_DIRS ${CURL_INCLUDE_DIR} CACHE STRING "Directory containing CURL header files")
SET(CURL_LIBRARY debug ${CURL_LIBRARY_DEBUG} optimized ${CURL_LIBRARY_RELEASE})
SET(CURL_LIBRARIES ${CURL_LIBRARY} CACHE STRING "CURL libraries files")

IF(CURL_INCLUDE_DIR AND CURL_LIBRARY)
	SET(CURL_FOUND TRUE)
ENDIF()

# Hide those variables in GUI
SET(CURL_INCLUDE_DIR ${CURL_INCLUDE_DIR} CACHE INTERNAL "")
SET(CURL_LIBRARY_RELEASE ${CURL_LIBRARY_RELEASE} CACHE INTERNAL "")
SET(CURL_LIBRARY_DEBUG ${CURL_LIBRARY_DEBUG} CACHE INTERNAL "")
SET(CURL_LIBRARY ${CURL_LIBRARY} CACHE INTERNAL "")

# log find result
IF(CURL_FOUND)
	IF(NOT CURL_FIND_QUIETLY)
		MESSAGE(STATUS "  libraries: ${CURL_LIBRARIES}")
		MESSAGE(STATUS "  includes: ${CURL_INCLUDE_DIRS}")
	ENDIF()
ELSE(CURL_FOUND)
	IF(NOT CURL_LIBRARIES)
		MESSAGE(STATUS, "CURL library or one of it dependencies could not be found.")
	ENDIF()
	IF(NOT CURL_INCLUDE_DIRS)
		MESSAGE(STATUS "CURL include files could not be found.")
	ENDIF()
ENDIF(CURL_FOUND)