function(VENDOR_CHECK_DOWNLOAD EXEC)
    if(MSVC OR WIN32)
        set(PLATFORM "windows")
        set(EXTENSION ".exe")
    else()
        set(EXTENSION "")
        if(APPLE)
            set(PLATFORM "osx")
        else()
            set(PLATFORM "linux")
        endif()
    endif()

    if(NOT EXISTS ${PROJECT_BINARY_DIR}/vendor/bin/${EXEC}${EXTENSION})
        if(APPLE)
            set(ARCH "x86_64")
        else()
            include(CheckTypeSize)
            check_type_size("void*" SIZEOF_VOID_P BUILTIN_TYPES_ONLY)
            if(SIZEOF_VOID_P EQUAL 8)
                set(ARCH "x86_64")
            else()
                set(ARCH "x86")
            endif()
        endif()
        set(DOWNLOAD_URL "https://sourceforge.net/projects/xeth/files/${PLATFORM}/${ARCH}/${EXEC}${EXTENSION}/download")
        file(DOWNLOAD ${DOWNLOAD_URL} ${PROJECT_BINARY_DIR}/download/${EXEC}${EXTENSION} SHOW_PROGRESS)
        file(COPY ${PROJECT_BINARY_DIR}/download/${EXEC}${EXTENSION} DESTINATION ${PROJECT_BINARY_DIR}/vendor/bin FILE_PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE NO_SOURCE_PERMISSIONS)
        file(REMOVE ${PROJECT_BINARY_DIR}/download/${EXEC}${EXTENSION})
    endif()

endfunction(VENDOR_CHECK_DOWNLOAD)

function(CHECK_BIN EXEC HINTS)
    if(MSVC OR WIN32)
        set(EXTENSION ".exe")
        
    else()
        set(EXTENSION "")
    endif()

    find_program(${EXEC}_PATH ${EXEC}${EXTENSION} HINTS ${HINTS})

    if(${EXEC}_PATH)
        set(${EXEC}_FOUND 1 PARENT_SCOPE)
    else()
        set(${EXEC}_FOUND 0 PARENT_SCOPE)
    endif()
endfunction(CHECK_BIN)

function(CHECK_BIN_PATH EXEC EXEC_PATH)
    if(MSVC OR WIN32)
        set(EXTENSION ".exe")
    else()
        set(EXTENSION "")
    endif()

    if(NOT EXISTS ${EXEC_PATH})
        message(FATAL_ERROR "${EXEC} not found in specified path (${EXEC_PATH})")
    endif()

endfunction(CHECK_BIN_PATH)



function(REQUIRE_VENDOR_BIN EXEC EXEC_PATH HINTS)
    string(TOUPPER ${EXEC} EXEC_UPPER)
    if(EXEC_PATH)
        CHECK_BIN_PATH(${EXEC} ${EXEC_PATH})
        add_definitions(-D__${EXEC_UPPER}_PATH__="${EXEC_PATH}")
    else()
        CHECK_BIN(${EXEC} "${HINTS}")
        if(NOT ${EXEC}_FOUND)
            if(BIN_DOWNLOAD)
                VENDOR_CHECK_DOWNLOAD(${EXEC})
            else()
                message(FATAL_ERROR "${EXEC} not found, please use -D${EXEC_UPPER}_PATH=... or -DBIN_DOWNLOAD=1 flags")
            endif()
        endif()
    endif()
endfunction(REQUIRE_VENDOR_BIN)
