# include "dvomerge.h"

/*** check for command line options ***/
int dvomerge_args (int *argc, char **argv) {
  
  int N;

  /* extra error messages */
  VERBOSE = FALSE;
  if ((N = get_argument (*argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-photcode-file"))) {
    remove_argument (N, argc, argv);
    ALTERNATE_PHOTCODE_FILE = strcreate(argv[N]);
    remove_argument (N, argc, argv);
  }

  /* replace measurement, don't duplicate */
  REPLACE_BY_PHOTCODE = FALSE;
  if ((N = get_argument (*argc, argv, "-replace"))) {
    REPLACE_BY_PHOTCODE = TRUE;
    remove_argument (N, argc, argv);
  }

  NTHREADS = 0;
  if ((N = get_argument (*argc, argv, "-threads"))) {
    remove_argument (N, argc, argv);
    NTHREADS = MAX(0, atoi(argv[N]));
    remove_argument (N, argc, argv);
  }

  /*** provide additional data ***/ 
  /* restrict to a portion of the sky? */
  UserPatch.Rmin = 0;
  UserPatch.Rmax= 360;
  UserPatch.Dmin = -90;
  UserPatch.Dmax = +90;
  if ((N = get_argument (*argc, argv, "-region"))) {
    remove_argument (N, argc, argv);
    UserPatch.Rmin = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Rmax = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Dmin = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Dmax = atof (argv[N]);
    remove_argument (N, argc, argv);
  }

  // XXX for the moment, make this selection manual.  it needs to be automatic 
  // based on the state of the SkyTable
  PARALLEL = FALSE;
  if ((N = get_argument (*argc, argv, "-parallel"))) {
    PARALLEL = TRUE;
    remove_argument (N, argc, argv);
  }
  // this is a test mode : rather than launching the remote jobs and waiting for completion,
  // relphot will simply list the remote command and wait for the user to signal completion
  PARALLEL_MANUAL = FALSE;
  if ((N = get_argument (*argc, argv, "-parallel-manual"))) {
    PARALLEL = TRUE; // -parallel-manual implies -parallel
    PARALLEL_MANUAL = TRUE;
    remove_argument (N, argc, argv);
  }
  // this is a test mode : rather than launching the relphot_client jobs remotely, they are 
  // run in serial via 'system'
  PARALLEL_SERIAL = FALSE;
  if ((N = get_argument (*argc, argv, "-parallel-serial"))) {
    if (PARALLEL_MANUAL) {
      fprintf (stderr, "ERROR: cannot mix -parallel-manual and -parallel-serial\n");
      exit (1);
    }
    PARALLEL = TRUE; // -parallel-serial implies -parallel
    PARALLEL_SERIAL = TRUE;
    remove_argument (N, argc, argv);
  }

  if ((*argc < 4) || (*argc > 6)) dvomerge_usage();
  return TRUE;
}

/*** check for command line options ***/
int dvomerge_client_args (int *argc, char **argv) {
  
  int N;

  // by definition, the client is not parallel 
  PARALLEL = FALSE;
  PARALLEL_MANUAL = FALSE;
  PARALLEL_SERIAL = FALSE;

  HOST_ID = 0;
  if ((N = get_argument (*argc, argv, "-hostID"))) {
    remove_argument (N, argc, argv);
    HOST_ID = atoi (argv[N]);
    remove_argument (N, argc, argv);
  }
  if (!HOST_ID) dvomerge_client_usage();

  HOSTDIR = NULL;
  if ((N = get_argument (*argc, argv, "-hostdir"))) {
    remove_argument (N, argc, argv);
    HOSTDIR = strcreate (argv[N]);
    remove_argument (N, argc, argv);
  }
  if (!HOSTDIR) dvomerge_client_usage();

  /* extra error messages */
  VERBOSE = FALSE;
  if ((N = get_argument (*argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, argc, argv);
  }

  /* replace measurement, don't duplicate */
  REPLACE_BY_PHOTCODE = FALSE;
  if ((N = get_argument (*argc, argv, "-replace"))) {
    REPLACE_BY_PHOTCODE = TRUE;
    remove_argument (N, argc, argv);
  }

  NTHREADS = 0;
  if ((N = get_argument (*argc, argv, "-threads"))) {
    remove_argument (N, argc, argv);
    NTHREADS = MAX(0, atoi(argv[N]));
    remove_argument (N, argc, argv);
  }

  /*** provide additional data ***/ 
  /* restrict to a portion of the sky? */
  UserPatch.Rmin = 0;
  UserPatch.Rmax= 360;
  UserPatch.Dmin = -90;
  UserPatch.Dmax = +90;
  if ((N = get_argument (*argc, argv, "-region"))) {
    remove_argument (N, argc, argv);
    UserPatch.Rmin = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Rmax = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Dmin = atof (argv[N]);
    remove_argument (N, argc, argv);
    UserPatch.Dmax = atof (argv[N]);
    remove_argument (N, argc, argv);
  }

  if ((*argc < 4) || (*argc > 6)) dvomerge_client_usage();
  return TRUE;
}

/*** check for command line options ***/
int dvoconvert_args (int *argc, char **argv) {
  
  int N;

  /* extra error messages */
  VERBOSE = FALSE;
  if ((N = get_argument (*argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, argc, argv);
  }

  if (*argc != 4) dvoconvert_usage();
  return TRUE;
}

/*** check for command line options ***/
int dvosecfilt_args (int *argc, char **argv) {
  
  int N;

  /* extra error messages */
  VERBOSE = FALSE;
  if ((N = get_argument (*argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, argc, argv);
  }

  if (*argc != 3) dvosecfilt_usage();
  return TRUE;
}
