/*
 * Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 */

#include <stdio.h>
#ifdef WIN32
#include "gnu_getopt\getopt.h"
#endif

#ifdef __unix__
#include <unistd.h>
#endif
#include "rtv.h"
#include "guide.h"
#include "dump.h"

#define DEFAULT_VERSION 0x00050001

struct mapping map[] = {
    {-1, NULL},
};
        
static int dump_replay_show_file(FILE * fp, u32 version)
{
    unsigned char buffer[4096];
    unsigned char * p;
    struct replay_show rs;
    size_t sz;

    sz = replay_show_size(version);
    expect(sz < sizeof(buffer));
    while (fread(buffer, sz, 1, fp)) {
        p = buffer;
        parse_replay_show(&p, &rs, version);
        dump_replay_show(&rs);
    }

    return 0;
}

int main(int argc, char ** argv)
{
    int i;
    int ch;
    u32 v = DEFAULT_VERSION;
    
    while ((ch = getopt(argc, argv, "v")) != EOF) {
        switch(ch) {
            case 'v':
                v = strtoul(optarg, NULL, 0);
                break;
            default:
                fprintf(stderr, "Invalid argument\n");
                return -1;
        }
    }
    
    dump_set_file(stdout);

    if (optind < argc) {
        for (i = optind; i < argc; i++) {
            FILE * fp;
            fp = fopen(argv[i], "rb");
            if (!fp) {
                fprintf(stderr, "Couldn't open %s\n", argv[i]);
                return -1;
            }
            dump_group_start(argv[i]);
            dump_replay_show_file(fp, v);
            dump_group_end();
        }
    } else {
        dump_group_start("stdin");
        dump_replay_show_file(stdin, v);
        dump_group_end();
    }

    return 0;
}
