int main(int argc, char **argv) {
  const char *optstring = "hd:c:t:FLRUDS:";

  int o;
  int delay = 0;

  int device_type = 1;
  
  char *device = NULL;

  unsigned int set_event = 0;
  unsigned int set_fire = 0, set_left = 0, set_right = 0;
  unsigned int set_up = 0, set_down = 0, set_stop = 0;

  //---------------------------------------------------------------------------
  // Get args

  o = getopt(argc, argv, optstring);

  while (o != -1) {
    switch (o) {

    case 'h':
      usage(argv[0]);
      return 0;

    case 'd':
      debug_level = atoi(optarg);
      fprintf(stderr, "debug_level set to %d\n", debug_level);
      break;

    case 'c':
      device = optarg;
      set_event = 1;
      break;

    case 't':
      device_type = atoi(optarg);
      break;

    case 'F':
      set_fire = 1;
      break;

    case 'U':
      set_up = 1;
      break;

    case 'D':
      set_down = 1;
      break;

    case 'L':
      set_left = 1;
      break;

    case 'R':
      set_right = 1;
      break;

    case 'S':
      set_stop = 1;
      delay = atoi(optarg);
      break;

    }
    o = getopt(argc, argv, optstring);
  }

  //---------------------------------------------------------------------------

  if (missile_usb_initialise() != 0) {
    fprintf(stderr, "missile_usb_initalise failed: %s\n", strerror(errno));
    return -1;
  }
  
  control = missile_usb_create(debug_level, USB_TIMEOUT);
  if (control == NULL) {
    fprintf(stderr, "missile_usb_create() failed\n");
    return -1;
  }
  
  if (missile_usb_finddevice(control, 0, device_type) != 0) {
    fprintf(stderr, "USBMissileLauncher device not found\n");
    return -1;
  }

  if (debug_level)
    fprintf(stderr, "Now we're ready.  Move the thing around, and FIRE!\n");

  //---------------------------------------------------------------------------

  int fd;
  struct input_event ev[64];
  int events;

  if (set_event) {

    if (device == NULL) {
      fprintf(stderr, "No device given\n");
      usage(argv[0]);    
      return 0;
    }
    
    if (IEOpen(device, &fd) < 0) {
      fprintf(stderr, "IEOpen(%s, fd) failed\n", device);
      return -1;
    }

    while (1) {

      if (IERead(fd, ev, &events) < 0) {
	fprintf(stderr, "IERead() failed\n");
	return -1;
      }

      if (HandleKeyboardEvent(ev, events, device_type) < 0) {
	fprintf(stderr, "HandleKeyboardEvent() failed\n");
	return -1;
      }

      usleep(200000);
	
    }
    
  }

  //---------------------------------------------------------------------------

  char msg = 0x00;

  switch (device_type) {
    
  case DEVICE_TYPE_MISSILE_LAUNCHER:
  
    if (set_left)
      msg |= MISSILE_LAUNCHER_CMD_LEFT;
    
    if (set_right)
      msg |= MISSILE_LAUNCHER_CMD_RIGHT;
    
    if (set_up)
      msg |= MISSILE_LAUNCHER_CMD_UP;
    
    if (set_down)
      msg |= MISSILE_LAUNCHER_CMD_DOWN;
    
    if (set_fire)
      msg |= MISSILE_LAUNCHER_CMD_FIRE;

    missile_do(control, msg, device_type);
    
    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);
    }

    break;

  case DEVICE_TYPE_CIRCUS_CANNON:

    if (set_left)
      msg |= CIRCUS_CANNON_CMD_LEFT;
    
    if (set_right)
      msg |= CIRCUS_CANNON_CMD_RIGHT;
    
    if (set_up)
      msg |= CIRCUS_CANNON_CMD_UP;
    
    if (set_down)
      msg |= CIRCUS_CANNON_CMD_DOWN;
    
    if (set_fire)
      msg |= CIRCUS_CANNON_CMD_FIRE;

    missile_do(control, msg, device_type);

    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, CIRCUS_CANNON_CMD_STOP, device_type);
    }
      
    break;
    
  default:
    printf("Device Type (%d) not implemented, please do it!\n",
	   device_type);
    return -1;
    
  }

  missile_usb_destroy(control);  

  //---------------------------------------------------------------------------

  return 0;
}
int main(int argc, char **argv) {
  setbuf(stdout, NULL);
  const char *optstring = "hnFLRUDS:";

  int o;
  int delay = 0;

  int device_type = 1;

  unsigned int set_fire = 0, set_left = 0, set_right = 0;
  unsigned int set_up = 0, set_down = 0, set_stop = 0, set_net = 0;

  //---------------------------------------------------------------------------
  // Get args

  o = getopt(argc, argv, optstring);

  while (o != -1) {
    switch (o) {

    case 'h':
      usage(argv[0]);
      return 0;

    case 'n':
      set_net = 1;
      break;

    case 'F':
      set_fire = 1;
      break;

    case 'U':
      set_up = 1;
      break;

    case 'D':
      set_down = 1;
      break;

    case 'L':
      set_left = 1;
      break;

    case 'R':
      set_right = 1;
      break;

    case 'S':
      set_stop = 1;
      delay = atoi(optarg);
      break;

    }
    o = getopt(argc, argv, optstring);
  }

  //---------------------------------------------------------------------------

  if (missile_usb_initialise() != 0) {
    fprintf(stderr, "missile_usb_initalise failed: %s\n", strerror(errno));
    return -1;
  }
  
  control = missile_usb_create(debug_level, USB_TIMEOUT);
  if (control == NULL) {
    fprintf(stderr, "missile_usb_create() failed\n");
    return -1;
  }
  
  if (missile_usb_finddevice(control, 0, device_type) != 0) {
    fprintf(stderr, "USBMissileLauncher device not found\n");
    return -1;
  }

  //---------------------------------------------------------------------------

  char msg = 0x00;

  switch (device_type) {
    
  case DEVICE_TYPE_MISSILE_LAUNCHER:
  
    if (set_left)
      msg |= MISSILE_LAUNCHER_CMD_LEFT;
    
    if (set_right)
      msg |= MISSILE_LAUNCHER_CMD_RIGHT;
    
    if (set_up)
      msg |= MISSILE_LAUNCHER_CMD_UP;
    
    if (set_down)
      msg |= MISSILE_LAUNCHER_CMD_DOWN;
    
    if (set_fire)
      msg |= MISSILE_LAUNCHER_CMD_FIRE;

    missile_do(control, msg, device_type);
    
    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);
    }

    break;
    
  default:
    printf("Device Type (%d) not implemented, please do it!\n",
     device_type);
    return -1;
    
  }
  
  if(set_net){
  //UDPServer();
  TCPServer();
  }
  

  missile_usb_destroy(control);  

  //---------------------------------------------------------------------------

  return 0;
}