# 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);
  }

  /* verify merge status of output tables, but do not modify */
  VERIFY = FALSE;
  VERIFY_CATALOG_ONLY = FALSE;
  if ((N = get_argument (*argc, argv, "-verify"))) {
    VERIFY = TRUE;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-check-only"))) {
    VERIFY = TRUE;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-verify-catalogs"))) {
    VERIFY = TRUE;
    VERIFY_CATALOG_ONLY = TRUE;
    remove_argument (N, argc, argv);
  }

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

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

  /* use a different photcode file to define mean values */
  if ((N = get_argument (*argc, argv, "-photcode-file"))) {
    remove_argument (N, argc, argv);
    ALTERNATE_PHOTCODE_FILE = strcreate(argv[N]);
    remove_argument (N, argc, argv);
  }

  // merge even if header claims db has already been merged 
  FORCE_MERGE = FALSE;
  if ((N = get_argument (*argc, argv, "-force-merge"))) {
    FORCE_MERGE = TRUE;
    remove_argument (N, argc, argv);
  }

  UPDATE_CATFORMAT = NULL;
  if ((N = get_argument (*argc, argv, "-update-catformat"))) {
    remove_argument (N, argc, argv);
    UPDATE_CATFORMAT = 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);
  }

  // is the input database a parallel db?
  PARALLEL_INPUT = FALSE;
  if ((N = get_argument (*argc, argv, "-parallel-input"))) {
    PARALLEL_INPUT = TRUE;
    remove_argument (N, argc, argv);
  }

  // is the output database a parallel db?
  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;
  IMAGES_ONLY = FALSE;

  // is the input database a parallel db?
  PARALLEL_INPUT = FALSE;
  if ((N = get_argument (*argc, argv, "-parallel-input"))) {
    PARALLEL_INPUT = TRUE;
    remove_argument (N, argc, argv);
  }

  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);
  }

  /* verify merge status of output tables, but do not modify */
  VERIFY = FALSE;
  if ((N = get_argument (*argc, argv, "-verify"))) {
    VERIFY = TRUE;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-check-only"))) {
    VERIFY = TRUE;
    remove_argument (N, argc, argv);
  }
  if ((N = get_argument (*argc, argv, "-verify-catalogs"))) {
    VERIFY = TRUE;
    VERIFY_CATALOG_ONLY = TRUE;
    remove_argument (N, argc, argv);
  }

  // merge even if header claims db has already been merged 
  FORCE_MERGE = FALSE;
  if ((N = get_argument (*argc, argv, "-force-merge"))) {
    FORCE_MERGE = TRUE;
    remove_argument (N, argc, argv);
  }

  /* extra error messages */
  MATCHED_TABLES = FALSE;
  if ((N = get_argument (*argc, argv, "-matched-tables"))) {
    MATCHED_TABLES = 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;

  HOST_ID = 0;

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

  SINGLE_CPT = NULL;
  if ((N = get_argument (*argc, argv, "-cpt"))) {
    remove_argument (N, argc, argv);
    SINGLE_CPT = strcreate (argv[N]);
    remove_argument (N, argc, argv);
  }

  /* specify portion of the sky : allow default of all 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);
  } 

  // is the output database a parallel db?
  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 != 3) dvosecfilt_usage();
  return TRUE;
}

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

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

  SINGLE_CPT = NULL;
  if ((N = get_argument (*argc, argv, "-cpt"))) {
    remove_argument (N, argc, argv);
    SINGLE_CPT = strcreate (argv[N]);
    remove_argument (N, argc, argv);
  }

  /* specify portion of the sky : allow default of all 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);
  } 

  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) dvosecfilt_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) dvosecfilt_client_usage();

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

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