#include <errno.h>              /* errno and error codes */
#include <sys/time.h>           /* for gettimeofday() */
#include <unistd.h>             /* for gettimeofday(), getpid() */
#include <stdio.h>              /* for printf() */
#include <unistd.h>             /* for fork() */
#include <sys/types.h>          /* for wait() */
#include <sys/wait.h>           /* for wait() */
#include <signal.h>             /* for kill(), sigsuspend(), others */
#include <sys/ipc.h>            /* for all IPC function calls */
#include <sys/shm.h>            /* for shmget(), shmat(), shmctl() */
#include <sys/sem.h>            /* for semget(), semop(), semctl() */
#include <stdlib.h>             /* for atoi() */
#include "grabparent.h"
#include "grabber.h"

void printusage()
{
  printf("USAGE: grabber numcameras cam1source cam2source ... ");
  printf("brightness contrast\n");
  printf("\nWhere cam#source is either 0, 1, or 2, corresponding\n");
  printf("to RCA input, SVIDEO, or INTEL-CAMERA input, respectively.\n\n");
}

/*
 * The main function
 */
int main( int argc, char *argv[] )
{
        /*
         * Some needed variables
         */
        int rtn;
        int count;
	int i;

	signal(SIGINT, break_handler);
	signal(SIGQUIT, break_handler);
	signal(SIGKILL, break_handler);
	signal(SIGTERM, break_handler);

	/* Parse arguments */
	int num_cameras=1;
	int cameramode[6];
	char brightness;
	char contrast;

	cameramode[0] = 0;
	if (argc > 1)
	  {
	    num_cameras = atoi(argv[1]);
	    if (argc-4 != num_cameras)
	      {
		printusage();
		return(0);
	      }
	    for (i=0; i<num_cameras; i++)
	      {
		cameramode[i]=atoi(argv[2+i]);
	      }
	  }
	else
	  {
	    printusage();
	    return(0);
	  }
	
	brightness = (char)atoi (argv[argc - 2]);
	contrast = (char)atoi (argv[argc - 1]);

        /*
         * Store the process ids of the children so they can be killed
         */
        int pid[num_cameras];

        /*
         * Create all of the threads
         */
 
	rtn = 1;
        for( count = 0; count < num_cameras; count++ ) {
                if( rtn != 0 ) {
                        pid[count] = rtn = fork();
			if (rtn == -1)
			  {
			    printf("THREAD %d DID NOT START.  EXITING.\n",count);
			    exit(-1);
			  }
                } else {
		  break;
                }
        }
	
	
        /*
         * Check return value to determine if process is parent or child
         */
        if( rtn == 0 ) {
                /*
                 * I am a child
                 */
                printf("Thread %i started ...\n", count);
		switch (count)
		  {
		  case GRABBER1:
		    grabber(GRABBER1,cameramode[0], brightness, contrast);
		    break;
		  case GRABBER2:
		    grabber(GRABBER2,cameramode[1], brightness, contrast);
		    break;
		  case GRABBER3:
		    grabber(GRABBER3,cameramode[2], brightness, contrast);
		    break;
		  case GRABBER4:
		    grabber(GRABBER4,cameramode[3], brightness, contrast);
		    break;
		  case GRABBER5:
		    grabber(GRABBER5,cameramode[4], brightness, contrast);
		    break;
		  case GRABBER6:
		    grabber(GRABBER6,cameramode[5], brightness, contrast);
		    break;
		  }
		  
        } else {
	  /*
	   *  I am the parent
	   */
	  while(1)
	    {
	      pause();
	    }
	}
}

/*
 * This code is a break handler.  It automatically catches any 
 * signals and closes things down gracefully.
 */
void break_handler(int sig) {

  printf("Break detected...\n");
  exit(-1);
}







