/*
**  main
**
**   main loop and valve for programming contest 1
**
*/

/* 
**   Scott "Jerry" Lawrence
**   2000 January 21
**   j@absynth.com
*/

/*
** $Id: main.c,v 1.7 2000/02/08 22:32:52 sdlpci Exp sdlpci $
**
** $Log: main.c,v $
 * Revision 1.7  2000/02/08  22:32:52  sdlpci
 * fixed a dumb bug.
 *
 * Revision 1.6  2000/02/08  01:13:48  sdlpci
 * version id in a variable.
 *
 * Revision 1.5  2000/02/07  22:21:02  sdlpci
 * removed the conditional structure -- it was not needed.
 *
 * Revision 1.4  2000/01/25  05:18:57  sdlpci
 * *** empty log message ***
 *
 * Revision 1.4  2000/01/25  05:18:57  sdlpci
 * *** empty log message ***
 *
 * Revision 1.3  2000/01/24  16:39:39  sdlpci
 * re-orged code a little.
 *
 * Revision 1.2  2000/01/22  01:27:30  sdlpci
 * added dump & read code
 *
 * Revision 1.1  2000/01/21  22:48:55  sdlpci
 * Initial revision
 *
**
*/

#include <stdio.h>
#include <stdlib.h>  /* for srand() */
#include <time.h>    /* for srand(timer) */
#include "map.h"
#include "gp_strct.h"
#include "gp.h"

char version_id[] = "$Id: main.c,v 1.7 2000/02/08 22:32:52 sdlpci Exp sdlpci $";

int main(int argc, char ** argv)
{
    time_t t;
    FILE * input_file = NULL;
    INTMAP * the_map = NULL;

    /* seed the random number generator */
    srand((unsigned) time(&t));

    /*
    ** i've never done this before...
    **
    **  if there's an argument on the command line, assume it's an input
    **  file, and use that as the file we use.
    **  if there's no arg, or the fopen fails, then just read from stdin
    */
    if (argc == 2)
    {
	input_file = fopen(argv[1], "r");
	if (!input_file)
	{
	    printf ("open of file failed.  will use stdin instead.\n");
	    input_file = stdin;
	} else {
	    printf ("opened %s fine\n", argv[1]);
	}
	
    } else {
	input_file = stdin;
    }

    the_map = map_read_in_file(input_file);
    if (the_map)
    {
	map_dump(the_map);
    }

    gp_setup();

    // do stuff here
    gp_run(the_map);

    gp_upset();
    map_destroy(the_map);

    /* close the file if necessary... */
    if (input_file != stdin)
	fclose(input_file);

    return 0;
}
