/*
 *  call-seq:
 *     FSDirectory.new(/path/to/index/, create = false)
 *
 *  Create a new FSDirectory at +/path/to/index/+ which must be a valid path
 *  on your file system. If it doesn't exist it will be created. You can also
 *  specify the +create+ parameter. If +create+ is true the FSDirectory will
 *  be refreshed as new. That is to say, any existing files in the directory
 *  will be deleted. The default value for +create+ is false.
 *
 *  path::   path to index directory. Must be a valid path on your system
 *  create:: set to true if you want any existing files in the directory to be
 *           deleted
 */
static VALUE
frt_fsdir_new(int argc, VALUE *argv, VALUE klass) 
{
    VALUE self, rpath, rcreate;
    Store *store;
    bool create;

    rb_scan_args(argc, argv, "11", &rpath, &rcreate);
    StringValue(rpath);
    create = RTEST(rcreate);
    if (create) {
        frt_create_dir(rpath);
    }
    if (!rb_funcall(rb_cFile, id_is_directory, 1, rpath)) {
        rb_raise(rb_eIOError, "No directory <%s> found. Use :create => true"
                 " to create one.", rs2s(rpath));
    }
    store = open_fs_store(rs2s(rpath));
    if (create) store->clear_all(store);
    if ((self = object_get(store)) == Qnil) {
        self = Data_Wrap_Struct(klass, NULL, &frt_dir_free, store);
        object_add(store, self);
        rb_ivar_set(self, id_ref_cnt, INT2FIX(0));
    }
    else {
        int ref_cnt = FIX2INT(rb_ivar_get(self, id_ref_cnt)) + 1;
        rb_ivar_set(self, id_ref_cnt, INT2FIX(ref_cnt));
        DEREF(store);
    }
    return self;
}