# Copyright 2015-2017 AeonGames, Rodrigo Hernandez
# Licensed under the terms of the Apache 2.0 License.
function(gitclone repo path)
    if(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
        message(STATUS "Cloning ${repo}, please wait")
        execute_process(COMMAND ${GIT_EXECUTABLE} clone ${repo} ${path} WORKING_DIRECTORY "${BUILD_DIRECTORY}" RESULT_VARIABLE git_result OUTPUT_VARIABLE git_output ERROR_VARIABLE git_output)
        if(NOT git_result EQUAL 0)
            MESSAGE(FATAL_ERROR "Cloning ${repo} failed.\nResult: ${git_result}\nOutput: ${git_output}")
        endif(NOT git_result EQUAL 0)
    endif(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
endfunction(gitclone repo path)

function(gitclonetag repo path tag)
    if(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
        message(STATUS "Cloning ${repo}, please wait")
        execute_process(COMMAND ${GIT_EXECUTABLE} clone -b ${tag} ${repo} ${path} WORKING_DIRECTORY "${BUILD_DIRECTORY}" RESULT_VARIABLE git_result OUTPUT_VARIABLE git_output ERROR_VARIABLE git_output)
        if(NOT git_result EQUAL 0)
            MESSAGE(FATAL_ERROR "Cloning ${repo} failed.\nResult: ${git_result}\nOutput: ${git_output}")
        endif(NOT git_result EQUAL 0)
    endif(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
endfunction(gitclonetag repo path tag)

function(gitclonecommit repo path commit)
    if(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
        message(STATUS "Cloning ${repo}, please wait")
        execute_process(COMMAND ${GIT_EXECUTABLE} clone ${repo} ${path} WORKING_DIRECTORY "${BUILD_DIRECTORY}" RESULT_VARIABLE git_result OUTPUT_VARIABLE git_output ERROR_VARIABLE git_output)
        if(NOT git_result EQUAL 0)
            MESSAGE(FATAL_ERROR "Cloning ${repo} failed.\nResult: ${git_result}\nOutput: ${git_output}")
        endif(NOT git_result EQUAL 0)
        execute_process(COMMAND ${GIT_EXECUTABLE} checkout ${commit} WORKING_DIRECTORY "${BUILD_DIRECTORY}/${path}" RESULT_VARIABLE git_result OUTPUT_VARIABLE git_output ERROR_VARIABLE git_output)
        if(NOT git_result EQUAL 0)
            MESSAGE(FATAL_ERROR "Cloning ${repo} failed.\nResult: ${git_result}\nOutput: ${git_output}")
        endif(NOT git_result EQUAL 0)
    endif(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${path}")
endfunction(gitclonecommit repo path commit)

function(download url filename)
    if(NOT EXISTS "${DOWNLOAD_DIRECTORY}/${filename}")
        message(STATUS "Downloading ${url}")
        file(DOWNLOAD "${url}" "${DOWNLOAD_DIRECTORY}/${filename}" STATUS download_status SHOW_PROGRESS)
        list(GET download_status 0 download_status_code)
        list(GET download_status 1 download_status_desc)
        if(download_status_code)
            file(REMOVE "${DOWNLOAD_DIRECTORY}/${filename}")
            message(FATAL_ERROR "Download failed\nError Code: ${download_status_code}\nMessage: ${download_status_desc}")
        endif()
        message(STATUS "Done downloading ${filename}")
    endif()
endfunction(download url filename)

function(decompress filename directory)
    if(NOT IS_DIRECTORY "${BUILD_DIRECTORY}")
        file(MAKE_DIRECTORY "${BUILD_DIRECTORY}")
    endif()
    if(NOT IS_DIRECTORY "${BUILD_DIRECTORY}/${directory}")
        message(STATUS "Extracting ${filename} into ${BUILD_DIRECTORY}")
        execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzvf "${DOWNLOAD_DIRECTORY}/${filename}" WORKING_DIRECTORY "${BUILD_DIRECTORY}" RESULT_VARIABLE extract_result)
        if(extract_result)
            message(FATAL_ERROR "Extracting Failed with error code: ${extract_result}")
        endif()
    endif()
endfunction(decompress filename directory)
