#include <svn_client.h>
#include <svn_auth.h>
#include <svn_ra.h>

static svn_error_t *
add_file(const char *path,
         void *parent_baton,
         const char *copyfrom_path,
         svn_revnum_t copyfrom_revision,
         apr_pool_t *pool,
         void **file_baton) {
    if (copyfrom_path) {
        fprintf(stderr, "entered file     \t\t\t%s\t\t\t%s\t%d\n", path, copyfrom_path, copyfrom_revision);
    }
    return SVN_NO_ERROR;
}

static svn_error_t *
auth_callback(svn_auth_cred_username_t **cred, void *baton, const char *realm, svn_boolean_t may_save, apr_pool_t *pool) {
    if (cred) {
        svn_auth_cred_username_t *ret = apr_pcalloc (pool, sizeof (*ret));
        ret->username = apr_pstrdup(pool, "username");
        *cred = ret;                                                                                                                                                                                       
    }                                                                                                                                                                                                      
    return SVN_NO_ERROR;                                                                                                                                                                                   
}                                                                                                                                                                                                          
                                                                                                                                                                                                           
int main(int argc, char **argv) {                                                                                                                                                                          
    apr_pool_t* pool;                                                                                                                                                                                      
    const char* url = "file:///root/tablib";                                                                                                                                                               
                                                                                                                                                                                                           
    apr_pool_initialize();                                                                                                                                                                                 
    apr_pool_create_ex(&pool, NULL, NULL, NULL);                                                                                                                                                           
                                                                                                                                                                                                           
    // initialize remote access API                                                                                                                                                                        
    svn_ra_initialize(pool);                                                                                                                                                                               
                                                                                                                                                                                                           
    svn_ra_callbacks2_t* callbacks;                                                                                                                                                                        
    svn_ra_create_callbacks(&callbacks, pool);                                                                                                                                                             

    svn_ra_session_t* session;
    svn_error_t* error = svn_ra_open3(&session, url, NULL, callbacks, NULL, NULL, pool);

    if (!error) {
        const svn_ra_reporter3_t* update_reporter;
        void* reporter_baton;

        // revision to list (SVN_INVALID_REVNUM means HEAD revision)
        svn_revnum_t revision = SVN_INVALID_REVNUM;

        // setup our editor
        svn_delta_editor_t *editor = svn_delta_default_editor(pool);
        editor->add_file = add_file;

        // run update call
        svn_ra_do_update2(session, &update_reporter, &reporter_baton, 50, "", svn_depth_infinity, TRUE, editor, NULL, pool);

        // report our virtual working copy as empty (start_empty=TRUE)
        update_reporter->set_path(reporter_baton, "", 50, svn_depth_infinity, TRUE, NULL, pool);
        update_reporter->finish_report(reporter_baton, pool);
    } else {
        fprintf(stderr, "Unable to open connection to %s: %s\n", url, error->message);
    }

    apr_pool_destroy(pool);
    apr_pool_terminate();
    return 0;
}

