// ArchiveDataTool ----------

// System
#include <stdio.h>
#include <string.h>
#include <time.h>

// Base
#include <epicsVersion.h>
// Tools
#include <AutoPtr.h>
#include <ArgParser.h>
#include <BenchTimer.h>
#include <epicsTimeHelper.h>
// Storage
#include <OldDirectoryFile.h>
#include <IndexFile.h>
#include <DataFile.h>
#include <RawDataReader.h>
#include <DataWriter.h>

/*
#define DEBUG
*/

#define MAX_BUFFER_LEN 132
#define TIME_STRING_LEN 19
#define DOUBLE_STRING_LEN 14
#define UNITS_LEN 20
#define MAX_CHANNEL_NAME_LEN 50
#define TIME_OUTPUT_FILE_INTERVAL 100
#define TIME_OUTPUT_STRING_LEN 20

int verbose;

bool do_enforce_off = false;

// TODO: export/import: ASCII or XML
// TODO: delete/rename channel

void show_hash_info(const stdString &index_name)
{
    IndexFile index(3);
    index.open(index_name, true);
    index.showStats(stdout);
}

void count_channel_values(RTree *tree, const stdString &directory,
                          DbrType &type, DbrCount &count, size_t &blocks, size_t &values)
{
    type   = 0;
    count  = 0;
    blocks = 0;
    values = 0;
    if (! tree)
        return;
    DataFile *datafile;
    AutoPtr<DataHeader> header;
    RTree::Datablock block;
    RTree::Node node(tree->getM(), true);
    stdString start, end;
    int idx;
    bool ok;
    for (ok = tree->getFirstDatablock(node, idx, block);
         ok;
         ok = tree->getNextDatablock(node, idx, block))
    {
        ++blocks;
        datafile = DataFile::reference(directory,
                                       block.data_filename,
                                       false);
        header = datafile->getHeader(block.data_offset);
        datafile->release();
        if (header)
        {
            if (blocks == 1)
            {
                type  = header->data.dbr_type;
                count = header->data.dbr_count;
            }
            values += header->data.num_samples;
            header = 0;
        }
        else
            printf("Cannot read header in data file.\n");
    }
}

void list_names(const stdString &index_name, bool channel_detail)
{
    IndexFile index(3);
    IndexFile::NameIterator names;
    epicsTime stime, etime, t0, t1;
    stdString directory, start, end;
    bool ok;
    size_t channels = 0, blocks = 0, values = 0;
    index.open(index_name, true);
    if (channel_detail)
        printf("# Name\tType\tCount\tStart\tEnd\tBlocks\tValues\n");
    for (ok = index.getFirstChannel(names);
         ok;
         ok = index.getNextChannel(names))
    {
        AutoPtr<RTree> tree(index.getTree(names.getName(), directory));
        tree->getInterval(stime, etime);
        if (channels == 0)
        {
            t0 = stime;
            t1 = etime;
        }
        else
        {
            if (t0 > stime)
                t0 = stime;
            if (t1 < etime)
                t1 = etime;
        }
	size_t chan_blocks, chan_values;
        DbrType  type;
        DbrCount count;
        count_channel_values(tree, directory, type, count, chan_blocks, chan_values);
        ++channels;
        blocks += chan_blocks;
        values += chan_values;
	if (channel_detail)
            printf("%s\t%d\t%d\t%s\t%s\t%zu\t%zu\n",
                   names.getName().c_str(),
                   (int) type, (int) count,
                   epicsTimeTxt(stime, start),
                   epicsTimeTxt(etime, end),
                   chan_blocks, chan_values);
    }
    printf("%zu channels, %zu values, %s - %s\n",
           channels, values,
           epicsTimeTxt(t0, start), epicsTimeTxt(t1, end));
}

void list_num_values_channel(const stdString &channel_name,
                             const epicsTime *start, const epicsTime *end,
                             IndexFile &index, RawDataReader &reader,
                             size_t &channel_count, size_t &value_count)
{
    size_t          count = 0;

    const RawValue::Data *value = reader.find(channel_name, start);
    while (value && start && RawValue::getTime(value) < *start)
    {   // Correct for "before-or-at" idea of find()
        if (verbose > 2)
            printf("Skipping sample before start time\n");
        value = reader.next();
    }
    for (/**/; value; value = reader.next())
    {
        if (end  &&  RawValue::getTime(value) >= *end)
            break; // Reached or exceeded end time

        ++count;
    }
    ++channel_count;
    value_count += count;

    printf("%s %d\n", channel_name.c_str(), count);
    fflush(stdout);
}

void list_num_values(const stdString &index_name,
          int RTreeM, const epicsTime *start, const epicsTime *end,
          const stdString &single_name)
{
    IndexFile               index(RTreeM);
    IndexFile::NameIterator names;
    size_t                  channel_count = 0, value_count = 0;
    BenchTimer              timer;
    stdString               dir1, dir2;
    Filename::getDirname(index_name, dir1);

    index.open(index_name, true);
    RawDataReader reader(index);
    if (single_name.empty())
    {
        bool ok = index.getFirstChannel(names);
        while (ok)
        {
            list_num_values_channel(names.getName(), start, end, index, reader,
                                    channel_count, value_count);
            ok = index.getNextChannel(names);
        }    
    }
    else
        list_num_values_channel(single_name, start, end, index, reader,
                     channel_count, value_count);
    index.close();
    timer.stop();
    if (verbose)
    {
        printf("Total: %lu channels, %lu values\n",
               (unsigned long) channel_count, (unsigned long) value_count);
        printf("Runtime: %s\n", timer.toString().c_str());
    }
}


DataHeader *get_dataheader(const stdString &dir, const stdString &file,
                           FileOffset offset)
{
    DataFile *datafile = DataFile::reference(dir, file, false);
    if (!datafile)
        return 0;
    DataHeader *header = datafile->getHeader(offset);
    datafile->release(); // ref'ed by header
    return header; // might be NULL
}

// Try to determine the period and a good guess
// for the number of samples for a channel.
void determine_period_and_samples(IndexFile &index,
                                  const stdString &channel,
                                  double &period, size_t &num_samples)
{
    period      = 1.0; // initial guess
    num_samples = 10;
    // Whatever fails only means that there is no data,
    // so the initial guess remains OK.
    // Otherwise we peek into the last datablock.
    stdString directory;
    AutoPtr<RTree> tree(index.getTree(channel, directory));
    if (!tree)
        return;
    RTree::Node node(tree->getM(), false);
    RTree::Datablock block;
    int i;
    if (!tree->getLastDatablock(node, i, block))
        return;
    AutoPtr<DataHeader> header;
    if (!(header = get_dataheader(directory,
                                  block.data_filename, block.data_offset)))
        return;
    period = header->data.period;
    num_samples = header->data.num_samples;
    if (verbose > 2)
        printf("Last source buffer: Period %g, %lu samples.\n",
               period, (unsigned long) num_samples);
}

