/***************************************************************************
 *   Copyright (C) 2000-2010 by Noel Novelli                               *
 *   novelli@lif.univ-mrs.fr                                               *
 *                                                                         *
 *   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.             *
 ***************************************************************************/


#include <iostream>
#include <string>
#include "parseCmdLine.h"
#include "partitionCFUN.h"

using namespace std;

string gbl_sOutputFileName;

CFunCmdLine::CFunCmdLine( int argc, char *argv[] )
{
	_cDelim       = '\0';
	_bPrune       = true;
    //_minSup       = 0;
    _minSupCFD    = 0;
	_bWaitUser    = false;
	_bStdOutput   = false;

	_errorCmdLine = parseCmdLine( argc, argv );
}

void CFunCmdLine::usage( char *exeName )
{
	cout << "INPUT FILE FORMAT:\n"
			"------------------\n"
		    "The format of input files is CSV using a column delimiter.\n"
			"The first line must contain the column (or attribute) names\n"
			"using one character as column delimiter.\n"
			"If the option -d is not set, the first line is also used to automaticly compute the delimiter (among: ',' ';' 'tab character')."
		 << endl
		 << endl;

	cout << "USAGE:\n"
			"------\n"
		 << exeName << " <datafile> [options]"
		 << endl
		 << endl
	     << "OPTIONS:\n"
			"--------\n"
	     << "   -d <one and only one character as delimiter>\n"
			"      This is optional. The default is the automatic detection.\n"
			"   -noprune (To disable the pruning rules of levels and partitions)\n"
			"   -minSupCFD <minSup of CFDs (number of tuples)> (default is 1)\n"
			"   -waitUser (Wait until the ENTER key is pressed between two levels)\n"
			"   -o (use the standard output for printing all CFDs discovered)"
         << endl
		 << endl;
}

int CFunCmdLine::parseCmdLine( int argc, char *argv[] )
{
	if ( argc < 2 )
		return -1; // Nb arg trop insuffisant

	// On commence a 2 car on saute le nom de l'exec et celui de la relation (obligatoire)
	for ( int i = 2; i < argc; ++i ) {
		string sCmdLine ( argv[i] );

		if ( sCmdLine == "-d" ) {
			if ( _cDelim != '\0' )
				return i; // Delim deja traite

            if ( i+1 >= argc )
                return i;
			_cDelim = argv[i+1][0];
			++i;
			continue;
		}

		if ( sCmdLine == "-noprune" ) {
			if ( _bPrune != true )
				return i; // noprune deja traite

            _bPrune = false;
			continue;
		}

		//if ( sCmdLine == "-minSup" ) {
  //          if ( _minSup != 0 )
		//		return i; // minSup deja traite

  //          if ( i+1 >= argc )
  //              return i;
		//	_minSup = atoi ( argv[i+1] );
		//	if ( _minSup <= 0 )
		//		return i+1;
		//	++i;
		//	continue;
		//}

		if ( sCmdLine == "-minSupCFD" ) {
            if ( _minSupCFD != 0 ) {
                return i; // CFD deja traite
            }

            if ( i+1 >= argc )
                return i;
			_minSupCFD = atoi ( argv[i+1] );
			if ( _minSupCFD <= 0 )
				return i+1;
			++i;
            continue;
        }

		if ( sCmdLine == "-waitUser" ) {
			_bWaitUser = true;
			continue;
		}

		if ( sCmdLine == "-o" ) {
			_bStdOutput = true;
			continue;
		}

		if ( sCmdLine == "-f" ) {
			if ( i+1 >= argc || !gbl_sOutputFileName.empty() )
				return i;
			
			gbl_sOutputFileName = argv[i+1];
			++i;
			continue;
		}
		
		return i;
	}

 //   if ( _minSup == 0 ) {
 //       _minSup = cst_uDefaultMinSup; // Elegage des partitons
	//}
    if ( _minSupCFD == 0 ) {
        _minSupCFD = cst_uDefaultMinSupCFD; // Support min des CFD
	}
    //if ( _minSupCFD < _minSup ) {
    //    _minSupCFD = _minSup;
    //}

	return 0;
}
