message(STATUS "variable OPENSSL_FOUND=${OPENSSL_FOUND}")
message(STATUS "variable OPENSSL_VERSION=${OPENSSL_VERSION}")
message(STATUS "variable OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}")
message(STATUS "variable OPENSSL_LIBRARIES=${OPENSSL_LIBRARIES}")
message(STATUS "variable OPENSSL_CRYPTO_LIBRARY=${OPENSSL_CRYPTO_LIBRARY}")
message(STATUS "variable OPENSSL_SSL_LIBRARY=${OPENSSL_SSL_LIBRARY}")

if(NOT TARGET OpenSSL::SSL)
    message(STATUS "OpenSSL::SSL target not defined. Creating IMPORTED target.")

    # old cmake version which only has OPENSSL_LIBRARIES
    # first library is ssl, second is crypto
    if(NOT OPENSSL_SSL_LIBRARY)
        set(OPENSSL_SSL_LIBRARY    list(GET OPENSSL_LIBRARIES 0))
        set(OPENSSL_CRYPTO_LIBRARY list(GET OPENSSL_LIBRARIES 1))
    endif(NOT OPENSSL_SSL_LIBRARY)

    add_library(OpenSSL::Crypto SHARED IMPORTED GLOBAL)
    set_target_properties(OpenSSL::Crypto PROPERTIES
        IMPORTED_LOCATION ${OPENSSL_CRYPTO_LIBRARY}
        INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR}
    )

    add_library(OpenSSL::SSL SHARED IMPORTED GLOBAL)
    set_target_properties(OpenSSL::SSL PROPERTIES
        IMPORTED_LOCATION ${OPENSSL_SSL_LIBRARY}
        INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR}
        INTERFACE_LINK_LIBRARIES OpenSSL::Crypto
    )
endif(NOT TARGET OpenSSL::SSL)
