/* monx.cc --- "main" for the lap suite (rev 3 -- after lisp and perl)

Joanna Bryson, Feb 1997

$Log: grasp.cc,v $
Revision 1.1  1999/12/27 23:48:46  joanna
Initial revision

Revision 1.3  1997/09/10 01:36:43  joanna
works with robs stuff -- fixed loads more chaining problems,
new rob libraries.  Interactive rob ringrose, not piped yet.

Revision 1.1  1997/08/10 18:34:11  joanna
Initial revision

Revision 1.5  1997/06/19 22:41:07  jo
fairly well working route building stuff

*/

#include <stdio.h>
#include <stream.h>
#include <GetOpt.h>
#include <signal.h>
#include "monx.hh"
#include "brain-objs.hh"
#include "brain.def"            // define the static/class variables

// #include "monx-dirattn.hh"
// #include "monx.def"             // define the static/class variables

static char rcsid[] =  "$Id: grasp.cc,v 1.1 1999/12/27 23:48:46 joanna Exp joanna $";

#define HTZ 1  // report cycle rate (already an HZ somewhere)


char *LOG_PATH = "./logs";
char *IOF_PATH = "./iofiles";	// lap and landmark files

char *hostname = 0;  // see usage, grasp-prims

main (int argc, char *argv[]) {

  // read options
  char opt, *lapfilename = 0;
  
  GetOpt gopt(argc, argv, "f:l:h:");
  while ((opt = gopt()) != -1) {
    switch (opt) {
    case 'f':      
      lapfilename = gopt.optarg;
      break;
    case 'h': 
      hostname = gopt.optarg;
      break;
    default:
      cerr << "Error:  in main, unknown option " << opt << "\n";
      Usage(argv[0]);
      break;
    } // switch opt
  } // while gopt

  if (!(lapfilename)) {
      cerr << "Error:  in main, no lapfile provided (argument -l) \n";
      Usage(argv[0]);
    }
  if (!(hostname)) {
      cerr << "Warning:  in main, no hostname provided (argument -h) \n";
      cerr << "          will just talk to screen.\n";
    }
/*  Do this again some day...
  // catch interrupt (control C), call an interactive menu routine
  void (*INTerface)(int);
  if ((int) signal (SIGINT, INTerface) < 0) { // INTerface is a function name
    cerr << "Error:  in main, couldn't handle SIG_INT\n";
  }
*/

/* 

################
### Time to run the program...


# now we need to read in the LAPs...

*/

  //  initclasstypes();   // set up the class variables no more! [see monx.def]
  initprims();                         // add the primitives (senses and acts)

  read_brains(lapfilename, IOF_PATH);  // does its own error checking

/* 
# and initialize the robot...   Er, any equiv for monx?

*/

  // get a life
  Drive * life = Drive::get_drive("life");
  life = (Drive *) life->instantiate();

  // Comp * life = Comp::get_comp("grasp");
  // life = (Comp *) life->instantiate();

#ifdef HTZ    // test Hz
  int count = 0; float ttt;
  struct timeval tv, xv;
  gettimeofday(&tv, 0);
#endif

  cerr << "Starting up... life =>\n";
  int res;  // save its result!
 RESTART: 
  while ((res = life->execute()) == CONTINUE)
#ifdef HTZ    // test Hz
    if (count++ == 10000) {
      gettimeofday (&xv, 0);
      ttt = xv.tv_sec - tv.tv_sec + (xv.tv_usec - tv.tv_usec) / 1000000.0;
      cerr << "Running at " << 10000/ttt << "Hz.\n";
      count = 0;  gettimeofday(&tv, 0);
    } // if report Hz
#else
;
#endif
      
  cerr << "Life ended!!!\n";             // should never happen...

  switch (res) {
  case SUCCESS:
    cout << "Yaaay, we won!\n"; break;
  case FAILURE:
    cout << "Boooo, we lost!\n"; break;
  default:
    cout << "Huh?";
  } // switch res

} /* main */

void
Usage(const char* prog) {
  cerr << "\nUsage:  " << prog << " -f <lapfile> [-l <landmarkfile>]\n" <<
    "     -f lapfile:      filename for competence and behavior sequence script,"
       <<"\n                      `.lap' will be appended.\n"
       <<"     -h hostname:      where the behavior library / server is running,"
       << "\n";
  ::exit (-1);
} /* Usage */

