# include "relastro.h"
void usage (void);

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

  int N;
  double trange;

  /* possible operations */
  FIT_TARGET = TARGET_NONE;
  FIT_MODE = FIT_AVERAGE;
  if ((N = get_argument (argc, argv, "-update-objects"))) {
    remove_argument (N, &argc, argv);
    FIT_TARGET = TARGET_OBJECTS;

    // check for object fitting modes (not valid for images)
    if ((N = get_argument (argc, argv, "-pm"))) {
	remove_argument (N, &argc, argv);
	FIT_MODE = FIT_PM_ONLY;
    }
    if ((N = get_argument (argc, argv, "-par"))) {
	remove_argument (N, &argc, argv);
	FIT_MODE = FIT_PAR_ONLY;
    }
    if ((N = get_argument (argc, argv, "-pmpar"))) {
	remove_argument (N, &argc, argv);
	FIT_MODE = FIT_PM_AND_PAR;
    }
  }
  if ((N = get_argument (argc, argv, "-update-simple"))) {
    remove_argument (N, &argc, argv);
    FIT_TARGET = TARGET_SIMPLE;
  }
  if ((N = get_argument (argc, argv, "-update-chips"))) {
    remove_argument (N, &argc, argv);
    FIT_TARGET = TARGET_CHIPS;
  }
  if ((N = get_argument (argc, argv, "-update-mosaics"))) {
    remove_argument (N, &argc, argv);
    FIT_TARGET = TARGET_MOSAICS;
  }
  if (FIT_TARGET == TARGET_NONE) usage();

  /* 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);
  } else {
    usage ();
  }

  /* define time */
  TimeSelect = FALSE;
  if ((N = get_argument (argc, argv, "-time"))) {
    remove_argument (N, &argc, argv);
    if (!ohana_str_to_time (argv[N], &TSTART)) { 
      fprintf (stderr, "ERROR: syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!ohana_str_to_dtime (argv[N], &trange)) { 
      if (!ohana_str_to_time (argv[N], &TSTOP)) { 
	fprintf (stderr, "ERROR: syntax error\n");
	return (FALSE);
      }
    } else {
      if (trange < 0) {
	trange = fabs (trange);
	TSTOP = TSTART;
	TSTART -= trange;
      } else {
	TSTOP = TSTART + trange;
      }
    }
    remove_argument (N, &argc, argv);
    TimeSelect = TRUE;
  }

  PHOTCODE_LIST = NULL;
  if ((N = get_argument (argc, argv, "-photcode"))) {
    remove_argument (N, &argc, argv);
    PHOTCODE_LIST = strcreate(argv[N]);
    remove_argument (N, &argc, argv);
  }

  VERBOSE = FALSE;
  if ((N = get_argument (argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, &argc, argv);
  }

  PLOTSTUFF = FALSE;
  if ((N = get_argument (argc, argv, "-plot"))) {
    PLOTSTUFF = TRUE;
    remove_argument (N, &argc, argv);
  }

  PLOTDELAY = 500000;
  if ((N = get_argument (argc, argv, "-plotdelay"))) {
    remove_argument (N, &argc, argv);
    PLOTDELAY = 1e6*atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  strcpy (STATMODE, "CHI_INNER_WTMEAN");
  if ((N = get_argument (argc, argv, "-statmode"))) {
    remove_argument (N, &argc, argv);
    strcpy (STATMODE, argv[N]);
    remove_argument (N, &argc, argv);
  }

  RESET = FALSE;
  if ((N = get_argument (argc, argv, "-reset"))) {
    remove_argument (N, &argc, argv);
    RESET = TRUE;
  }

  UPDATE = FALSE;
  if ((N = get_argument (argc, argv, "-update"))) {
    remove_argument (N, &argc, argv);
    UPDATE = TRUE;
  }

  SHOW_PARAMS = FALSE;
  if ((N = get_argument (argc, argv, "-params"))) {
    remove_argument (N, &argc, argv);
    SHOW_PARAMS = TRUE;
  }

  /* XXX update these for relevant plots */
  PlotMmin = 10.0; PlotMmax = 20.0; PlotdMmin = -1.0; PlotdMmax = 1.0;
  if ((N = get_argument (argc, argv, "-plrange"))) {
    remove_argument (N, &argc, argv);
    PlotMmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotMmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotdMmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotdMmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* XXX update this */
  MIN_ERROR = 0.001;
  if ((N = get_argument (argc, argv, "-minerror"))) {
    remove_argument (N, &argc, argv);
    MIN_ERROR = atof (argv[N]);
    remove_argument (N, &argc, argv);
    /* require MIN_ERROR > 0 */
  }  

  AreaSelect = FALSE;
  if ((N = get_argument (argc, argv, "-area"))) {
    remove_argument (N, &argc, argv);
    AreaXmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaXmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaYmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaYmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaSelect = TRUE;
  }

  ImagSelect = FALSE;
  if ((N = get_argument (argc, argv, "-instmag"))) {
    remove_argument (N, &argc, argv);
    ImagMin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    ImagMax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    ImagSelect = TRUE;
  }
  
  /* XXX drop this? */
  DophotSelect = FALSE;
  if ((N = get_argument (argc, argv, "-dophot"))) {
    remove_argument (N, &argc, argv);
    DophotValue = atof (argv[N]);
    remove_argument (N, &argc, argv);
    DophotSelect = TRUE;
  }

  if (argc != 1) usage ();
  return TRUE;
}

void usage () {
  fprintf (stderr, "ERROR: USAGE: relastro -region RA RA DEC DEC\n");
  fprintf (stderr, "  working options: \n");
  fprintf (stderr, "  -time (start) (stop)\n");
  fprintf (stderr, "  -v\n");
  fprintf (stderr, "  -plot\n");
  fprintf (stderr, "  -plotdelay (seconds)\n");
  fprintf (stderr, "  -update\n");
  fprintf (stderr, "  -params\n");
  fprintf (stderr, "  -reset\n");
  fprintf (stderr, "  -area Xmin Xmax Ymin Ymax\n");
  fprintf (stderr, "  -instmag min max\n\n");
  fprintf (stderr, "  planned options: \n");
  fprintf (stderr, "  -photcode code[,code,code,..]\n");
  fprintf (stderr, "  \n");
  exit (2);
} 
