//=========================================================================
// Summary  : Binary wrapper for Supervisor component.
// Filename : _supervisor.cc
// Author   : O'Reilly, Marsh
// Project  :
// Revision : 1
// Created  : 2000/08/19
// Modified : 2000/08/19
//=========================================================================
// Description :
//=========================================================================
#include <time.h>
#include <process.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/name.h>

#include "Supervisor.h"
#include "Syslog.h"
#include "VehicleConfigurator.h"
#include "VehicleConfigurationIF.h"
#include "SupervisorIF.h"
#include "Task.h"
#include "System.h"
#include "Syslog.h"

int main(int argc, char **argv, char **arge)
{
	// N.B. - Don't make any calls to VehicleConfigurator
	//        'til we start VehicleConfigurationServer.
	//        This includes Syslog::write()'s!

    Boolean simulated = False;
    Boolean realTailCone = True;

    char *planFileName = NULL;
    char *abortFileName = NULL;

    char umbrellaDirName[32];

    Boolean error = False;

    if ( System::checkLogDir( True ) )
    	exit(1);

    // This really has to be the first thing we do.
	if ( ! System::createVehicleLogDir() )
		exit(1);

	fprintf(stderr, "_supervisor - ran createVehicleLogDir()\n");

	// Next, create the umbrella log directory for this supervisor run...
	if ( ! System::createUmbrellaLogDir(umbrellaDirName) )
		exit(1);

	fprintf(stderr, "_supervisor - ran createUmbrellaLogDir, returned '%s'\n", umbrellaDirName);

	// Finally, set the link to the "inter-mission" directory
	if ( ! System::createLogDir(NULL, umbrellaDirName, False) )
		exit(1);

	fprintf(stderr, "_supervisor - ran createLogDir.\n");

	// Now we can proceed...

    //Syslog::write("Parsing args");

    for (int i = 1; i < argc; i++) {

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

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

		else if (!strcmp(argv[i], "-sim")) {
			simulated = True;
			realTailCone = False;
		}

		else if (!strcmp(argv[i], "-simtail")) {
			simulated = True;
			realTailCone = True;
		}

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

    if (error) {
		Syslog::write("usage: %s "
			"[-sim][-simtail][-plan file][-abort file][-dev file]",
			argv[0]);

		exit(1);
    }

    try {

		if ( System::findServer( VehicleConfigurationIFServerName ) < 0 ) {
			if ( System::spawn("vehicleConfigurationServer", (simulated) ? "-sim" : "", False ) == -1 ) {
				fprintf(stderr, "Couldn't start vehicleConfigurationServer");
				exit(-1);
			}
		}

		SupervisorIF *sup;

		try {
			Syslog::write("Spawning server %s",SupervisorIFServerName);
			System::findServerOrSpawn(SupervisorIFServerName,"supervisorServer","");

			Syslog::write("Creating interface");
			sup = new SupervisorIF("supervisor");

		}
		catch( SharedObjectClient::MissingServer e ) {
			Syslog::write("Couldn't connect to %s",
				  SupervisorIFServerName);
			exit(-1);

		}
		catch(...) {
			Syslog::write("Caught exception while creating %s"
				  SupervisorIFServerName );
		}

		Syslog::write("Spawning supervisorDriver");
		System::spawn("supervisorDriver", umbrellaDirName);

		sleep(1); //have to wait for the supervisorDriver to construct

		if ( planFileName != NULL )
		{
			if ( abortFileName != NULL )
			{
				Syslog::write("Setting mission");
				sup->setMission(planFileName,abortFileName);
			}
			else
			{
				Syslog::write("Setting mission");
				sup->setMission(planFileName);
			}

			Syslog::write("Setting flags (sim=%d, realTC=%d)",simulated, realTailCone);
			sup->setFlags(simulated, realTailCone);
			Syslog::write("Starting mission");
			sup->startMission();
		}

    }
    catch (Exception e) {
		Syslog::write("%s caught Exception: %s", argv[0], e.msg);
    }
    catch (...) {
		Syslog::write("%s caught unknown exception!", argv[0]);
    }

    return 0;
}
