cmake_minimum_required (VERSION 2.6...3.10.2)
project (CGenerator C)

cmake_policy(SET CMP0063 NEW)

set(CMAKE_C_VISIBILITY_PRESET default)
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=missing-declarations")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=int-conversion")

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

find_package(OpenSSL)

if (OPENSSL_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DOPENSSL")
    if(CMAKE_VERSION VERSION_LESS 3.4)
        include_directories(${OPENSSL_INCLUDE_DIR})
        include_directories(${OPENSSL_INCLUDE_DIRS})
        link_directories(${OPENSSL_LIBRARIES})
    endif()
endif()

set(pkgName "koldan")

# this default version can be overridden in PreTarget.cmake
set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 1)

if( (DEFINED CURL_INCLUDE_DIR) AND (DEFINED CURL_LIBRARIES))
    include_directories(${CURL_INCLUDE_DIR})
    set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
else()
    find_package(CURL 7.58.0 REQUIRED)
    if(CURL_FOUND)
        include_directories(${CURL_INCLUDE_DIR})
        set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
    endif()
endif()

set(SRCS
    src/list.c
    src/apiKey.c
    src/apiClient.c
    src/binary.c
    external/cJSON.c
    model/object.c
    model/alias_type.c
    model/api_key_create_request.c
    model/api_key_response.c
    model/api_key_revoke_request.c
    model/effective_permissions_response.c
    model/files_filter.c
    model/files_sort.c
    model/language_dto.c
    model/language_update_dto.c
    model/listening_audio_filter.c
    model/listening_audio_response.c
    model/listening_audio_status.c
    model/login_request.c
    model/logout_request.c
    model/model_alias_response.c
    model/model_capabilities_response.c
    model/model_languages_response.c
    model/model_status.c
    model/o_auth_token_response.c
    model/oidc_provider_info.c
    model/quota_entry.c
    model/quota_limits_response.c
    model/quotas_response.c
    model/rate_limits_response.c
    model/refresh_token_request.c
    model/retention_policy_response.c
    model/shared_files_filter.c
    model/site_capabilities_response.c
    model/site_properties_response.c
    model/speech_service_channel_mapping.c
    model/speech_service_diarization_options.c
    model/speech_service_file_access_type.c
    model/speech_service_file_response.c
    model/speech_service_file_share_list_response.c
    model/speech_service_file_share_request.c
    model/speech_service_file_share_response.c
    model/speech_service_file_update_request.c
    model/speech_service_files_list_response.c
    model/speech_service_quota_limits.c
    model/speech_service_quotas.c
    model/speech_service_rate_limits.c
    model/speech_service_retention.c
    model/speech_service_session_list_response.c
    model/speech_service_session_response.c
    model/speech_service_session_segment_response.c
    model/speech_service_shared_file_response.c
    model/speech_service_shared_files_list_response.c
    model/speech_service_summary_error.c
    model/speech_service_summary_filter.c
    model/speech_service_summary_list_response.c
    model/speech_service_summary_request.c
    model/speech_service_summary_response.c
    model/speech_service_summary_result.c
    model/speech_service_summary_sort.c
    model/speech_service_summary_token_usage.c
    model/speech_service_transcription_job_error.c
    model/speech_service_transcription_job_request.c
    model/speech_service_transcription_job_response.c
    model/speech_service_transcription_job_result.c
    model/speech_service_transcription_jobs_list_response.c
    model/speech_service_transcription_language_options.c
    model/speech_service_transcription_options.c
    model/speech_service_transcription_segment.c
    model/speech_service_upload_and_transcribe_response.c
    model/speech_service_webhook.c
    model/speech_service_word_timing.c
    model/subscription_plan_response.c
    model/subscription_plan_summary.c
    model/summary_status.c
    model/tag_list_response.c
    model/tag_response.c
    model/tag_validation_request.c
    model/tag_validation_response.c
    model/text_service_quota_limits.c
    model/text_service_quotas.c
    model/text_service_rate_limits.c
    model/text_service_retention.c
    model/transcription_job_status.c
    model/transcription_jobs_filter.c
    model/transcription_jobs_sort.c
    model/unified_login_request.c
    model/user_card_response.c
    model/user_lookup_search_response.c
    model/user_subscription_response.c
    model/users_service_rate_limits.c
    api/APIKeysAPI.c
    api/AuthenticationAPI.c
    api/DataRetentionAPI.c
    api/LanguagesAPI.c
    api/MySubscriptionAPI.c
    api/PermissionsEffectiveAPI.c
    api/QuotasAPI.c
    api/RateLimitsAPI.c
    api/SitePropertiesAPI.c
    api/SpeechFilesAPI.c
    api/SpeechFilesSharingAPI.c
    api/SpeechModelsAPI.c
    api/SpeechStreamingSessionsAPI.c
    api/SpeechSummariesAPI.c
    api/SpeechTranscriptionsAPI.c
    api/SubscriptionsPlansAPI.c
    api/TagsAPI.c
    api/UsersLookupAPI.c

)

