/* main.c
 *
 * Written by Matt Levy
 * modified 29 Jul 1999 by Neil Moore <neil@cs.uky.edu>
 * Modified 20 Aug 1999 by Neil Moore <neil@cs.uky.edu>
 * Modified  8 Sep 1999 by Neil Moore <neil@cs.uky.edu>
 *
 * Part of lderes.  See COPYRIGHT for copyright information.
 *
 */

#ifndef _MAIN_C
#define _MAIN_C	1
#include "SimpleDefaultTheory.h"
#include "default_types.h"
#include "search.h"
#include "global.h"
#include <stdio.h>
#include <string.h>
#include <time.h>

#ifndef VERSION
#define VERSION "unknown"
#endif

#ifndef LPSMSVER
#define LPSMSVER "unknown"
#endif

static int to_int(const char *str);
static void usage(void);
static void version(void);

int timex = 1;
int verbosity = 1;

int main(int argc, char **argv)
{
	char *filename = NULL;
	SimpleDefaultTheory theory;
	int numberExtensions = -1;
	int nexts;
	clock_t StartTime;
	double timeTook;

	while (*++argv != NULL) {
		if(!strcmp(*argv, "-v") || !strcmp(*argv, "--version")) {
			version();
			exit(0);
		} else if(!strcmp(*argv, "-h") || !strcmp(*argv, "--help")) {
			usage();
			exit(0);
		} else if(!strcmp(*argv, "-V") || !strcmp(*argv, "--verbose")) {
			verbosity = 2;
		} else if(!strcmp(*argv, "-m") || !strcmp(*argv, "--messages")) {
			verbosity = 1;
		} else if(!strcmp(*argv, "-q") || !strcmp(*argv, "--quiet")) {
			verbosity = 0;
		} else if(!strcmp(*argv, "-qq") || !strcmp(*argv, "--very-quiet")) {
			verbosity = -1;
		} else if(!strcmp(*argv, "-t") || !strcmp(*argv, "--time")) {
			timex = 1;
		} else if(!strcmp(*argv, "-n") || !strcmp(*argv, "--no-time")) {
			timex = 0;
		} else if(!strcmp(*argv, "-1") || !strcmp(*argv, "--single")) {
			numberExtensions = 1;
		} else if(!strcmp(*argv, "-a") || !strcmp(*argv, "--all")) {
			numberExtensions = 0;
		} else if(!strcmp(*argv, "-c") || !strcmp(*argv, "--count")) {
			if(*++argv == NULL || (numberExtensions = to_int(*argv)) < 0) {
				usage();
				exit(1);
			}
			if(numberExtensions == 0)
				numberExtensions = -1;
		} else if(!strncmp(*argv, "-c", 2)) {
			if((numberExtensions = to_int(*argv+2)) < 0) {
				usage();
				exit(1);
			}
		} else if(!strncmp(*argv, "--count=", 8)) {
			if((numberExtensions = to_int(*argv+8)) < 0) {
				usage();
				exit(1);
			}
		} else if((*argv)[0] == '-') {
			usage();
			exit(1);
		} else {
			filename = *argv;
		}		
	}
	
	if (filename == NULL) {
		usage();
		exit(1);
	}
	
	StartTime = clock();
	InitializeSimpleDefaultTheory(&theory, filename);
	nexts = FindExtensions(&theory, numberExtensions);
	if(verbosity != 0)
		printf("%d extension%s\n", nexts, nexts==1 ? "" : "s");
	timeTook = (double)(clock() - StartTime)/CLOCKS_PER_SEC;
	if(timex)
		printf("%f sec\n", timeTook);
	
	return 0;
}

/* Like atoi(), but returns a negative number if the string is non-numeric */
int to_int(const char *str)
{
	long result;
	char *end=NULL;
	if(str == NULL)
		return 0;
	
	result = strtol(str, &end, 0);

	if(*str != '\0' && *end == '\0')
		return (int)result;
	else
		return -1;
}

void usage(void) 
{
	printf("usage: lderes [options] filename\n");
	printf("Compute extensions of a simple default theory.\n\n");
	
	printf(" -a, --all             find all extensions (default)\n");
	printf(" -1, --single          find only the first extension\n");
	printf(" -c, --count=N         find the first N extensions\n");
	printf(" -t, --time            print timing information to stderr (default)\n");
	printf(" -n, --no-time         do not print timing information\n");
	printf(" -V, --verbose         print additional information to stderr\n");
	printf(" -m, --messages        print status messages to stderr (default)\n");
	printf(" -q, --quiet           do not print status information (other than\n");
	printf("                        timing and models)\n");
	printf(" -qq, --very-quiet     compute models but do not print them\n");
	printf(" -v, --version         print version information to stdout\n");
	printf(" -h, --help            display this message\n\n");

	printf("Input is read from filename.{dt,thc,dc}.\n");
}

void version(void) 
{
	printf("lderes %s, from lpsms %s\n", VERSION, LPSMSVER);
}

#endif /* _MAIN_C */
