# include "getstar.h"

void help () {
  fprintf (stderr, "USAGE: \n"
	   "getstar -region ra dec ra dec\n"
	   "getstar -radius ra dec radius\n"
	   "getstar -catalog n0000/0000.cpt\n"
	   "getstar -image name\n"
	   "getstar -immatch partial-name\n\n"
	   " option options: \n"
	   " -o output  : defaults to stdout\n"
	   " -v         : verbose mode\n"
	   " -h / -help : this list\n"
    );
  exit (2);
}

int args (int argc, char **argv) {
  
  int N;

  /* check for help request */
  if (get_argument (argc, argv, "-help") ||
      get_argument (argc, argv, "-h")) {
    help ();
  }

  /* configuration info */
  ConfigInit (&argc, argv);

  /* check for command line options */
  VERBOSE = FALSE;
  if ((N = get_argument (argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, &argc, argv);
  }

  MagLimitUse = FALSE;
  if ((N = get_argument (argc, argv, "-maglim"))) {
    MagLimitUse = TRUE;
    remove_argument (N, &argc, argv);
    MagLimitValue = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* check for command line options */
  strcpy (OUTPUT, "stdout");
  if ((N = get_argument (argc, argv, "-o"))) {
    remove_argument (N, &argc, argv);
    strcpy (OUTPUT, argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* parse optional entries above. one of the options below is required */
  MODE = BY_NOTHING;
  if ((N = get_argument (argc, argv, "-region"))) {
    double R, D;
    MODE = BY_REGION;
    remove_argument (N, &argc, argv);
    if (argc != 5) help();
    ohana_str_to_radec (&R, &D, argv[N+0], argv[N+1]);
    REGION.Rmin = R;
    REGION.Dmin = D;
    remove_argument (N, &argc, argv);
    remove_argument (N, &argc, argv);

    ohana_str_to_radec (&R, &D, argv[N+0], argv[N+1]);
    REGION.Rmax = R;
    REGION.Dmax = D;
    remove_argument (N, &argc, argv);
    remove_argument (N, &argc, argv);

    // XXX we will have issues at 0,360 boundary...
    // see code in dvo/pmeasure for fixes
    while (REGION.Rmin > 360) REGION.Rmin -= 360.0;
    while (REGION.Rmin <   0) REGION.Rmin += 360.0;
    while (REGION.Rmax > 360) REGION.Rmax -= 360.0;
    while (REGION.Rmax <   0) REGION.Rmax += 360.0;
    if (REGION.Dmax < REGION.Dmin) {
	SWAP (REGION.Dmax, REGION.Dmin);
    }
  }
  if ((N = get_argument (argc, argv, "-radius"))) {
    double R, D, radius;
    fprintf (stderr, "-radius is not recommended\n");
    MODE = BY_RADIUS;
    remove_argument (N, &argc, argv);
    if (argc != 4) help();
    ohana_str_to_radec (&R, &D, argv[N+0], argv[N+1]);
    radius = atof(argv[N+2]);
    REGION.Rmin = R - radius / cos(D*RAD_DEG);
    REGION.Dmin = D - radius;
    REGION.Rmax = R + radius / cos(D*RAD_DEG);
    REGION.Dmax = D + radius;
    remove_argument (N, &argc, argv);
    remove_argument (N, &argc, argv);
    remove_argument (N, &argc, argv);
  }
  if ((N = get_argument (argc, argv, "-catalog"))) {
    fprintf (stderr, "-catalog is not implemented\n");
    exit (2);
    MODE = BY_CATALOG;
    remove_argument (N, &argc, argv);
    if (argc != 2) help();
  }
  if ((N = get_argument (argc, argv, "-image"))) {
    MODE = BY_IMAGE;
    remove_argument (N, &argc, argv);
    IMAGENAME = strcreate (argv[N]);
    if (argc != 2) help();
  }
  if ((N = get_argument (argc, argv, "-immatch"))) {
    MODE = BY_IMMATCH;
    remove_argument (N, &argc, argv);
    IMAGENAME = strcreate (argv[N]);
    if (argc != 2) help();
  }
  if (MODE == BY_NOTHING) help ();

  return (TRUE);
}


/* USAGE

getstar -region ra dec ra dec
getstar -radius ra dec radius
getstar -catalog n0000/0000.cpt
getstar -image name [-smp | -smf]
getstar -immatch partial-name [-smp | -smf]
   
* return measurements 
* return average / secfilt table
* return a single image (smf/smp format)

*/