// Helper for copy: Handles samples of a single channel
void copy_channel(const stdString &channel_name,
                  const epicsTime *start, const epicsTime *end,
                  IndexFile &index, RawDataReader &reader,
                  IndexFile &new_index,
                  size_t &channel_count, size_t &value_count, size_t &back_count)
{
    double          period = 1.0;
    size_t          num_samples = 4096;    
    AutoPtr<DataWriter> writer;
    RawValue::Data *last_value = 0;
    bool            last_value_set = false;
    size_t          count = 0, back = 0;
    if (verbose > 1)
    {
        printf("Channel '%s': ", channel_name.c_str());
        if (verbose > 3)
            printf("\n");
        fflush(stdout);
    }
    determine_period_and_samples(index, channel_name, period, num_samples);
    const RawValue::Data *value = reader.find(channel_name, start);
    while (value && start && RawValue::getTime(value) < *start)
    {   // Correct for "before-or-at" idea of find()
        if (verbose > 2)
            printf("Skipping sample before start time\n");
        value = reader.next();
    }
    for (/**/; value; value = reader.next())
    {
        if (end  &&  RawValue::getTime(value) >= *end)
            break; // Reached or exceeded end time
        if (!writer || reader.changedType() || reader.changedInfo())
        {   // Need new or different writer
            writer = new DataWriter(
                new_index, channel_name,
                reader.getInfo(), reader.getType(), reader.getCount(),
                period, num_samples);
            RawValue::free(last_value);
            last_value_set = false;
            last_value = RawValue::allocate(reader.getType(),
                                            reader.getCount(), 1);
            if (!writer  ||  !last_value)
            {
                printf("Cannot allocate DataWriter/RawValue\n");
                return;
            }
        }
        if (RawValue::getTime(value) < writer->getLastStamp())
        {
            ++back;
            if (verbose > 2)
                printf("Skipping %lu back-in-time values\r",
                       (unsigned long) back);
            continue;
        }
        if (! writer->add(value))
        {
            printf("DataWriter::add claims back-in-time\n");
            continue;
        }
        // Keep track of last time stamp and status (not value!)
        RawValue::setStatus(last_value,
                            RawValue::getStat(value),
                            RawValue::getSevr(value));
        RawValue::setTime(last_value, RawValue::getTime(value));
        last_value_set = true;
        ++count;
        if (verbose > 3 && (count % 1000 == 0))
            printf("Copied %lu values\r", (unsigned long) count);
        
    }
    if (do_enforce_off && last_value_set && count > 0 &&
        RawValue::getSevr(last_value) != ARCH_STOPPED)
    {   // Try to add an "Off" Sample.
        RawValue::setStatus(last_value, 0, ARCH_STOPPED);
        writer->add(last_value);        
    }
    writer = 0;
    if (verbose > 1)
    {
        if (back)
            printf("%lu values, %lu back-in-time                       \n",
                   (unsigned long) count, (unsigned long) back);
        else
            printf("%lu values                                         \n",
                   (unsigned long) count);
    }
    ++channel_count;
    value_count += count;
    back_count += back;
}

// Copy samples from archive with index_name
// to new index copy_name.
// Uses all samples in source archive or  [start ... end].
void copy(const stdString &index_name, const stdString &copy_name,
          int RTreeM, const epicsTime *start, const epicsTime *end,
          const stdString &single_name)
{
    IndexFile               index(RTreeM), new_index(RTreeM);
    IndexFile::NameIterator names;
    size_t                  channel_count = 0, value_count = 0, back_count = 0;
    BenchTimer              timer;
    stdString               dir1, dir2;
    Filename::getDirname(index_name, dir1);
    Filename::getDirname(copy_name, dir2);
    if (dir1 == dir2)
    {
        printf("You have to assert that the new index (%s)\n"
               "is in a  directory different from the old index\n"
               "(%s)\n", copy_name.c_str(), index_name.c_str());
        return;
    }
    index.open(index_name, true);
    new_index.open(copy_name, false);
    if (verbose)
        printf("Copying values from '%s' to '%s'\n",
               index_name.c_str(), copy_name.c_str());
    RawDataReader reader(index);
    if (single_name.empty())
    {
        bool ok = index.getFirstChannel(names);
        while (ok)
        {
            copy_channel(names.getName(), start, end, index, reader,
                         new_index, channel_count, value_count, back_count);
            ok = index.getNextChannel(names);
        }    
    }
    else
        copy_channel(single_name, start, end, index, reader,
                     new_index, channel_count, value_count, back_count);
    new_index.close();
    index.close();
    timer.stop();
    if (verbose)
    {
        printf("Total: %lu channels, %lu values\n",
               (unsigned long) channel_count, (unsigned long) value_count);
        printf("Skipped %lu back-in-time values\n",
               (unsigned long) back_count);
        printf("Runtime: %s\n", timer.toString().c_str());
    }
}

