#[[

Create imported targets:
  * PIP - main library
  * PIP::USB
  * PIP::Console
  * PIP::Crypt
  * PIP::Compress
  * PIP::FFTW
  * PIP::OpenCL
  * PIP::IOUtils
  * PIP::Cloud
  * PIP::Lua

These targets include directories and depends on
main library

]]

cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
include(SHSTKMacros)

shstk_set_find_dirs(pip)
if(PIP_DIR)
	list(APPEND pip_LIBDIR "${PIP_DIR}/lib")
	list(APPEND pip_INCDIR "${PIP_DIR}/include/pip")
	list(APPEND pip_BINDIR "${PIP_DIR}/bin")
endif()

set(__libs "usb;crypt;console;fftw;compress;io_utils;opencl;cloud;lua")

if (BUILDING_pip)
	#set(_libs "pip;pip_usb;pip_console;pip_crypt;pip_fftw;pip_compress;pip_opencl;pip_io_utils;pip_cloud;pip_lua")
	#set(_bins "pip_cmg;pip_rc;deploy_tool")
	#get_target_property(_path pip BINARY_DIR)
	#get_target_property(_path pip LIBRARY_OUTPUT_NAME)
	#message("${_path}")
	#set(PIP_LIBRARY          "$<TARGET_FILE_DIR:pip>/$<TARGET_FILE_NAME:pip>"                   CACHE STRING "")	
	set(PIP_LIBRARY pip CACHE STRING "")	
	
	set(PIP_FOUND ON CACHE BOOL "")
else()
	find_library(PIP_LIBRARY pip HINTS ${pip_LIBDIR})
	foreach (_l ${__libs})
		find_library(PIP_LIBRARY_${_l} pip_${_l} HINTS ${pip_LIBDIR})
	endforeach()
endif()
	
if (BUILDING_pip AND (NOT CMAKE_CROSSCOMPILING))
	set(PIP_CMG          "$<TARGET_FILE_DIR:pip_cmg>/$<TARGET_FILE_NAME:pip_cmg>"          CACHE STRING "")
	set(PIP_RC           "$<TARGET_FILE_DIR:pip_rc>/$<TARGET_FILE_NAME:pip_rc>"            CACHE STRING "")
	set(PIP_DEPLOY_TOOL  "$<TARGET_FILE_DIR:deploy_tool>/$<TARGET_FILE_NAME:deploy_tool>"  CACHE STRING "")
else()
	find_program(PIP_CMG pip_cmg${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
	find_program(PIP_RC pip_rc${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
	find_program(PIP_DEPLOY_TOOL deploy_tool${pip_BINEXT} HINTS ${pip_BINDIR} ${pip_FIND_PROGRAM_ARG})
endif()
if (NOT PIP_LIBRARY)
	if(PIP_FIND_REQUIRED)
		message(FATAL_ERROR "Can`t find PIP library!")
	endif()
	return()
endif()
set(_PIP_LIBRARY_PATH_ "${PIP_LIBRARY}")
set(_PIP_ADD_LIBS_ "")
if (NOT BUILDING_pip)
	if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
		find_library(DL_LIBRARY dl)
		list(APPEND PIP_LIBRARY ${DL_LIBRARY})
		if (NOT DEFINED ANDROID_PLATFORM)
			find_library(PTHREAD_LIBRARY pthread)
			find_library(UTIL_LIBRARY util)
			set(_PIP_ADD_LIBS_ ${PTHREAD_LIBRARY} ${UTIL_LIBRARY})
			list(APPEND PIP_LIBRARY ${_PIP_ADD_LIBS_})
		endif()
	endif()
endif()

if (NOT BUILDING_pip)
	shstk_find_header(pip PIP "pip_version.h" "${_PIP_LIBRARY_PATH_}")
endif()
if(PIP_FIND_VERSION VERSION_GREATER PIP_VERSION)
	message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!")
endif()

set(__module_usb       USB      )
set(__module_console   Console  )
set(__module_crypt     Crypt    )
set(__module_fftw      FFTW     )
set(__module_compress  Compress )
set(__module_opencl    OpenCL   )
set(__module_io_utils  IOUtils  )
set(__module_cloud     Cloud    )
set(__module_lua       Lua      )

foreach (_l ${__libs})
	set( __inc_${_l} "")
	set(__deps_${_l} "")
	set(__libs_${_l} "")
endforeach()

set(__deps_io_utils  "PIP::Crypt")
set(__deps_cloud     "PIP::IOUtils")


if (BUILDING_pip)

	if (NOT SET_TARGETS_pip)
		set(SET_TARGETS_pip ON CACHE BOOL "")
		#message("create aliases")
		if((NOT TARGET PIP) AND PIP_LIBRARY)
			#message("alias PIP = pip")
			add_library(PIP ALIAS pip)
		endif()
		foreach (_l ${__libs})
			set(_m ${__module_${_l}})
			if((NOT TARGET PIP::${_m}) AND TARGET pip_${_l})
				#message("alias PIP::${_m} = pip_${_l}")
				add_library(PIP::${_m} ALIAS pip_${_l})
			endif()
		endforeach()
	endif()
	
else()

	#message("create interfaces")
	if((NOT TARGET PIP) AND PIP_LIBRARY)
		add_library(PIP UNKNOWN IMPORTED)
		set_target_properties(PIP PROPERTIES
			IMPORTED_LOCATION "${_PIP_LIBRARY_PATH_}"
			INTERFACE_INCLUDE_DIRECTORIES "${pip_INCLUDES}"
			INTERFACE_LINK_LIBRARIES "${_PIP_ADD_LIBS_}")
		#message("imported PIP = ${PIP_LIBRARY}")
	endif()
	foreach (_l ${__libs})
		set(_m ${__module_${_l}})
		if((NOT TARGET PIP::${_m}) AND PIP_LIBRARY_${_l})
			add_library(PIP::${_m} UNKNOWN IMPORTED)
			set_target_properties(PIP::${_m} PROPERTIES
				IMPORTED_LOCATION "${PIP_LIBRARY_${_l}}"
				INTERFACE_INCLUDE_DIRECTORIES "${__inc_${_l}}"
				INTERFACE_LINK_LIBRARIES "PIP;${__deps_${_l}};${__libs_${_l}}")
			#message("imported PIP::${_m} = ${PIP_LIBRARY_${_l}} ${__deps_${_l}} ${__libs_${_l}} ${__inc_${_l}}")
		endif()
	endforeach()
	
endif()

set(PIP_FOUND ON CACHE BOOL "")

include(PIPMacros)

