/****************************************************************************/
/* Copyright (c) 2000 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : layeredControl.cc                                             */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 02/07/2000                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <process.h>
#include <stdlib.h>
#include <signal.h>
#include "LayeredControl.h"
#include "Syslog.h"
#include "System.h"

void main(int argc, char **argv)
{
  char *planName = NULL;
  char *abortPlanName = "abortMplan.cfg";

  Boolean error = False;

  // Set task priority
  if (System::setTaskPriority(LayeredControlPriority) == -1)
    exit(1);

  for (int i = 1; argv[i] != NULL; i++) {

    if (!strcmp(argv[i], "-plan")) {
	 printf("I've got a new plan: %s\n", argv[i+1] );
      if (i < argc - 1)
	planName = argv[++i];
      else
	error = True;
    }

    else if (!strcmp(argv[i], "-abort")) {
      if (i < argc - 1)
	abortPlanName = argv[++i];
      else
	error = True;
    }

    else {
      Syslog::write("Illegal option: %s", argv[i]);
      error = True;
    }
  }

  // Don't run if you haven't been given a mission
  if( planName == NULL ) {
       Syslog::write("No plan file specified for layeredControl");
       error = True;
  }

  if (error) {
    Syslog::write("usage: %s [-plan file][-abort file]", argv[0]);
    exit(1);
  }

  char normalPlanFile[256];
  char abortPlanFile[256];
  
  strcpy(normalPlanFile, System::missionPlanFile(planName));
  strcpy(abortPlanFile, System::missionPlanFile(abortPlanName));

  try {

    System::copyToLogDir(normalPlanFile);
    System::copyToLogDir(abortPlanFile);

    LayeredControl *layeredControl = 
      new LayeredControl(normalPlanFile, abortPlanFile, 200);

    layeredControl->run();
  }
  catch (SharedObject::Error e) {
    Syslog::write("layeredControl - caught SharedObject::Error %s", e.msg);
  }
  catch (Task::Error e) {
    Syslog::write("layeredControl - caught Task::Error %s", e.msg);
  }
  catch (...) {
    Syslog::write("layeredControl - caught exception!\n");
    exit(1);
  }

  exit(0);
}



