/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library 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 Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

/*
 * Fuzzy Rules Compiler
 *
 * $Id: fuzzymain.c,v 1.6 2000/04/27 23:37:28 c-medina Exp $
 *
 * main
 */

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

#define PROGRAM "FuzzyLibFRC"

#define MAXPATH 128


FILE *hfile;
FILE *cfile;
FILE *in;
char *in_name;


char output[MAXPATH] = "Fuzzy";
char buf[MAXPATH];

extern char *optarg;
extern int optind;

extern int yyparse();
extern void yyupdate(void);
extern FILE *yyin;

void open_in()
{
    if(!strcmp(in_name, "-"))
    {
	in = stdin;
	strcpy(in_name, "stdin");
    }
    else if((in = fopen(in_name, "r")) == NULL)
    {
	fprintf(stderr, "%s: can't open %s for reading\n", PROGRAM, in_name);
	exit(1);
    }
    yyin = in;
}

void close_in()
{
    fclose(in);
}

void open_hfile()
{
    sprintf(buf, "%s.h", output);
    if((hfile = fopen(buf, "w")) == NULL)
    {
	fprintf(stderr, "%s: can't open %s for writing\n", PROGRAM, buf);
	exit(1);
    }
    fprintf(hfile, "// -*- C++ -*-\n");
    fprintf(hfile, "/*\n");
    fprintf(hfile, " * Automatically generated from %s\n", in_name);
    fprintf(hfile, " *\n");
    fprintf(hfile, " * Do not edit !!!\n");
    fprintf(hfile, " */\n\n");
  fprintf(hfile, "#include <stdlib.h>\n");
    fprintf(hfile, "#include <FlFRuleBase.h>\n");
    fprintf(hfile, "#include <FlFSet.h>\n");
    fprintf(hfile, "#include <FlFSetTrapez.h>\n");
    fprintf(hfile, "#include <FlFVar.h>\n\n");
}

void close_hfile()
{
    fprintf(hfile, "};\n");

    fclose(hfile);
}

void open_cfile()
{
    sprintf(buf, "%s.C", output);
    if((cfile = fopen(buf, "w")) == NULL)
    {
	fprintf(stderr, "%s: can't open %s for writing\n", PROGRAM, buf);
	exit(1);
    }
    fprintf(cfile, "// -*- C++ -*-\n");
    fprintf(cfile, "/*\n");
    fprintf(cfile, " * Automatically generated from %s\n", in_name);
    fprintf(cfile, " *\n");
    fprintf(cfile, " * Do not edit !!!\n");
    fprintf(cfile, " */\n\n");
    fprintf(cfile, "#include \"%s.h\"\n\n", output);
}

void close_cfile()
{
    fprintf(cfile, "};\n");

    fclose(cfile);
}

void usage()
{
    fprintf(stderr, "usage: %s [-o output] rules\n", PROGRAM);
    exit(1);
}

int main(int argc, char *argv[])
{
#ifndef __linux__
    int c;

    while (1)
    {
	c = getopt(argc, argv, "o:");
        if (c  == EOF)
	    break;
	switch(c)
	{
	case 'o':
	    strcpy(output, optarg);
	    break;
	default:
	    usage();
	    break;
	}
    }
    

    if(optind != argc-1)
	usage();
#else
    if (argc != 2 && argc != 4)
    {
	usage();
    }
    else if (argc == 2)
    {
	optind = 1;
    }
    else
    {
	if (!strcmp(argv[1], "-o"))
	{
	    strcpy(output, argv[2]);
	    optind = 3;
	}
	else if (!strcmp(argv[2], "-o"))
	{
	    strcpy(output, argv[3]);
	    optind = 1;
	}
	else
	{
	    usage();
	}
    }
#endif

    in_name = argv[optind];

    open_in();
    open_hfile();
    open_cfile();

    yyparse();

    yyupdate();         /* Adds the updateFuzzySets method to the generated code */
    close_hfile();
    close_cfile();
    close_in();

    exit(0);
}


