CMAKE_MINIMUM_REQUIRED( VERSION 3.0 )

if(HUMMINGBIRD_DOWNLOADS_SILENT)
    SET(HUMMINGBIRD_GIT_PROGRESS FALSE)
    SET(HUMMINGBIRD_DOWNLOAD_NO_PROGRESS 1)
    SET(HUMMINGBIRD_FILE_DOWNLOAD_SHOW_PROGRESS FALSE)
else()
    SET(HUMMINGBIRD_GIT_PROGRESS TRUE)
    SET(HUMMINGBIRD_DOWNLOAD_NO_PROGRESS 0)
    SET(HUMMINGBIRD_FILE_DOWNLOAD_SHOW_PROGRESS TRUE)
endif()

MESSAGE("HUMMINGBIRD_DOWNLOADS_SILENT: " ${HUMMINGBIRD_DOWNLOADS_SILENT})
MESSAGE("HUMMINGBIRD_GIT_PROGRESS: " ${HUMMINGBIRD_GIT_PROGRESS})
MESSAGE("HUMMINGBIRD_DOWNLOAD_NO_PROGRESS: " ${HUMMINGBIRD_DOWNLOAD_NO_PROGRESS})
MESSAGE("HUMMINGBIRD_FILE_DOWNLOAD_SHOW_PROGRESS: " ${HUMMINGBIRD_FILE_DOWNLOAD_SHOW_PROGRESS})

include(ExternalProject)

find_package(Git REQUIRED)

macro(HUMMINGBIRD_DOWNLOAD_FILE NAME URL FILE)
    if(NOT EXISTS ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}/${FILE})
        if(HUMMINGBIRD_FILE_DOWNLOAD_SHOW_PROGRESS)
            file(DOWNLOAD ${URL} ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}/${FILE} SHOW_PROGRESS)
        else()
            file(DOWNLOAD ${URL} ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}/${FILE})
        endif()
    endif()
endmacro()

macro(HUMMINGBIRD_DOWNLOAD_URL NAME URL)
    ExternalProject_Add(${NAME}_download PREFIX ${NAME}
        SOURCE_DIR ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}
        
        URL ${URL}
        DOWNLOAD_NO_PROGRESS ${HUMMINGBIRD_DOWNLOAD_NO_PROGRESS}

        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        UPDATE_COMMAND ""
        INSTALL_COMMAND ""
    )
    
    add_library(${NAME} STATIC IMPORTED)
endmacro()

macro(HUMMINGBIRD_DOWNLOAD_URL_HASH NAME URL HASH_ALGO HASH)
    ExternalProject_Add(${NAME}_download PREFIX ${NAME}
        SOURCE_DIR ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}
        
        URL ${URL}
        URL_HASH ${HASH_ALGO}=${HASH}
        DOWNLOAD_NO_PROGRESS ${HUMMINGBIRD_DOWNLOAD_NO_PROGRESS}

        CONFIGURE_COMMAND ""
        BUILD_COMMAND ""
        UPDATE_COMMAND ""
        INSTALL_COMMAND ""
    )
    
    add_library(${NAME} STATIC IMPORTED)
endmacro()

macro(HUMMINGBIRD_GIT_CLONE NAME REPOSITORY)
    set(TAG ${ARGN})
    
    list(LENGTH TAG EXIST_TAG)
    
    if(${EXIST_TAG} GREATER 0)
        ExternalProject_Add( "${NAME}_${TAG}_download" PREFIX ${NAME}
            SOURCE_DIR ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}
            
            GIT_REPOSITORY ${REPOSITORY}
            GIT_TAG ${TAG}
            GIT_PROGRESS ${HUMMINGBIRD_GIT_PROGRESS}

            CONFIGURE_COMMAND ""
            BUILD_COMMAND ""
            UPDATE_COMMAND ""
            INSTALL_COMMAND ""
        )
    else()
        ExternalProject_Add(${NAME}_download PREFIX ${NAME}
            SOURCE_DIR ${HUMMINGBIRD_DEPENDENCIES_SOURCE_DIRECTORY}/${NAME}
            
            GIT_REPOSITORY ${REPOSITORY}
            GIT_PROGRESS ${HUMMINGBIRD_GIT_PROGRESS}

            CONFIGURE_COMMAND ""
            BUILD_COMMAND ""
            UPDATE_COMMAND ${GIT_EXECUTABLE} pull
            INSTALL_COMMAND ""
            )
    endif()
    
    add_library(${NAME} STATIC IMPORTED)
endmacro()