// Helper for copy: Handles samples of a single channel
void reduce_copy_channel(const stdString &channel_name,
                         const epicsTime *start, const epicsTime *end,
                         IndexFile &index, RawDataReader &reader,
                         IndexFile &new_index, int reduced_res,
                         size_t &channel_count, size_t &value_count, size_t &back_count)
{
    double          period = 1.0;
    double          epsilon = 0.00001;
    double          removal_fraction;
    double          removal_sum;
    double          time_span_days;

    size_t          num_samples = 4096;

    AutoPtr<DataWriter> writer;
    RawValue::Data *last_value = 0;
    bool            last_value_set = false;
    size_t          count = 0, back = 0;
    size_t          num_reduced_samples;
    int             num_secs_per_day = 86400;
    int             min_num_samples_per_day = 480;
    int             min_num_reduced_samples;
    int             sparc_count;
    int             need_sparc;
    int             actual_num_samples;
    int             set_to_num_min_samples;
    int             num_samples_to_remove;
    int             keep_sample;
    int             quotient;
    int             last_quotient;
    int             time_span_secs;

    epicsTime       cur_time;
    epicsTime       start_time;
    epicsTime       end_time;

    epicsTimeStamp  start_timestamp;
    epicsTimeStamp  end_timestamp;

    // Be sure to at least copy the first sample.
    sparc_count = reduced_res - 1;

    // Count the actual number of samples for determining whether
    // any sparcification is needed.  Also store the timestamps
    // of the first and last samples.  The time range determined
    // by the difference of these timestamps will be
    // used to calculate the minimum number of reduced samples
    // so that a chosen number of reduced samples per day (e.g.,
    // 480) will be retained.
    actual_num_samples = 0;

    try
    {
        const RawValue::Data *count_value = reader.find(channel_name, start);
        while (count_value && start && RawValue::getTime(count_value) < *start)
        {   // Correct for "before-or-at" idea of find()
            if (verbose > 2)
                printf("Skipping sample before start time\n");
            count_value = reader.next();
        }
        for (/**/; count_value; count_value = reader.next())
        {
            cur_time = RawValue::getTime(count_value);

            if (actual_num_samples == 0)
            {
                start_time = cur_time;
                end_time = cur_time;
            }

            if (end  && cur_time >= *end)
            {
                break; // Reached or exceeded end time
            }

            end_time = cur_time;

            actual_num_samples++;
        }
    }
    catch (GenericException ex)
    {
        printf("Caught reader Generic Exception for channel = %s\n", channel_name.c_str());
        fflush(stdout);
        return;
    }
    catch (...)
    {
        printf("Caught reader miscellaneous exception for channel = %s\n", channel_name.c_str());
        fflush(stdout);
        return;
    }

    if (verbose)
    {
        printf("Channel '%s' actual_num_samples = %d\n", channel_name.c_str(), actual_num_samples);
        fflush(stdout);
    }

    start_timestamp = (epicsTimeStamp) start_time;
    end_timestamp = (epicsTimeStamp) end_time;

    time_span_secs = (int) end_timestamp.secPastEpoch -
                     (int) start_timestamp.secPastEpoch;

    if (time_span_secs < 1)
    {
        time_span_secs = 1;
    }

    time_span_days = (double) time_span_secs /
                     (double) num_secs_per_day;

    min_num_reduced_samples = (int) (time_span_days * (double) min_num_samples_per_day);

    if (min_num_reduced_samples < min_num_samples_per_day)
    {
        min_num_reduced_samples = min_num_samples_per_day;
    }

    if (verbose > 1)
    {
        printf("time_span_days = %f, min_num_reduced_samples = %d\n", time_span_days, min_num_reduced_samples);
    }
     
    // If the actual number of samples is greater than the calculated minimum
    // number of reduced samples, then sparcification needs to be performed.
    // Otherwise, every sample will be copied.
    //
    // If sparcification is needed and the actual number of samples divided
    // by the reduction resolution >= the calculated minimum number of reduced
    // samples, then sparcification is easy: every Nth sample is kept, where
    // N is the reduction resolution.  Otherwise, we want to keep the
    // chosen minimum number of reduced samples and remove the rest.  Also
    // we want to remove samples periodically instead of just keeping the
    // first minimum number of reduced samples.  To do this, we first calculate
    // the removal fraction-- the number of samples to remove divided by
    // the actual number of samples.  Each time a new sample is found,
    // this removal fraction is added to the removal sum.  When the
    // whole part of the removal sum (called the quotient) is not equal
    // to the last quotient then the sample is removed.
    //
    need_sparc = 0;
    set_to_num_min_samples = 0;
    if (actual_num_samples > min_num_reduced_samples)
    {
        need_sparc = 1;
        if ((actual_num_samples / reduced_res) < min_num_reduced_samples)
        {
            set_to_num_min_samples = 1;

            removal_sum = 0.0;
            last_quotient = 0;

            num_samples_to_remove = actual_num_samples - min_num_reduced_samples;
            removal_fraction = (double) num_samples_to_remove /
                (double) actual_num_samples;
        }
    }

    determine_period_and_samples(index, channel_name, period, num_samples);

    const RawValue::Data *value = reader.find(channel_name, start);
    while (value && start && RawValue::getTime(value) < *start)
    {   // Correct for "before-or-at" idea of find()
        if (verbose > 2)
            printf("Skipping sample before start time\n");
        value = reader.next();
    }
    for (/**/; value; value = reader.next())
    {
        if (end  &&  RawValue::getTime(value) >= *end)
            break; // Reached or exceeded end time
        if (!writer || reader.changedType() || reader.changedInfo())
        {   // Need new or different writer
            writer = new DataWriter(
                new_index, channel_name,
                reader.getInfo(), reader.getType(), reader.getCount(),
                period, num_samples);
            RawValue::free(last_value);
            last_value_set = false;
            last_value = RawValue::allocate(reader.getType(),
                                            reader.getCount(), 1);
            if (!writer  ||  !last_value)
            {
                printf("Cannot allocate DataWriter/RawValue\n");
                return;
            }
        }
        if (RawValue::getTime(value) < writer->getLastStamp())
        {
            ++back;
            if (verbose > 2)
                printf("Skipping %lu back-in-time values\r",
                       (unsigned long) back);
            continue;
        }

        keep_sample = 0;
        if (!need_sparc)
        {
            keep_sample = 1;
        }
        else if (set_to_num_min_samples)
        {
            removal_sum += removal_fraction;
            quotient = (int) (removal_sum + epsilon);

            if (quotient == last_quotient)
            {
                keep_sample = 1;
            }
            else
            {
                last_quotient = quotient;
            }
        }
        else
        {
            ++sparc_count;
            if (sparc_count >= reduced_res)
            {
                keep_sample = 1;
                sparc_count = 0;
            }
        }
     
        if (keep_sample)
        {
            if (! writer->add(value))
            {
                printf("DataWriter::add claims back-in-time\n");
                continue;
            }
            // Keep track of last time stamp and status (not value!)
            RawValue::setStatus(last_value,
                                RawValue::getStat(value),
                                RawValue::getSevr(value));
            RawValue::setTime(last_value, RawValue::getTime(value));
            last_value_set = true;
            ++count;
            if (verbose > 3 && (count % 1000 == 0))
                printf("Copied %lu values\n", (unsigned long) count);
        } 
    }
    if (do_enforce_off && last_value_set && count > 0 &&
        RawValue::getSevr(last_value) != ARCH_STOPPED)
    {   // Try to add an "Off" Sample.
        RawValue::setStatus(last_value, 0, ARCH_STOPPED);
        writer->add(last_value);        
    }
    writer = 0;
    num_reduced_samples = actual_num_samples - count;
    if (verbose)
    {
        if (back)
            printf("values = %lu, back-in-time = %lu, num_reduced_samples = %lu\n",
                   (unsigned long) count, (unsigned long) back, (unsigned long) num_reduced_samples);
        else
            printf("values = %lu, num_reduced_samples = %lu\n",
                   (unsigned long) count, (unsigned long) num_reduced_samples);
    }
    ++channel_count;
    value_count += count;
    back_count += back;
}

