//
// C/C++ headers
//
#include <iostream>
//
// ROOT headers
//
#include <TString.h>
#include <TSQLServer.h>
//
// This package headers
//
#include <MyDect1Core.h>
#include <MyDect1Verl2.h>
//
using namespace std;
//
// Usage subroutine
//
void usage(){
  printf("\nUsage:\n");
  printf("\n MyDetector1Level2 [-v] [-h] [--version] -idRun ID_RUN [-processFile filename] [-processFolder folder]\n");
  printf("\n --version       print informations about compilation and exit\n");
  printf("\n -h | --help     print this help and exit \n");
  printf("\n -v | --verbose  be verbose [default: print nothing on STDOUT]\n");
  printf("\n -idRun          ID_RUN: ID number of the run to be processed \n");
  printf("\n -processFile    filename : output filename [default ID_RUN.Level2.root]\n");
  printf("\n -processFolder  not used [default \"\"]\n");
  printf("\n Notice that parameter order does not matter. \n");
  printf("\nExample: \n\nMyDetector1Level2 -idRun 1085 -processFile nomefile.root\n\n");
};
//
// Here the main
//
int main(int numinp, char *inps[]){
  //
  // Variables booking
  //
  int nul = 0;
  Int_t error = 0;
  Bool_t beverbose = false;
  ULong64_t run = 0;
  TString filename;
  TSQLServer *dbc = 0;
  Bool_t givenid = false;
  //
  // Checking input parameters
  //
  if ( numinp > 1 ){
    for ( int i = 0; i < numinp; i++ ){
      if ( !strcmp(inps[i],"--version") ){
	MyDect1Info(true);
	exit(0);
      };
      if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){
	usage();
	exit(0);
      };
      if ( !strcmp(inps[i],"-idRun") ) {
	if ( numinp-1 < i+1 ) exit(-3);
	givenid = true;
	char *pEnd;	
	run = strtoull(inps[i+1],&pEnd,0);	
      };
      if ( !strcmp(inps[i],"-processFile") ) {
	if ( numinp-1 < i+1 ) exit(-3);
	filename = (TString)inps[i+1];	
      };
      if ( !strcmp(inps[i],"-v") || !strcmp(inps[i],"--verbose") ) beverbose = true;
    };
  } else {
    //
    // no input parameters exit with error, we need at least the run id.
    //
    printf(" MYDETECTOR1 - ERROR: you must provide a run number (at least -1)\n");
    exit(-1);
  };
  //
  // If not in verbose mode redirect to /dev/null the stdout and stderr
  //
  if ( !beverbose ){
    nul = open("/dev/null", O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
    dup2(nul,1);
    dup2(nul,2);
  };
  //
  // Check that an input run number has been given
  //
  if ( !givenid ) {
    printf(" MYDETECTOR1 - ERROR: you must provide a run number (at least -1)\n");
    exit(-1);
  };
  //
  char *version = MyDect1Info(false);
  //
  // Start:
  //
  printf("\n Welcome to the MyDetector1 LEVEL2 flight software, version %s \n",version);
  //
  // Connect to the DB
  //
  printf("\nConnecting to database... \n"); 
  //
  dbc = TSQLServer::Connect("mysql://localhost/pamelaprod","anonymous","");
  if( !dbc ) {
    printf(" MYDETECTOR1 - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n");
    exit(-2);    
  };
  bool connect = dbc->IsConnected();
  //
  if( !connect ) {
    printf(" MYDETECTOR1 - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n");
    exit(-2);    
  };
  printf("...connected! \n\n");
  //
  // Run the core program, put any output error in the "error" variable
  //
  error = MyDect1Core(run,filename,dbc);
  //
  // Close the DB connection
  //
  printf("\nClose the connection to the database... \n");
  dbc->Close();
  printf("...connection terminated!\n\n");   
  //
  // Close redirection if the case.
  //
  if ( !beverbose ) close(nul);
  //
  // Return "error"
  //
  if ( error > 0 ) printf(" MYDETECTOR1 - WARNING: exiting with signal %i \n\n",error);
  if ( error < 0 ) printf(" MYDETECTOR1 - ERROR: exiting with signal %i \n\n",error);
  exit(error);
}
