#include <stdlib.h>
#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "common.h"
#include "io.h"
#include "index.h"

typedef struct {
    char*    index;
    unsigned d;
    unsigned argc;
} opts_t;

opts_t options;

void init_args(int args, char* argv[])
{
    optparse_t opts;
    optparse_init(&opts, argv);

    for (char opt; opt != -1; opt = optparse(&opts, "qv")) {
        switch (opt) {
            case 'q':
                verbosity--;
                break;
            case 'v':
                verbosity++;
                break;
        }
    }

    char* argument;

    options.index = optparse_arg(&opts);
   
    if ((argument = optparse_arg(&opts))) {
        options.d  = strtoul(argument, NULL, 0);
    } else {
        options.d = 10;
    }
    
    options.argc  = opts.optind;
}

void help(const char* name) {
    printf(
        "Usage:\n"
        "\t %s index [d]\n"
        ,
        name
    );
}

int main(int argc, char* argv[])
{
    init_args(argc, argv);

    if (options.argc < 2) {
        help(argv[0]);
        return 0;
    }

    btree_t tree;
    btree_init(&tree, options.index, options.d);
    btree_close(&tree);

    return EXIT_SUCCESS;
}