// Reduce data copy from archive with index_name to new index
// reduce_name.  Uses all samples in source archive or
// [start ... end].  The specified reduction resolution (or the
// default, 1) N indicates that every Nth sample will be copied
// from the source to the target.
void reduce(const stdString &index_name, const stdString &reduce_name,
          int RTreeM, const epicsTime *start, const epicsTime *end,
          const stdString &single_name, int reduced_res)
{
    IndexFile               index(RTreeM), new_index(RTreeM);
    IndexFile::NameIterator names;
    size_t                  channel_count = 0, value_count = 0, back_count = 0;
    BenchTimer              timer;
    stdString               dir1, dir2;
    Filename::getDirname(index_name, dir1);
    Filename::getDirname(reduce_name, dir2);
    if (dir1 == dir2)
    {
        printf("You have to assert that the new index (%s)\n"
               "is in a  directory different from the old index\n"
               "(%s)\n", reduce_name.c_str(), index_name.c_str());
        return;
    }
    index.open(index_name, true);
    new_index.open(reduce_name, false);
    if (verbose)
        printf("Copying values from '%s' to '%s' with reduction factor %d\n",
               index_name.c_str(), reduce_name.c_str(), reduced_res);
    RawDataReader reader(index);
    if (single_name.empty())
    {
        bool ok = index.getFirstChannel(names);
        while (ok)
        {
            reduce_copy_channel(names.getName(), start, end, index, reader,
                                new_index, reduced_res,
                                channel_count, value_count, back_count);
            ok = index.getNextChannel(names);
        }    
    }
    else
        reduce_copy_channel(single_name, start, end, index, reader,
                            new_index, reduced_res, 
                            channel_count, value_count, back_count);
    new_index.close();
    index.close();
    timer.stop();
    if (verbose)
    {
        printf("Total: %lu channels, %lu values\n",
               (unsigned long) channel_count, (unsigned long) value_count);
        printf("Skipped %lu back-in-time values\n",
               (unsigned long) back_count);
        printf("Runtime: %s\n", timer.toString().c_str());
    }
}

void convert_dir_index(int RTreeM,
                       const stdString &dir_name, const stdString &index_name)
{
    OldDirectoryFile dir;
    if (!dir.open(dir_name))
        return;
    
    OldDirectoryFileIterator channels = dir.findFirst();
    IndexFile index(RTreeM);
    stdString index_directory;
    if (verbose)
        printf("Opened directory file '%s'\n", dir_name.c_str());
    index.open(index_name, false);
    if (verbose)
        printf("Created index '%s'\n", index_name.c_str());
    for (/**/;  channels.isValid();  channels.next())
    {
        if (verbose)
            printf("Channel '%s':\n", channels.entry.data.name);
        if (! Filename::isValid(channels.entry.data.first_file))
        {
            if (verbose)
                printf("No values\n");
            continue;
        }
        AutoPtr<RTree> tree(index.addChannel(channels.entry.data.name, index_directory));
        if (!tree)
        {
            fprintf(stderr, "Cannot add channel '%s' to index '%s'\n",
                    channels.entry.data.name, index_name.c_str());
            continue;
        }   
        DataFile *datafile =
            DataFile::reference(dir.getDirname(),
                                channels.entry.data.first_file, false);
        AutoPtr<DataHeader> header(
            datafile->getHeader(channels.entry.data.first_offset));
        datafile->release();
        while (header && header->isValid())
        {
            if (verbose)
            {
                stdString start, end;
                epicsTime2string(header->data.begin_time, start);
                epicsTime2string(header->data.end_time, end);    
                printf("'%s' @ 0x0%lX: %s - %s\n",
                       header->datafile->getBasename().c_str(),
                       (unsigned long)header->offset,
                       start.c_str(),
                       end.c_str());
            }
            if (!tree->insertDatablock(
                    header->data.begin_time, header->data.end_time,
                    header->offset,  header->datafile->getBasename()))
            {
                fprintf(stderr, "insertDatablock failed for channel '%s'\n",
                        channels.entry.data.name);
                break;
            }
            header->read_next();
        }
    }
    DataFile::close_all();
}


void convert_index_dir(const stdString &index_name, const stdString &dir_name)
{
    IndexFile::NameIterator names;
    IndexFile index(50);
    stdString index_directory;
    RTree::Datablock block;
    stdString start_file, end_file;
    FileOffset start_offset, end_offset;
    epicsTime start_time, end_time;
    bool ok;
    index.open(index_name.c_str(), true);
    OldDirectoryFile dir;
    if (!dir.open(dir_name, true))
    {
        fprintf(stderr, "Cannot create  '%s'\n", dir_name.c_str());
        return;
    }
    for (ok=index.getFirstChannel(names); ok; ok=index.getNextChannel(names))
    {
        if (verbose)
            printf("Channel '%s':\n", names.getName().c_str());
        AutoPtr<const RTree> tree(index.getTree(names.getName(),
                                                index_directory));
        RTree::Node node(tree->getM(), false);
        int i;
        if (!tree->getFirstDatablock(node, i, block))
        {
            printf("No first datablock for channel '%s'\n",
                   names.getName().c_str());
            continue;
        }
        start_file   = block.data_filename;
        start_offset = block.data_offset;
        if (!tree->getLastDatablock(node, i, block))
        {
            printf("No last datablock for channel '%s'\n",
                   names.getName().c_str());
            continue;
        }
        end_file   = block.data_filename;
        end_offset = block.data_offset;
        // Check by reading first+last buffer & getting times
        bool times_ok = false;
        AutoPtr<DataHeader> header(
            get_dataheader(index_directory, start_file, start_offset));
        if (header)
        {
            start_time = header->data.begin_time;
            header = get_dataheader(index_directory, end_file, end_offset);
            if (header)
            {
                end_time = header->data.end_time;
                header = 0;
                times_ok = true;
            }
        }
        if (times_ok)
        {
            stdString start, end;
            if (verbose)
                printf("%s - %s\n",
                       epicsTimeTxt(start_time, start),
                       epicsTimeTxt(end_time, end));
            OldDirectoryFileIterator dfi;
            dfi = dir.find(names.getName());
            if (!dfi.isValid())
                dfi = dir.add(names.getName());
            dfi.entry.setFirst(start_file, start_offset);
            dfi.entry.setLast(end_file, end_offset);
            dfi.entry.data.create_time = start_time;
            dfi.entry.data.first_save_time = start_time;
            dfi.entry.data.last_save_time = end_time;
            dfi.save();
        }
    }
}

