#INCLUDE( WGExternalProject )
INCLUDE( FindPackageHandleStandardArgs )
INCLUDE( ExternalModuleUrls )

IF( NOT Perforce_FIND_VERSION )
	SET( Perforce_FIND_VERSION "15.2" )
ENDIF()

IF (NOT EXISTS ${PERFORCE_DIRECTORY})
IF( NOT EXISTS ${Perforce_URL} )
SET( Perforce_SUPPORT FALSE )
ENDIF()
ENDIF()

IF( NOT Perforce_SUPPORT )
	MESSAGE(STATUS COMMENT "Skipping Perforce Support, Perforce_SUPPORT is not enabled.")
	SET( PERFORCE_EXISTS FALSE )
	SET( PERFORCE_FOUND FALSE )
	RETURN()	
ENDIF()

SET( PERFORCE_FILES ${WG_TOOLS_THIRD_PARTY_DIR}/Perforce )
SET( PERFORCE_DIRECTORY ${PERFORCE_FILES}/r${Perforce_FIND_VERSION} )

IF( BW_PLATFORM_WINDOWS )
	SET( PLATFORM_SUFFIX w )
ELSE()
	MESSAGE( "Skipping Perforce, currently only windows libraries are available" )
	RETURN()
ENDIF()

IF (NOT EXISTS ${PERFORCE_DIRECTORY})
	FILE( REMOVE_RECURSE "${WG_EXTERNAL_PROJECTS_ROOT}/Perforce" )
	IF( ${CMAKE_VERSION} VERSION_GREATER "3.5")
		WG_FETCH_EXTERNAL_PACKAGE( Perforce ${Perforce_FIND_VERSION}
			GIT_REPOSITORY ${Perforce_URL}
			CONFIGURE_COMMAND ""  
			BUILD_COMMAND "" 
			INSTALL_COMMAND "" 
			GIT_SHALLOW 1
			SOURCE_DIR ${PERFORCE_FILES}
		)
	ELSE()
	WG_FETCH_EXTERNAL_PACKAGE( Perforce ${Perforce_FIND_VERSION}
		GIT_REPOSITORY ${Perforce_URL}
		CONFIGURE_COMMAND ""  
		BUILD_COMMAND "" 
        INSTALL_COMMAND "" 
		SOURCE_DIR ${PERFORCE_FILES}
)
	ENDIF()
ENDIF()


IF( CMAKE_SIZEOF_VOID_P EQUAL 8 )
	SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}/bin.ntx64 )
	SET( BITNESS_SUFFIX 64 )
ELSE()
	SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}/bin.ntx86 )
ENDIF()

SET( OPENSSL_DIR ${WG_TOOLS_THIRD_PARTY_DIR}/perforce/OpenSSL )

IF( EXISTS "${PERFORCE_DIRECTORY}" )
	SET( PERFORCE_EXISTS TRUE )
ELSE()
	SET( PERFORCE_EXISTS FALSE )
ENDIF()

# Same checks as WGQtCommon
# CMAKE_GENERATOR_TOOLSET check is for generating Visual Studio solutions
# CMAKE_LINKER check is for generating QtCreator projects
IF( CMAKE_GENERATOR_TOOLSET STREQUAL "v110_xp"
	OR CMAKE_GENERATOR_TOOLSET STREQUAL "v110"
	OR CMAKE_LINKER MATCHES "Visual Studio 11" )
	SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}/vs2012 )
	SET( PERFORCE_EXISTS FALSE )
ELSEIF( CMAKE_GENERATOR_TOOLSET STREQUAL "v120_xp"
	OR CMAKE_GENERATOR_TOOLSET STREQUAL "v120"
	OR CMAKE_LINKER MATCHES "Visual Studio 12" )
	SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}/vs2013 )
	SET( OPENSSL_DIR ${OPENSSL_DIR}/vs2013 )
ELSEIF( CMAKE_GENERATOR_TOOLSET STREQUAL "v140_xp"
	OR CMAKE_GENERATOR_TOOLSET STREQUAL "v140"
	OR CMAKE_GENERATOR_TOOLSET STREQUAL "v141"
	OR CMAKE_LINKER MATCHES "Visual Studio 14" )
	SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}/vs2015 )
	SET( OPENSSL_DIR ${OPENSSL_DIR}/vs2015 )
ELSE()
	MESSAGE( FATAL_ERROR "Cannot find Perforce binaries built with the ${CMAKE_GENERATOR_TOOLSET} toolset." )
ENDIF()

SET( PERFORCE_DIRECTORY ${PERFORCE_DIRECTORY}_dyn )

# Handle the QUIETLY and REQUIRED arguments and set PERFORCE_FOUND to TRUE
# if all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS( Perforce
	DEFAULT_MSG
	PERFORCE_EXISTS
)

# Only certain commands, like INCLUDE_DIRECTORIES can contain generator
# expressions like $CONFIG:Debug
SET( PERFORCE_CONFIG_DIR ${PERFORCE_DIRECTORY}$<$<CONFIG:Debug>:_debug> )
SET( CONFIG_SUFFIX $<$<CONFIG:Debug>:d>$<$<NOT:$<CONFIG:Debug>>:r> )
SET( LIB_SUFFIX ${PLATFORM_SUFFIX}${BITNESS_SUFFIX}${CONFIG_SUFFIX} )
SET( LIBEAY_PATH ${OPENSSL_DIR}/lib/libeay_${LIB_SUFFIX}.lib )
SET( SSLEAY_PATH ${OPENSSL_DIR}/lib/ssleay_${LIB_SUFFIX}.lib )
SET( PERFORCE_LIB_DIR ${PERFORCE_CONFIG_DIR}/lib )

# Definitions, libraries and include dirs should be output for each FIND_PACKAGE script
# No definitions
SET( PERFORCE_DEFINITIONS )
# No dependencies
SET( PERFORCE_LIBRARIES
	Ws2_32
	${PERFORCE_CONFIG_DIR}/lib/libclient.lib
	${PERFORCE_CONFIG_DIR}/lib/librpc.lib
	${PERFORCE_CONFIG_DIR}/lib/libsupp.lib
	${LIBEAY_PATH}
	${SSLEAY_PATH}
	Advapi32.lib
	gdi32.lib
	User32.lib
)
# Includes
SET( PERFORCE_INCLUDE_DIRS ${PERFORCE_CONFIG_DIR}/include )

MARK_AS_ADVANCED( PERFORCE_EXISTS )

IF( PERFORCE_FOUND )
	MESSAGE( STATUS "Enabled ${PROJECT_NAME}. Perforce found in ${PERFORCE_DIRECTORY}." )
ELSE()
	MESSAGE( STATUS "Disabled ${PROJECT_NAME}. Perforce not found in ${PERFORCE_DIRECTORY}." )
ENDIF()
