/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2011 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <event.h>
#include <libcouchbase/couchbase.h>

static void error_callback(libcouchbase_t instance,
                           libcouchbase_error_t error,
                           const char *errinfo)
{
    fprintf(stderr, "Error %d \n", error);
    exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
    (void)argc; (void)argv;

    char *host = "192.168.0.103:8091";
    char *username = "admin";
    char *passwd = "password";
    char *bucket = "default";
    
    struct libcouchbase_io_opt_st *io;
    libcouchbase_t instance;
    libcouchbase_error_t ret;

    io = libcouchbase_create_io_ops(LIBCOUCHBASE_IO_OPS_DEFAULT, NULL, NULL);
    
    if (io == NULL) {
        fprintf(stderr, "Failed to create IO instance\n");
        return 1;
    }
    
    instance = libcouchbase_create(host, username,
                                   passwd, bucket, io);

    if (instance == NULL) {
        fprintf(stderr, "Failed to create libcouchbase instance\n");
        return 1;
    }
    
    (void)libcouchbase_set_error_callback(instance, error_callback);
    
    ret = libcouchbase_connect(instance);
    if (ret != LIBCOUCHBASE_SUCCESS) {
        fprintf(stderr, "Failed to connect libcouchbase instance to server: %s\n",
                libcouchbase_strerror(instance, ret));
        return 1;
    }
    
    // Wait for the connect to compelete
    libcouchbase_wait(instance);
    
    //do a simple store operation, this will prevent bug where wait blocks forever below
    //libcouchbase_store(instance, "123", LIBCOUCHBASE_SET, "123", 3, "456", 3, 0, 0, 0);
                               
    // Wait for the operation to compelete
    libcouchbase_wait(instance);
    
    fprintf(stdout, "Test finished\n");
    
    return 0;
}