unsigned long dump_datablocks_for_channel(IndexFile &index,
                                          const stdString &channel_name,
                                          unsigned long &direct_count,
                                          unsigned long &chained_count)
{
    DataFile *datafile;
    AutoPtr<DataHeader> header;
    direct_count = chained_count = 0;
    stdString directory;
    AutoPtr<RTree> tree(index.getTree(channel_name, directory));
    if (! tree)
        return 0;
    RTree::Datablock block;
    RTree::Node node(tree->getM(), true);
    stdString start, end;
    int idx;
    bool ok;
    if (verbose > 1)
        printf("RTree M for channel '%s': %d\n", channel_name.c_str(),
               tree->getM());
    if (verbose > 2)
        printf("Datablocks for channel '%s':\n", channel_name.c_str());
    for (ok = tree->getFirstDatablock(node, idx, block);
         ok;
         ok = tree->getNextDatablock(node, idx, block))
    {
        ++direct_count;
        if (verbose > 2)
            printf("'%s' @ 0x%lX: Indexed range %s - %s\n",
                   block.data_filename.c_str(),
                   (unsigned long)block.data_offset,
                   epicsTimeTxt(node.record[idx].start, start),
                   epicsTimeTxt(node.record[idx].end, end));
        if (verbose > 3)
        {
            datafile = DataFile::reference(directory,
                                           block.data_filename,
                                           false);
            header = datafile->getHeader(block.data_offset);
            datafile->release();
            if (header)
            {
                header->show(stdout, true);
                header = 0;
            }
            else
                printf("Cannot read header in data file.\n");
        }
        bool first_hidden_block = true;
        while (tree->getNextChainedBlock(block))
        {
            if (first_hidden_block && verbose > 2)
            {
                first_hidden_block = false;
                printf("Hidden blocks with smaller time range:\n");
            }
            ++chained_count;
            if (verbose > 2)
            {
                printf("---  '%s' @ 0x%lX\n",
                       block.data_filename.c_str(),
                       (unsigned long)block.data_offset);
                if (verbose > 3)
                {
                    datafile = DataFile::reference(directory,
                                                   block.data_filename,
                                                   false);
                    header = datafile->getHeader(block.data_offset);
                    if (header)
                    {
                        header->show(stdout, false);
                        header = 0;
                    }
                    else
                        printf("Cannot read header in data file.\n");
                    datafile->release();
                }   
            }
        }
        printf("\n");
    }
    return direct_count + chained_count;
}

unsigned long dump_datablocks(const stdString &index_name,
                              const stdString &channel_name)
{
    unsigned long direct_count, chained_count;
    IndexFile index(3);
    index.open(index_name, true);
    dump_datablocks_for_channel(index, channel_name,
                                direct_count, chained_count);
    printf("%ld data blocks, %ld hidden blocks, %ld total\n",
           direct_count, chained_count,
           direct_count + chained_count);
    return direct_count + chained_count;
}

unsigned long dump_all_datablocks(const stdString &index_name)
{
    unsigned long total = 0, direct = 0, chained = 0, channels = 0;
    IndexFile index(3);
    bool ok;
    index.open(index_name, true);
    IndexFile::NameIterator names;
    for (ok = index.getFirstChannel(names);
         ok;  ok = index.getNextChannel(names))
    {
        ++channels;
        unsigned long direct_count, chained_count;
        total += dump_datablocks_for_channel(index, names.getName(),
                                             direct_count, chained_count);
        direct += direct_count;
        chained += chained_count;
    }
    printf("Total: %ld channels, %ld datablocks\n", channels, total);
    printf("       %ld visible datablocks, %ld hidden\n", direct, chained);
    return total;
}

void dot_index(const stdString &index_name, const stdString channel_name,
               const stdString &dot_name)
{
    IndexFile index(3);
    index.open(index_name, true);
    stdString directory;
    AutoPtr<RTree> tree(index.getTree(channel_name, directory));
    if (!tree)
    {
        fprintf(stderr, "Cannot find '%s' in index '%s'.\n",
                channel_name.c_str(), index_name.c_str());
        return;
    }
    tree->makeDot(dot_name.c_str());
}

bool seek_time(const stdString &index_name,
               const stdString &channel_name,
               const stdString &start_txt)
{
    epicsTime start;
    if (!string2epicsTime(start_txt, start))
    {
        fprintf(stderr, "Cannot convert '%s' to time stamp\n",
                start_txt.c_str());
        return false;
    }
    IndexFile index(3);
    index.open(index_name, true);
    stdString directory;
    AutoPtr<RTree> tree(index.getTree(channel_name, directory));
    if (tree)
    {
        RTree::Datablock block;
        RTree::Node node(tree->getM(), true);
        int idx;
        if (tree->searchDatablock(start, node, idx, block))
        {
            stdString s, e;
            printf("Found block %s - %s\n",
                   epicsTimeTxt(node.record[idx].start, s),
                   epicsTimeTxt(node.record[idx].end, e));
        }
        else
            printf("Nothing found\n");
    }
    else
        fprintf(stderr, "Cannot find channel '%s'\n", channel_name.c_str());
    return true;
}

bool check (const stdString &index_name)
{
    IndexFile index(3);
    index.open(index_name, true);
    return index.check(verbose);
}

void atest_create_index(const stdString &index_name,
                        int RTreeM,
                        const stdString &single_name)
{

    int i;
    int j;
    int total_num_samples;

    size_t num_samples = 1024;

    long precision;

    DbrCount dbr_count;
    DbrType dbr_type;

    float lgraph_limit;
    float hgraph_limit;
    float lwarn_limit;
    float hwarn_limit;
    float lalarm_limit;
    float halarm_limit;

    double *double_val;
    double period = 1.0;

    char units[UNITS_LEN + 1];

    stdString date_stdString;
    stdString units_str;
    stdString channel_name;

    epicsTime sample_datetime;

    CtrlInfo *control_info = 0;

    RawValue::Data *raw_value;

    IndexFile index(RTreeM);

    IndexFile::NameIterator names;

    index.open(index_name, false);

    for (i = 0; i < 1; i++)
    {
        channel_name = single_name;
        DataWriter *writer = 0;

        lgraph_limit = 0.0;
        hgraph_limit = 0.0;
        lwarn_limit = 0.0;
        hwarn_limit = 0.0;
        lalarm_limit = 0.0;
        halarm_limit = 0.0;
        strcpy(units, "UNKNOWN");
        precision = 4;

        total_num_samples = 2;

        for (j = 0; j < total_num_samples; j++)
        {
            dbr_type = DBR_TIME_DOUBLE;
            dbr_count = 1;

            if (j == 0)
            {
                units_str = units;

                delete control_info;
                control_info = new CtrlInfo();
                control_info->setNumeric(precision,
                                         units_str,
                                         lgraph_limit,
                                         hgraph_limit,
                                         lalarm_limit,
                                         lwarn_limit,
                                         hwarn_limit,
                                         halarm_limit);

                delete writer;

                writer = new DataWriter(index,
                                        channel_name,
                                        *control_info,
                                        dbr_type,
                                        dbr_count,
                                        period,
                                        num_samples);
                if (!writer)
                {
                    printf("Cannot create DataWriter\n");
                    return;
                }
            }

            raw_value = RawValue::allocate(dbr_type, dbr_count, 1);

            RawValue::setStatus(raw_value,
                                (short) 0,
                                (short) 0);

            if (j == 0)
            {
                date_stdString = "01/20/2004 00:00:00.000000000";
            }
            else
            {
                date_stdString = "01/20/2004 01:00:00.000000000";
            }

            if (!string2epicsTime(date_stdString, sample_datetime))
            {
                fprintf(stderr, "Cannot convert '%s' to time stamp\n",
                    date_stdString.c_str());
                return;
            }

            RawValue::setTime(raw_value, sample_datetime);

            double_val = &((dbr_time_double *)raw_value)->value;

            if (j == 0)
            {
                *double_val = 1.0;
            }
            else
            {
                *double_val = 2.0;
            }

            if (! writer->add(raw_value))
            {
                printf("DataWriter::add failed\n");
            }

            RawValue::free(raw_value);
        }

        delete writer;
    }

    index.close();

    return;
}

