#include <signal.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Syslog.h"
#include "System.h"
#include "TerrainAidIF.h"
#include "TerrainAidDriver.h"

#define NGULPERS 10

void signalHandler(int signalNo);

pid_t terrainAidPID;

void signalHandler(int signalNo)
{
  kill(terrainAidPID, SIGTERM);
  kill(0, SIGQUIT);
  exit(0);
}

int main(int argc, char **argv)
{
  //
  //char argString[100];
  char *argString = "-test";
  DeviceIF::Status status;

  signal(SIGQUIT, signalHandler);

  printf("Starting terrainAidServer ...\n");
  if ( (terrainAidPID = System::spawn("terrainAidServer", argString))
       == -1) 
  {
    fprintf(stderr, "System::spawn(terrainAidServer) failed\n");
    exit(-1);
  }
  //
  // Now get a handle to the server IF
  //
  TerrainAidIF *terrainAid;
  try {
    terrainAid = new TerrainAidIF("TerrainAid");
  }
  catch (Exception e) 
  {
    Syslog::write(
        "terrainAidTest -- Caught exception on creation of TerrainAidIF:%s\n",
		  e.msg);
    exit( -1 );
  }

  TimeIF::TimeSpec updateTime;
  char str[32];

  char buf[256];
  char *helpStr = "Valid commands are:\n"
                  "   help   - Help\n"
                  "   cnt    - Number of driver iterations.\n"
                  "   quit   - Quit.\n";
  while (True) 
  {
     printf("TerrainAidTest> ");
     if (gets(buf) == 0) continue;  // Some error occurred

     if ( !strncmp(buf, "help", 1) || !strncmp(buf, "Help", 1) || 
	  !strncmp(buf, "" , 1) )
     {
	printf("%s",helpStr);
     }
     else if (!strncmp(buf, "cnt", 3 )) 
     {
	printf("Number of samples: %d\n", 
	       terrainAid->counter() );
     }
#if 0
     else if (!strncmp(buf, "fire", 2 )) 
     {
	int   index;
	char *token = strtok(buf, " \t");
	while( token = strtok(0, " \t") )
	{
	   index = atoi(token);
	   if( index < 0 || index >= NGULPERS )
	   {
	      printf("Illegal gulper number %d\n", index);
	      break;
	   }
	   gulper->fireGulper(index);
	   printf("Fire request for gulper %d\n", index);
	}	   
     }
     else if (!strncmp(buf, "state", 2 )) 
     {
	terrainAid->state( gulperState );
	for( int i=0; i<NGULPERS; i++ )
	{
	   if( gulperState[i] == TerrainAidIF::Unarmed )
	      strcpy( str, "Unarmed" );
	   else if( gulperState[i] == TerrainAidIF::Armed )
	      strcpy( str, "Armed" );
	   else if( gulperState[i] == TerrainAidIF::Fired )
	      strcpy( str, "Fired" );
	   else printf("Gulper%d is in an unknown state.\n", i);
	   printf("Gulper%d is %s.\n", i, str);
	}
     }
     else if (!strncmp(buf, "arm", 3 )) 
     {
	//
	// First zero out the array
	for( int j=0; j<NGULPERS; j++ ) gulpersToArm[j] = False;

	int   index;
	char *token = strtok(buf, " \t");
	while( token = strtok(0, " \t") )
	{
	   index = atoi(token);
	   if( index < 0 || index >= NGULPERS )
	   {
	      printf("Illegal gulper number %d\n", index);
	      break;
	   }
	   gulpersToArm[index] = True;
	   printf("Arm gulper %d\n", index);
	}	   
	terrainAid->armGulper(gulpersToArm);
     }
#endif
     else if ( !strncmp(buf, "q", 1 ) || !strncmp(buf, "Q", 1 ) ) break;
     else printf(" Error: Command unrecognized.\n %s", helpStr);
  }

  // Just wait for termination signal
  signalHandler(0);
  return 0;
}