set(HDRS
    include/apiClient.h
    include/list.h
    include/binary.h
    include/keyValuePair.h
    external/cJSON.h
    model/object.h
    model/any_type.h
    model/alias_type.h
    model/api_key_create_request.h
    model/api_key_response.h
    model/api_key_revoke_request.h
    model/effective_permissions_response.h
    model/files_filter.h
    model/files_sort.h
    model/language_dto.h
    model/language_update_dto.h
    model/listening_audio_filter.h
    model/listening_audio_response.h
    model/listening_audio_status.h
    model/login_request.h
    model/logout_request.h
    model/model_alias_response.h
    model/model_capabilities_response.h
    model/model_languages_response.h
    model/model_status.h
    model/o_auth_token_response.h
    model/oidc_provider_info.h
    model/quota_entry.h
    model/quota_limits_response.h
    model/quotas_response.h
    model/rate_limits_response.h
    model/refresh_token_request.h
    model/retention_policy_response.h
    model/shared_files_filter.h
    model/site_capabilities_response.h
    model/site_properties_response.h
    model/speech_service_channel_mapping.h
    model/speech_service_diarization_options.h
    model/speech_service_file_access_type.h
    model/speech_service_file_response.h
    model/speech_service_file_share_list_response.h
    model/speech_service_file_share_request.h
    model/speech_service_file_share_response.h
    model/speech_service_file_update_request.h
    model/speech_service_files_list_response.h
    model/speech_service_quota_limits.h
    model/speech_service_quotas.h
    model/speech_service_rate_limits.h
    model/speech_service_retention.h
    model/speech_service_session_list_response.h
    model/speech_service_session_response.h
    model/speech_service_session_segment_response.h
    model/speech_service_shared_file_response.h
    model/speech_service_shared_files_list_response.h
    model/speech_service_summary_error.h
    model/speech_service_summary_filter.h
    model/speech_service_summary_list_response.h
    model/speech_service_summary_request.h
    model/speech_service_summary_response.h
    model/speech_service_summary_result.h
    model/speech_service_summary_sort.h
    model/speech_service_summary_token_usage.h
    model/speech_service_transcription_job_error.h
    model/speech_service_transcription_job_request.h
    model/speech_service_transcription_job_response.h
    model/speech_service_transcription_job_result.h
    model/speech_service_transcription_jobs_list_response.h
    model/speech_service_transcription_language_options.h
    model/speech_service_transcription_options.h
    model/speech_service_transcription_segment.h
    model/speech_service_upload_and_transcribe_response.h
    model/speech_service_webhook.h
    model/speech_service_word_timing.h
    model/subscription_plan_response.h
    model/subscription_plan_summary.h
    model/summary_status.h
    model/tag_list_response.h
    model/tag_response.h
    model/tag_validation_request.h
    model/tag_validation_response.h
    model/text_service_quota_limits.h
    model/text_service_quotas.h
    model/text_service_rate_limits.h
    model/text_service_retention.h
    model/transcription_job_status.h
    model/transcription_jobs_filter.h
    model/transcription_jobs_sort.h
    model/unified_login_request.h
    model/user_card_response.h
    model/user_lookup_search_response.h
    model/user_subscription_response.h
    model/users_service_rate_limits.h
    api/APIKeysAPI.h
    api/AuthenticationAPI.h
    api/DataRetentionAPI.h
    api/LanguagesAPI.h
    api/MySubscriptionAPI.h
    api/PermissionsEffectiveAPI.h
    api/QuotasAPI.h
    api/RateLimitsAPI.h
    api/SitePropertiesAPI.h
    api/SpeechFilesAPI.h
    api/SpeechFilesSharingAPI.h
    api/SpeechModelsAPI.h
    api/SpeechStreamingSessionsAPI.h
    api/SpeechSummariesAPI.h
    api/SpeechTranscriptionsAPI.h
    api/SubscriptionsPlansAPI.h
    api/TagsAPI.h
    api/UsersLookupAPI.h

)

include(PreTarget.cmake OPTIONAL)