int convert_hist_buf_files(const stdString &hist_buf_files_filename,
                           const stdString &index_name,
                           int RTreeM)
{
    int status;
    int cur_line_offset;
    int num_intervals;
    int time_span_secs;
    int num_hist_buf_files;

    size_t num_samples;

    long precision;

    float lgraph_limit;
    float hgraph_limit;
    float lwarn_limit;
    float hwarn_limit;
    float lalarm_limit;
    float halarm_limit;

    double period;
    double cur_double;
    double *double_val;

    char units_str[UNITS_LEN + 1];
    char buffer[MAX_BUFFER_LEN + 1];
    char cur_hist_buf_filename[MAX_BUFFER_LEN + 1];
    char cur_line[MAX_BUFFER_LEN + 1];
    char cur_time_str[TIME_STRING_LEN + 1];
    char cur_double_str[DOUBLE_STRING_LEN + 1];
    char cur_channel_name_str[MAX_CHANNEL_NAME_LEN + 1];
    char timestamp_str[TIME_OUTPUT_STRING_LEN + 1];

    char *match_ptr;

    FILE *hist_buf_files_filename_f_ptr;
    FILE *cur_hist_buf_file_f_ptr;

    struct tm cur_line_tm_time;

    time_t start_time;
    time_t end_time;
    time_t cur_line_time;
    time_t cur_time_secs;

    struct tm *tm_time_ptr;

    stdString units;
    stdString cur_channel_name;
    stdString cur_time;

    epicsTime cur_epics_time;

    DbrCount dbr_count;
    DbrType dbr_type;

    IndexFile index(RTreeM);

    DataWriter *writer = 0;

    CtrlInfo *control_info = 0;

    RawValue::Data *raw_value;

    /*** Logic begins */
    status = 0;

    lgraph_limit = 0.0;
    hgraph_limit = 0.0;
    lwarn_limit = 0.0;
    hwarn_limit = 0.0;
    lalarm_limit = 0.0;
    halarm_limit = 0.0;
    strcpy(units_str, "UNKNOWN");

    precision = 7;


    dbr_type = DBR_TIME_DOUBLE;
    dbr_count = 1;

#ifdef DEBUG
    fprintf(stdout, "hist_buf_files_filename = %s\n", hist_buf_files_filename.c_str());
    fprintf(stdout, "index_name = %s\n", index_name.c_str());
#endif

    /* Open the specified file containing the filepaths of each history buffer
       file to process.  Then read each filepath and verify that each history
       buffer file to process can be opened.
    -------------------------------------------------------------------------- */
    if ((hist_buf_files_filename_f_ptr = fopen(hist_buf_files_filename.c_str(), "r")) == NULL)
    {
        fprintf(stderr, "Cannot open file %s\n", hist_buf_files_filename.c_str());
        status = -1;
        return status;       
    }

    while (fgets(buffer, sizeof(buffer), hist_buf_files_filename_f_ptr) != NULL)
    {
        strtok(buffer, "\n");

        strcpy(cur_hist_buf_filename, buffer);

#ifdef DEBUG
        fprintf(stdout, "cur_hist_buf_filename = %s\n", cur_hist_buf_filename);
#endif

        if ((cur_hist_buf_file_f_ptr = fopen(cur_hist_buf_filename, "r")) == NULL)
        {
            fprintf(stderr, "Cannot open file %s\n", cur_hist_buf_filename);
            status = -1;
            return status;       
        }

        fclose(cur_hist_buf_file_f_ptr);
    }

    fclose(hist_buf_files_filename_f_ptr);

    /* Open the output index.
    ------------------------- */
    index.open(index_name, false);

    /* Open the specified file containing the filepaths of each history buffer
       file to process again so that each history buffer file can now be
       processed.
    -------------------------------------------------------------------------- */
    if ((hist_buf_files_filename_f_ptr = fopen(hist_buf_files_filename.c_str(), "r")) == NULL)
    {
        fprintf(stderr, "Cannot open file %s\n", hist_buf_files_filename.c_str());
        status = -1;
        return status;       
    }

    /* Loop for each history buffer file to process.  Each history buffer file
       contains a PV name and an array of timestamp/floating point value pairs.
    -------------------------------------------------------------------------- */
    num_hist_buf_files = 0;
    while (fgets(buffer, sizeof(buffer), hist_buf_files_filename_f_ptr) != NULL)
    {
        strtok(buffer, "\n");

        strcpy(cur_hist_buf_filename, buffer);

        /* Print the file number we are currently processing and also periodically
           the current time to track performance.
        -------------------------------------------------------------------------- */
        fprintf(stdout, "processing file number %d\n", num_hist_buf_files);
        fflush(stdout);

        if ((num_hist_buf_files == 0) ||
            (num_hist_buf_files % TIME_OUTPUT_FILE_INTERVAL == 0) )
        {
            time(&cur_time_secs);

            tm_time_ptr = localtime(&cur_time_secs);

            if (strftime(timestamp_str, 20, "%m/%d/%Y %H:%M:%S", tm_time_ptr) == 0)
            {
                fprintf(stderr, "Error during strftime time conversion\n");
                status = -1;
                return status;       
            }

            fprintf(stdout, "time before processing file number %d = %s\n",
                num_hist_buf_files, timestamp_str);
            fflush(stdout);
        }

#ifdef DEBUG
        fprintf(stdout, "cur_hist_buf_filename = %s\n", cur_hist_buf_filename);
#endif

        /* Open the current history buffer file being processed in order to
           validate it as well as determine the number of samples and the
           average period. 
        ------------------------------------------------------------------- */ 
        if ((cur_hist_buf_file_f_ptr = fopen(cur_hist_buf_filename, "r")) == NULL)
        {
            fprintf(stderr, "Cannot open file %s\n", cur_hist_buf_filename);
            status = -1;
            return status;       
        }

        cur_line_offset = 0; 
        while (fgets(buffer, sizeof(buffer), cur_hist_buf_file_f_ptr) != NULL)
        {
            strtok(buffer, "\n");

            strcpy(cur_line, buffer);

            if (cur_line_offset == 0)
            {
                /* Validate the channel name on the first line, which should start 3
                   characters after the blank before the exclamation point character
                   on this line.
                -------------------------------------------------------------------- */
                match_ptr = strstr(cur_line, " ! ");
                if (match_ptr == NULL)
                {
                    fprintf(stderr, "Invalid first line in file %s", cur_hist_buf_filename);
                    status = -1;
                    return status;       
                }

                match_ptr += 3;

#ifdef DEBUG
                fprintf(stdout, "New channel name = %s\n", match_ptr);
#endif
            }
            else
            {
                /* This line is not the first line so it should be a timestamp/floating
                   point value pair.  First, convert the timestamp string to a UNIX
                   timestamp.
                ------------------------------------------------------------------------ */
                strncpy(cur_time_str, cur_line, TIME_STRING_LEN);
                cur_time_str[TIME_STRING_LEN] = '\0';

                if (strptime(cur_time_str, "%m/%d/%Y %H:%M:%S", &cur_line_tm_time) == NULL)
                {
                    fprintf(stderr, "Error during strptime time conversion on line %d in file %s\n",
                        cur_line_offset, cur_hist_buf_filename);
                    status = -1;
                    return status;       
                }

                cur_line_tm_time.tm_isdst = -1;
                cur_line_time = mktime(&cur_line_tm_time);

                /* If this is the first timestamp/floating point value pair,
                   initialize the start and end times to the UNIX timestamp value.
                   Otherwise, update the start time if the UNIX timestamp value is
                   less than the current start time and update the end time if the
                   UNIX timestamp is greater than the current end time.  The
                   timestamps in the file should be monotonically increasing but
                   we are not relying on this assumption.
                ------------------------------------------------------------------ */
                if (cur_line_offset == 1)
                {
                    start_time = cur_line_time;
                    end_time = cur_line_time;
                }
                else
                {
                    if (cur_line_time < start_time)
                    {
                        start_time = cur_line_time;
                    }

                    if (cur_line_time > end_time)
                    {
                        end_time = cur_line_time;
                    }
                }

            }

            cur_line_offset++;
        }

        fclose(cur_hist_buf_file_f_ptr);

        /* Set the number of samples and calculate the average time period
           between samples.
        ------------------------------------------------------------------ */
        num_samples = cur_line_offset - 1;
        num_intervals = num_samples - 1;

        time_span_secs = end_time - start_time;

        period = (double) (time_span_secs / num_intervals);

#ifdef DEBUG
        fprintf(stdout, "num_samples = %d\n", num_samples);
        fprintf(stdout, "time_span_secs = %d\n", time_span_secs);
        fprintf(stdout, "period = %f\n", period);
#endif

        /* Open the current history buffer file being processed again in order
           to process it.
        ---------------------------------------------------------------------- */
        if ((cur_hist_buf_file_f_ptr = fopen(cur_hist_buf_filename, "r")) == NULL)
        {
            fprintf(stderr, "Cannot open file %s\n", cur_hist_buf_filename);
            status = -1;
            return status;       
        }

        cur_line_offset = 0;
        while (fgets(buffer, sizeof(buffer), cur_hist_buf_file_f_ptr) != NULL)
        {
            strtok(buffer, "\n");

            strcpy(cur_line, buffer);

            if (cur_line_offset == 0)
            {
                /* Extract the channel name on the first line, which should start 3
                   characters after the blank before the exclamation point character
                   on this line.
                -------------------------------------------------------------------- */
                match_ptr = strstr(cur_line, " ! ");
                if (match_ptr == NULL)
                {
                    fprintf(stderr, "Invalid first line in file %s", cur_hist_buf_filename);
                    status = -1;
                    return status;       
                }

                match_ptr += 3;

                strcpy(cur_channel_name_str, match_ptr);
            }
            else
            {
                /* This line is not the first line so it should be a timestamp/
                   floating point value pair.  First, copy the timestamp and double
                   values to strings.  The double value string is directly after
                   the timestamp.  If the double value is positive there is a blank
                   between the timestamp and the double value but if it is negative
                   there is no character between the two (that is, there is a minus
                   sign instead of a blank).  Also note that each double ends with
                   E, a plus or minus sign, and a two character exponent.  The
                   last digit is not the third character of the exponent but is
                   a flag which is ignored in this processing.
                --------------------------------------------------------------------- */
                strncpy(cur_time_str, cur_line, TIME_STRING_LEN);
                cur_time_str[TIME_STRING_LEN] = '\0';

                strncpy(cur_double_str, cur_line + TIME_STRING_LEN, DOUBLE_STRING_LEN);
                cur_double_str[DOUBLE_STRING_LEN] = '\0';

#ifdef DEBUG
                fprintf(stdout, "cur_time_str = %s\n", cur_time_str);
                fprintf(stdout, "cur_double_str = %s\n", cur_double_str);
#endif

                if (cur_line_offset == 1)
                {
                    /* Set the control ("meta") information for the current
                       channel to default values for units and limits.  The
                       precision for floating point values in the history
                       buffer text files is 7.
                    ------------------------------------------------------- */
                    units = units_str;
                    cur_channel_name = cur_channel_name_str;

                    control_info = new CtrlInfo();
                    control_info->setNumeric(precision,
                                             units,
                                             lgraph_limit,
                                             hgraph_limit,
                                             lalarm_limit,
                                             lwarn_limit,
                                             hwarn_limit,
                                             halarm_limit);

                    /* Create a DataWriter object for the current channel
                       using the period and number of samples found during
                       the previous processing of the file.
                    ------------------------------------------------------ */ 
                    writer = new DataWriter(index,
                                            cur_channel_name,
                                            *control_info,
                                            dbr_type,
                                            dbr_count,
                                            period,
                                            num_samples);
                    if (!writer)
                    {
                        fprintf(stderr, "Cannot create DataWriter\n");
                        status = -1;
                        return status;
                    }

                }

                /* Allocate a RawValue object and set the status for it
                   to default good values.
                ------------------------------------------------------- */
                raw_value = RawValue::allocate(dbr_type, dbr_count, 1);

                RawValue::setStatus(raw_value,
                                    (short) 0,
                                    (short) 0);

                /* Convert the timestamp for the current line from a string to
                   an EPICS timestamp.  Then set the time for the RawValue
                   object to the EPICS timestamp.
                -------------------------------------------------------------- */
                cur_time = cur_time_str;

                if (!string2epicsTime(cur_time, cur_epics_time))
                {
                    fprintf(stderr, "Cannot convert '%s' to time stamp in file %s\n",
                        cur_time_str, cur_hist_buf_filename);
                    status = -1;
                    return status;
                }

                RawValue::setTime(raw_value, cur_epics_time);

                /* Get a pointer to the double value of the RawValue object.
                   Then set the RawValue object double value to the double
                   obtained from converting the current value string to a
                   double.
                ------------------------------------------------------------ */
                double_val = &((dbr_time_double *)raw_value)->value;

                cur_double = strtod(cur_double_str, NULL);

                *double_val = cur_double;

                /* Write the RawValue object to the archive data.  If a
                   failure status is detected, write an error message and
                   continue processing rather than returning a bad status.
                   This must be done since a failure status will be detected
                   for timestamps during the "Spring forward" Standard Time
                   Daylight Savings Time transition.  This will occur for
                   timestamps between 03:00 and 04:00 on the Sunday night
                   of the transition (not between 02:00 and 03:00, as might
                   be expected.  Data cannot be archived for these bad
                   timestamps.
                ---------------------0-------------------------------------- */
                if (! writer->add(raw_value))
                {
                    fprintf(stdout, "DataWriter::add failed for line %d with timestamp %s in file %s\n",
                       cur_line_offset, cur_time_str, cur_hist_buf_filename);
                    fflush(stdout);
                }

                RawValue::free(raw_value);
            }

            cur_line_offset++;
        }

        /* The DataWriter object must be deleted so that the destructor can
           be invoked to perform index cleanup activities.
        ------------------------------------------------------------------- */
        delete writer;

        delete control_info; 

        fclose(cur_hist_buf_file_f_ptr);

        num_hist_buf_files++;
    }

    fclose(hist_buf_files_filename_f_ptr);

    index.close();

    return status;
}

