/*****************************************************
**
**  S-PRES Copyright (c) 2010
**
**  Joshua T Berryman
**
**  This file is part of S-PRES
**
**  S-PRES 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 3 of the License, or
**  (at your option) any later version.
**
**  S-PRES 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, see <http://www.gnu.org/licenses/>.
**
**  If you make use of this program for research 
**  resulting in a publication, please cite:
**
**  Joshua T Berryman and Tanja Schilling, 
**  Sampling rare events in nonequilibrium and nonstationary systems. 
**  
**  J Chem Phys, 133(24):244101, Dec 2010.
**
******************************************************/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include "../../shi/src/shi.h"
#include "../../spres/src/spres.h"


//main loop for shi simulation
int main(int argc, char *argv[]){
    
    shi      Shi;
    shiStats Stats;

    bool     newSetup=false;

       
    // Exits if less than 2 arguments
    if (argc < 3) {
      cerr << "Too few arguments \n";
      Shi.usage();
    }

    while ((argc > 1) && (argv[1][0] == '-')) {

  switch (argv[1][1]) {
	    case 'n':              // new Setup
		newSetup=true;
		break;
	    case 'p' :
		Shi.parameterFileName.assign( &argv[1][2] );
		break;
	    case 'f':
		Shi.configFileName.assign( &argv[1][2] );
		break; 
	    case 'c' :
		Shi.restartFileName.assign( &argv[1][2] );
		break;
	    case 'o' :
		Shi.logFileName.assign( &argv[1][2] );
		break;
	    default:
		cerr << "Bad option " << argv[1] << '\n';
		Shi.usage();
	}

	++argv;
	--argc;
    }

    // test argument status 
    if( newSetup != true ) {
	    if( Shi.configFileName.empty() )
	    {
	        cerr << "Failed: require config file.\n" << '\n';
	        Shi.usage();
	    }
    }
    if( Shi.parameterFileName.empty() )
    {
	    cerr << "Failed: require parameter file.\n" << '\n';
	    Shi.usage();
    }

    //setup the stats-calculating class
    Shi.stats = &Stats;
    Stats.init();

    //initialise and setup the class based on files
    if( newSetup == false ) {
        cerr << "setting up from config file " <<  Shi.configFileName << endl;
        Shi.setupFromFile( (char *)Shi.configFileName.c_str() );
    }
    else{
        cerr << "setting up new system" << endl;
        Shi.setupNew();
    }

    //run the simulation
    Shi.run();
    
    return( EXIT_SUCCESS );

}




void shi::usage(){

    cerr << "Usage is shi.exe [-n -f<ConfigFile>]\n"
         << "                  -p<ParameterFile>\n"
         << "                  -o<LogFile>\n"
         << "                  -c<outputTrajectoryFile>\n";
    exit( EXIT_FAILURE );

}












