//-----------------------------------------------------------------
// main.C : Main function for bus route simulation program.
// Written by Ali Ozmez for COM3360 project.
// Last update on Tue Nov 28 17:11:23 1995.
//-----------------------------------------------------------------

#include "proj.h"


void out_of_store()
{
  cerr << "proj: operator new failed, out of store\n";
  exit( 1 );
}

#ifndef __GNUC__
extern PF set_new_handler( PF );
#else
extern "C"  PF set_new_handler( PF );
#endif

/*
 * GEN_DIR is a global variable containing the path of the generation
 * environment. This path is used to find the cd-print and cd-parse
 * class dictionaries.
 * Do NOT delete the following statement.
 */

char* GEN_DIR = getenv( "GEN_DIR" );

int main( int argc, char* argv[], char* envp[] )
{
  // set new handler to catch out of memory exception
  set_new_handler( &out_of_store );

  //----------------------------------------
  // Parsing an object 
  //----------------------------------------
  // specify file to parse in 
  const int MAXPATH = 256;
  char Dem_input[MAXPATH];

  // Input file is first argument
  if( argc >= 2 )
     strcpy( Dem_input, argv[1] );
  else {
     strcpy( Dem_input, "demeter-input" );  // default input 
  }

  // parse it in 
  cout << "Parsing in object in: " << Dem_input << "." << endl;
  BusRoute* iBusRoute = new BusRoute();
  if ( ( BusRoute* ) iBusRoute -> g_parse( Dem_input ) == NULL )
  {
     cerr << "Parser error." << endl;
     exit(1);
  }
  cout << endl;

  //----------------------------------------
  // Your own code follows after this 
  //----------------------------------------
  int i, stime;

  // Simulation time is the second argument
  stime = (argc >= 3) ? atoi(argv[2]) : 30;

  // Display the initial state of the bus route
  cout << "Initial state of the input bus route : \n\n";
  iBusRoute->g_print();

  // Do the simulation here
  for (i=1; i < stime; i++) {
    cout << "\n\nNext state (after " << i << " minutes) : \n\n";
    iBusRoute->simulate();
    iBusRoute->g_print();
  }
    
  // Display the final state of the bus route
  cout << "\n\nFinal state of the input bus route (after ";
  cout << stime << " minutes) : \n\n";
  iBusRoute->simulate();
  iBusRoute->g_print();

  cout << "\n\n*** FINISHED ***" << endl;

  return ( 0 );
}