int main(int argc, const char *argv[])
{
    CmdArgParser parser(argc, argv);
    parser.setHeader("Archive Data Tool version " ARCH_VERSION_TXT ", "
                     EPICS_VERSION_STRING
                      ", built " __DATE__ ", " __TIME__ "\n\n"
                     );
    parser.setArgumentsInfo("<index-file>");
    CmdArgFlag help          (parser, "help", "Show help");
    CmdArgInt  verbosity     (parser, "verbose", "<level>", "Show more info");
    CmdArgFlag info          (parser, "info", "Simple archive info");
    CmdArgFlag list_index    (parser, "list", "List channel name info");
    CmdArgFlag values      (parser, "Values", "List channel name number of values for time span");
    CmdArgString copy_index  (parser, "copy", "<new index>", "Copy channels");
    CmdArgString reduce_index (parser, "reduce", "<new index>", "Reduced resolution copy channels");
    CmdArgInt ReducedRes      (parser, "R", "<1-100>", "Reduced resolution");

    CmdArgString start_time  (parser, "start", "<time>",
                              "Format: \"mm/dd/yyyy[ hh:mm:ss[.nano-secs]]\"");
    CmdArgString end_time    (parser, "end", "<time>", "(exclusive)");
    CmdArgDouble file_limit  (parser, "file_limit", "<MB>", "File Size Limit");
    CmdArgString basename    (parser, "basename", "<string>", "Basename for new data files");
    CmdArgFlag enforce_off   (parser, "append_off", "Enforce a final 'Archive_Off' value when copying data");
    CmdArgString dir2index   (parser, "dir2index", "<dir. file>",
                              "Convert old directory file to index");
    CmdArgString index2dir   (parser, "index2dir", "<dir. file>",
                              "Convert index to old directory file");
    CmdArgInt RTreeM         (parser, "M", "<1-100>", "RTree M value");
    CmdArgFlag dump_blocks   (parser, "blocks", "List channel's data blocks");
    CmdArgFlag all_blocks    (parser, "Blocks", "List all data blocks");
    CmdArgString dotindex    (parser, "dotindex", "<dot filename>",
                              "Dump contents of RTree index into dot file");
    CmdArgString channel_name(parser, "channel", "<name>", "Channel name");
    CmdArgFlag hashinfo      (parser, "hashinfo", "Show Hash table info");
    CmdArgString seek_test   (parser, "seek", "<time>", "Perform seek test");
    CmdArgFlag test          (parser, "test", "Perform some consistency tests");
    CmdArgString hist_buf    (parser, "HistBuf", "<files filename>", "Convert SLC History Buffer input files");
    CmdArgFlag atest         (parser, "atest", "Test index file creation");

    try
    {
        int status;

        // Defaults
        RTreeM.set(50);
        ReducedRes.set(1);

        file_limit.set(100.0);
        // Get Arguments
        if (! parser.parse())
            return -1;
        if (help   ||   parser.getArguments().size() != 1)
        {
            parser.usage();
            return -1;
        }
        // Consistency checks
        if ((dump_blocks ||
             dotindex.get().length() > 0  ||
             seek_test.get().length() > 0)
            && channel_name.get().length() <= 0)
        {
            fprintf(stderr,
                    "Options 'blocks' and 'dotindex' require 'channel'.\n");
            return -1;
        }
        verbose = verbosity;
        stdString index_name = parser.getArgument(0);
        DataWriter::file_size_limit = (unsigned long)(file_limit*1024*1024);
        if (file_limit < 10.0)
            fprintf(stderr, "file_limit values under 10.0 MB are not useful\n");
        // Start/end time
        epicsTime *start = 0, *end = 0;
        stdString txt;
        if (start_time.get().length() > 0)
        {
            start = new epicsTime;
            string2epicsTime(start_time.get(), *start);
            if (verbose > 1)
                printf("Using start time %s\n", epicsTimeTxt(*start, txt));
        }
        if (end_time.get().length() > 0)
        {
            end = new epicsTime();
            string2epicsTime(end_time.get(), *end);
            if (verbose > 1)
                printf("Using end time   %s\n", epicsTimeTxt(*end, txt));
        }
        // Base name
        if (basename.get().length() > 0)
    	    DataWriter::data_file_name_base = basename.get();
        if (enforce_off)
    	    do_enforce_off = true;
        // What's requested?
        if (info)
    	    list_names(index_name, false);
        else if (list_index)
            list_names(index_name, true);
        else if (values)
        {
            list_num_values(index_name, RTreeM, start, end, channel_name);
            return 0;
        }
        else if (copy_index.get().length() > 0)
        {
            copy(index_name, copy_index, RTreeM, start, end, channel_name);
            return 0;
        }
        else if (reduce_index.get().length() > 0)
        {
            reduce(index_name, reduce_index, RTreeM, start, end, channel_name, ReducedRes);
            return 0;
        }
        else if (hashinfo)
            show_hash_info(index_name);
        else if (dir2index.get().length() > 0)
        {
            convert_dir_index(RTreeM, dir2index, index_name);
            return 0;
        }
        else if (index2dir.get().length() > 0)
        {
            convert_index_dir(index_name, index2dir);
            return 0;
        }
        else if (all_blocks)
        {
            dump_all_datablocks(index_name);
            return 0;
        }
        else if (dump_blocks)
        {
            dump_datablocks(index_name, channel_name);
            return 0;
        }
        else if (dotindex.get().length() > 0)
        {
            dot_index(index_name, channel_name, dotindex);
            return 0;
        }
        else if (seek_test.get().length() > 0)
        {
            seek_time(index_name, channel_name, seek_test);
            return 0;
        }
        else if (test)
        {
            return check(index_name) ? 0 : -1;
        }
        else if (hist_buf.get().length() > 0)
        {
            status = convert_hist_buf_files(hist_buf, index_name, RTreeM);
            return status;
        }
        else if (atest)
        {
            atest_create_index(index_name, RTreeM, channel_name);
            return 0;
        }
        else
        {
            parser.usage();
            return -1;
        }
    }
    catch (GenericException &e)
    {
        fprintf(stderr, "Error:\n%s\n", e.what());
    }
        
    return 0;
}

