
/*
 * Copyright (c) 2001 BBNT Solutions LLC
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice and this permission
 * appear in all copies and in supporting documentation, and that the
 * name of BBN Technologies not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  BBN makes no representations about the
 * suitability of this software for any purposes.  It is provided "AS
 * IS" without express or implied warranties.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <math.h>
#include "stats.h"

unsigned long num_samples = 0, num_base = 0;
unsigned short verbose = FALSE;
unsigned short interactive = TRUE;
unsigned long mem_size_inc = MEM_SIZE_INC;
unsigned long max_samples = MEM_SIZE_INC;
unsigned long max_base = MEM_SIZE_INC;
double *sample, *base;
unsigned long data_changed = 0;
char *one_cmd = NULL;
FILE *out = NULL;

void usage (progname)
char *progname;
{
    fprintf (stderr,"usage: %s [batch_arguments] [-f script_file]\n", 
	     progname);
    fprintf (stderr," batch_arguments:\n");
    fprintf (stderr,"  -b[v1,v2,...] generate basic statistics\n");
    fprintf (stderr,"  -c            generate CDF\n");
    fprintf (stderr,"  -cc           generate complimentary CDF\n");
    fprintf (stderr,"  -C X          run a single command\n");
    fprintf (stderr,"  -h            usage instructions\n");
    fprintf (stderr,"  -M X          increment memory by X data points\n");
    fprintf (stderr,"  -n            don't print labels\n");
    fprintf (stderr,"  -o            dump all basic stats on one line\n");
    fprintf (stderr,"  -O X          send output to given file (X)\n");
    fprintf (stderr,"  -t[h][z] G    generate frequency table (with \n");
    fprintf (stderr,"                histogram and zero supression) \n");
    fprintf (stderr,"                with granularity G\n");
    fprintf (stderr,"  -v            version information\n");
    exit (1);
}


void parseargs (argc,argv)
int argc;
char *argv [];
{
    unsigned short flags = 0;
    int i;

    if (argc == 1)
	return;
    for (i = 1; i < argc; i++)
    {
	if ((argv [i] [0] == '-') && (strlen (argv [i]) > 1))
	{
	    if (!strcmp (argv [i],"-cc"))
	    {
		interactive = FALSE;
		batch_ccdf = TRUE;
		flags |= FLAGS_BATCH;
	    }
	    else if (!strcmp (argv [i],"-c"))
	    {
		interactive = FALSE;
		batch_cdf = TRUE;
		flags |= FLAGS_BATCH;
	    }
	    else if (!strcmp (argv [i],"-n"))
		basic_labels = FALSE;
	    else if (!strcmp (argv [i],"-o"))
		basic_oneline = TRUE;
	    else if (!strcmp (argv [i],"-h"))
		usage (argv [0]);
	    else if (!strcmp (argv [i],"-v"))
	    {
		silent = FALSE;
		print_info ();
		exit (0);
	    }
	    else if (!strncmp (argv [i],"-b",2))
	    {
		interactive = FALSE;
		flags |= FLAGS_BATCH;
		batch_basic = TRUE;
		if (strlen (argv [i]) > 2)
		    batch_basic_args = argv [i] + 2;
	    }
	    else if ((i + 1) < argc)
	    {
		if (!strncmp (argv [i],"-t",2))
		{
		    interactive = FALSE;
		    flags |= FLAGS_BATCH;
		    batch_freqtab = TRUE;
		    if (!strcmp (argv [i],"-th"))
			freqtab_hist = TRUE;
		    if (!strcmp (argv [i],"-tz"))
			freqtab_zero = FALSE;
		    if (!strcmp (argv [i],"-tzh") || !strcmp (argv [i],"-thz"))
		    {
			freqtab_hist = TRUE;
			freqtab_zero = FALSE;
		    }
		    freqtab_gran = atof (argv [++i]);
		}
		else if (!strcmp (argv [i],"-O"))
		    batch_outfile = argv [++i];
		else if (!strcmp (argv [i],"-M"))
		    mem_size_inc = atoi (argv [++i]);
		else if (!strcmp (argv [i],"-f"))
		{
		    interactive = FALSE;
		    flags |= FLAGS_SCRIPT;
		    script_file = argv [++i];
		}
		else if (!strcmp (argv [i],"-C"))
		{
		    interactive = FALSE;
		    flags |= FLAGS_ONE_CMD;
		    one_cmd = argv [++i];
		}
	    }
	    else
		usage (argv [0]);
	}
	else 
	    num_samples = readdata (argv [i]);
    }
    if ((flags != FLAGS_INTER) && (flags != FLAGS_BATCH) &&
	(flags != FLAGS_SCRIPT) && (flags != FLAGS_ONE_CMD))
	usage (argv [0]);
    if (num_samples)
	quick_sort (sample,0,num_samples - 1);
}


int main (argc,argv)
int argc;
char *argv [];
{
    out = stdout;
    sample = (double *)malloc (sizeof (double) * mem_size_inc);
    base = (double *)malloc (sizeof (double) * mem_size_inc);
    if ((sample == NULL) || (base == NULL))
    {
	fprintf (stderr,"can't allocate enough memory\n");
	exit (1);
    }
    parseargs (argc,argv);
    if (interactive)
	do_interactive ();
    else if (script_file != NULL)
    {
	silent = TRUE;
	script_proc ();
    }
    else if (one_cmd != NULL)
	do_cmd (one_cmd);
    else
	batch ();
    exit (0);
}
