/*
Copyright (c) 2000                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
-------------------------------------------------------------------------------
Module Header
Filename          : getargs.C
Author            : Rene Wilhelm
Date              : 17-OCT-2000
Revision          : 15-JUN-2001  revised for Linux
Revised		  : 08-AUG-2002  add CSV format
Description       : process command line arguments of delayplots program
Language Version  : C++
OSs Tested        : Solaris 2.6 , Debian Linux 2.2
To Do		  : Turn this into a 'CommandLineArguments' class, to
		    be passed to DelayPlots object upon creation.
$Id: getargs.C,v 1.8 2002/08/09 14:32:41 ttraffic Exp $
-------------------------------------------------------------------------------
*/

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "utils.h"
#include "getargs.h"
#include "iso8601.h"

static char const rcsid[] = "$Id: getargs.C,v 1.8 2002/08/09 14:32:41 ttraffic Exp $";

void usage() {
	fprintf (stderr, "usage: %s -p timeperiod [-v] [-l] [-a] [-o outputdir] [-s source_id] [-t target_id] [-f format] [-m maxdelay] [-z mindelay]\n", progname);
	exit(1);
}

void getargs(int argc, char *argv[], char* src, char* tgt, 
	time_t& endtime, time_t& duration, double& mindelay, double& maxdelay, 
        short& logscale, char** outfile, plotformat& fmt, short& allpackets) {

	extern char *optarg;

	char ch;
	char *endpointer;

	short fflag=0;
	short mflag=0;
	short oflag=0;
	short pflag=0;
	short sflag=0;
	short tflag=0;
	short zflag=0;

	progname = argv[0];
	logscale = 0;
	putenv("TZ=GMT+0");

	while ((ch = getopt(argc, argv, "valm:z:s:t:p:o:f:")) != -1)
	switch(ch) {
	case 's':
		if (!sflag) {
			sflag = 1;    
			if (sscanf(optarg, "%[0-9]", src) != 1) {
				usage();
			}
		}
		else {
			/* only one -s allowed */
			usage();
		}
		break;
	case 't':
		if (!tflag) {
			tflag = 1;    
			if (sscanf(optarg, "%[0-9]", tgt) != 1) {
				usage();
			}
		}
		else {
			/* only one -t allowed */
			usage();
		}
		break;
	case 'z':
		if (!zflag) {
			zflag = 1;    
			mindelay = strtod(optarg, &endpointer);
			if (*endpointer != '\0') {
				usage();
			}
			if (mindelay <0) {
				fprintf (stderr, "error: %s: -z positive value expected\n", progname);
				exit(1);
			}
			else if ((mindelay == HUGE) || (mindelay == HUGE_VAL)) {
				fprintf (stderr, "error: %s: -z value out of range\n", progname);
				exit(1);
			}
		}
		else {
			/* only one -z allowed */
			usage();
		}
		break;
	case 'm':
		if (!mflag) {
			mflag = 1;    
			maxdelay = strtod(optarg, &endpointer);
			if (*endpointer != '\0') {
				usage();
			}
			if (maxdelay <=0) {
				fprintf (stderr, "error: %s: -m positive value expected\n", progname);
				exit(1);
			}
			else if ((maxdelay == HUGE) || (maxdelay == HUGE_VAL)) {
				fprintf (stderr, "error: %s: -m value out of range\n", progname);
				exit(1);
			}
		}
		else {
			/* only one -m allowed */
			usage();
		}
		break;
	case 'o':
		if (!oflag) {
			*outfile = new char[strlen(optarg)+1];
			strcpy(*outfile, optarg);
		}
		else {
			/* only one -o allowed */
			usage();
		}
		break;
		
	case 'p':
		if (!pflag) {
			pflag = 1;    
			decodePeriod(optarg, endtime, duration);
		}
		else {
			/* only one -p allowed */
			usage();
		}
		break;
	case 'a':
		allpackets = 1;
		break;
	case 'l':
		logscale = 1;
		break;
	case 'v':
		verbose = 1;
		break;
	case 'f':
		if (fflag) {
			usage(); /* only one -f allowed */
		}

		if (strcmp(optarg, "gif") == 0) {
			fmt = gif;
		}
		else if (strcmp(optarg, "eps") == 0) {
			fmt = eps;
		}
		else if (strcmp(optarg, "ps") == 0) {
			fmt = ps;
		}
		else if (strcmp(optarg, "csv") == 0) {
			fmt = csv;
		}
		else {
			usage();
		}
		fflag = 1;
		break;
	default:
		usage();
	}

	if ( !pflag ) {
		/* time period is only required argument */
		usage();
	}
}