set(PROJECT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

# Add library with project file with project name as library name
if(NOT BUILD_STATIC_LIBS)
  add_library(${pkgName} ${SRCS} ${HDRS})
else()
  add_library(${pkgName} STATIC ${SRCS} ${HDRS})
endif()
# Link dependent libraries
if(NOT CMAKE_VERSION VERSION_LESS 3.4)
    target_link_libraries(${pkgName} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
endif()
target_link_libraries(${pkgName} PUBLIC ${CURL_LIBRARIES} )
target_include_directories(
    ${pkgName} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
    $<INSTALL_INTERFACE:include>
)

include(PostTarget.cmake OPTIONAL)

# installation of libraries, headers, and config files
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in)
    install(TARGETS ${pkgName} DESTINATION lib)
else()
    include(GNUInstallDirs)
    install(TARGETS ${pkgName} DESTINATION lib EXPORT ${pkgName}Targets)

    foreach(HDR_FILE ${HDRS})
        get_filename_component(HDR_DIRECTORY ${HDR_FILE} DIRECTORY)
        get_filename_component(ABSOLUTE_HDR_DIRECTORY ${HDR_DIRECTORY} ABSOLUTE)
        file(RELATIVE_PATH RELATIVE_HDR_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${ABSOLUTE_HDR_DIRECTORY})
        install(FILES ${HDR_FILE} DESTINATION include/${pkgName}/${RELATIVE_HDR_PATH})
    endforeach()

    include(CMakePackageConfigHelpers)
    write_basic_package_version_file(
            "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake"
            VERSION "${PROJECT_VERSION_STRING}"
            COMPATIBILITY AnyNewerVersion
    )

    export(EXPORT ${pkgName}Targets
            FILE "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Targets.cmake"
            NAMESPACE ${pkgName}::
            )

    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
            "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake"
            @ONLY
            )

    set(ConfigPackageLocation lib/cmake/${pkgName})
    install(EXPORT ${pkgName}Targets
            FILE
            ${pkgName}Targets.cmake
            NAMESPACE
            ${pkgName}::
            DESTINATION
            ${ConfigPackageLocation}
            )
    install(
            FILES
            "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}Config.cmake"
            "${CMAKE_CURRENT_BINARY_DIR}/${pkgName}/${pkgName}ConfigVersion.cmake"
            DESTINATION
            ${ConfigPackageLocation}
    )
endif()

# make installation packages
include(Packing.cmake OPTIONAL)

# Setting file variables to null
set(SRCS "")
set(HDRS "")


## This section shows how to use the above compiled library to compile the source files
## set source files
#set(SRCS
#    unit-tests/manual-APIKeysAPI.c
#    unit-tests/manual-AuthenticationAPI.c
#    unit-tests/manual-DataRetentionAPI.c
#    unit-tests/manual-LanguagesAPI.c
#    unit-tests/manual-MySubscriptionAPI.c
#    unit-tests/manual-PermissionsEffectiveAPI.c
#    unit-tests/manual-QuotasAPI.c
#    unit-tests/manual-RateLimitsAPI.c
#    unit-tests/manual-SitePropertiesAPI.c
#    unit-tests/manual-SpeechFilesAPI.c
#    unit-tests/manual-SpeechFilesSharingAPI.c
#    unit-tests/manual-SpeechModelsAPI.c
#    unit-tests/manual-SpeechStreamingSessionsAPI.c
#    unit-tests/manual-SpeechSummariesAPI.c
#    unit-tests/manual-SpeechTranscriptionsAPI.c
#    unit-tests/manual-SubscriptionsPlansAPI.c
#    unit-tests/manual-TagsAPI.c
#    unit-tests/manual-UsersLookupAPI.c
#)

##set header files
#set(HDRS
#)

## loop over all files in SRCS variable
#foreach(SOURCE_FILE ${SRCS})
#    # Get only the file name from the file as add_executable does not support executable with slash("/")
#    get_filename_component(FILE_NAME_ONLY ${SOURCE_FILE} NAME_WE)
#    # Remove .c from the file name and set it as executable name
#    string( REPLACE ".c" "" EXECUTABLE_FILE ${FILE_NAME_ONLY})
#    # Add executable for every source file in  SRCS
#    add_executable(unit-${EXECUTABLE_FILE} ${SOURCE_FILE})
#    # Link above created library to executable and dependent library curl
#    target_link_libraries(unit-${EXECUTABLE_FILE} ${CURL_LIBRARIES} ${pkgName} )
#endforeach(SOURCE_FILE ${SRCS})
