/*************************************************************************************************
** Project:     FSMGenerator
** Package:     FSMGenerator
** Module:      FSMGenerator::main
** File:        FSMMain.cpp
** Date:        2002.08.01
** Author:      Pavel Bekkerman (chpavel@tx.technion.ac.il)
** Copyright:   Copyright (C) 2002 Pavel Bekkerman
** License:
**              This program is free software; you can redistribute it and/or
**              modify it under the terms of the GNU General Public License as
**              published by the Free Software Foundation; either version 2 of
**              the License, or (at your option) any later version.
**
**              This program 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 General Public License for more details.
**
**              You should have received a copy of the GNU General Public License
**              along with this program; if not, write to the Free Software
**              Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
** Contact:     Pavel Bekkerman (chpavel@tx.technion.ac.il)
*************************************************************************************************/

#include "FSMCommon.h"
#include "scanner/FSMScanner.h"
#include "db/FSMDB.h"
#include "parser/FSMParser.h"
#include "generator/FSMCGenerator.h"
#include "generator/FSMCppGenerator.h"
#include "generator/FSMJavaGenerator.h"

#include <iostream>

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

using namespace FSMGenerator;
using namespace std;

/*
**	TODO:
**			1) Add relevant text to the generated-files headers (title, details ...).
**			2) What to do with the static files (FSMException.java, IDPair.java and etc.) ?
*/

void print_usage( char *strApplication );

#define LANGUAGE_MAX	3
char* g_strLanguage[LANGUAGE_MAX] = { "C", "Cpp", "Java" };

int _isLanguage( char *strLanguage )
{
	for( int i = 0; i<LANGUAGE_MAX; i++ )
	{
		if(!strcmp( g_strLanguage[i], strLanguage ))
			return i;
	}

  return -1;
}

void print_usage( char *strApplication )
{
        cout << "Usage: " << strApplication << " <input_file> <output_language> <output_path>" << endl;
	cout << "<input_file>      - The path to the file with FSM scecifications." << endl;
	cout << "<output_language> - Defines the language of the resulting source code (currently - C or Cpp or Java)." << endl;
	cout << "<output_path>     - The path to the resulting source code." << endl;
}

void print_license( 
	char *strProject,
	char *strPackage,
	char *strModule,
	char *strFile,
	char *strDate,
	char *strAuthor,
	char *strCopyright,
	char *strContact
)
{
	cout << "************************************************************************************************" << endl;
	cout << "** Project:	" << strProject << endl;
	cout << "** Package:	" << strPackage << endl;
	cout << "** Module:	" << strModule << endl;
	cout << "** File:	" << strFile << endl;
	cout << "** Date:	" << strDate << endl;
	cout << "** Author:	" << strAuthor << endl;
	cout << "** Copyright:	" << strCopyright << endl;
	cout << "** License:	" << endl;
	cout << "**             This program is free software; you can redistribute it and/or                    " << endl;
	cout << "**             modify it under the terms of the GNU General Public License as                   " << endl;
	cout << "**             published by the Free Software Foundation; either version 2 of                   " << endl;
	cout << "**             the License, or (at your option) any later version.                              " << endl;
	cout << "**                                                                                              " << endl;
	cout << "**             This program is distributed in the hope that it will be useful,                  " << endl;
	cout << "**             but WITHOUT ANY WARRANTY; without even the implied warranty of                   " << endl;
	cout << "**             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    " << endl;
	cout << "**             GNU General Public License for more details.                                     " << endl;
	cout << "**                                                                                              " << endl;
	cout << "**             You should have received a copy of the GNU General Public License                " << endl;
	cout << "**             along with this program; if not, write to the Free Software                      " << endl;
	cout << "**             Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.      " << endl;
	cout << "**                                                                                              " << endl;
	cout << "** Contact:	" << strContact << endl;
	cout << "************************************************************************************************" << endl;
}

int
main( int argc, char **argv )
{
	print_license(
		"FSMGenerator",
		"FSMGenerator",
		"",
		"fsmgenerator",
		"2002.08.01",
		"Pavel Bekkerman (chpavel@tx.technion.ac.il)",
		"Copyright (C) 2002 Pavel Bekkerman",
		"Pavel Bekkerman (chpavel@tx.technion.ac.il)"
	);

	if( argc != 4 )
	{
		print_usage( argv[0] );
		return( ERROR_FAILURE );
	}

	FSMDB *pdbFSM = parse( argv[1] );
	if( pdbFSM == NULL )
	{
		return( ERROR_FAILURE );
	}

	FSMFileGenerator *pGenerator = NULL;
	switch( _isLanguage( argv[2] ) )
	{
		case 0:
			pGenerator = new FSMCGenerator( *pdbFSM, argv[3] );
		break;

		case 1:
			pGenerator = new FSMCPPGenerator( *pdbFSM, argv[3] );
		break;

		case 2:
			pGenerator = new FSMJavaGenerator( *pdbFSM, argv[3] );
		break;

		default:
		{
			delete( pdbFSM );
			return( ERROR_SUCCESS );
		}
	}

	if( pGenerator == NULL )
	{
		delete( pdbFSM );
		return( ERROR_SUCCESS );
	}

	if(!pGenerator->GenerateFiles())
	{
		cout << "Failed to Generate the Files !" << endl;
	}
	else
	{
		cout << "Success !" << endl;
	}

	delete( pdbFSM );
	delete( pGenerator );
	return( ERROR_SUCCESS );
}

