# 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_KEEP_LIST = NULL;
  if ((N = get_argument (argc, argv, "+photcode"))) {
    remove_argument (N, &argc, argv);
    PHOTCODE_KEEP_LIST = strcreate(argv[N]);
    remove_argument (N, &argc, argv);
  }

  PHOTCODE_SKIP_LIST = NULL;
  if ((N = get_argument (argc, argv, "-photcode"))) {
    remove_argument (N, &argc, argv);
    PHOTCODE_SKIP_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);
  }

  CHIPORDER = 0;
  if ((N = get_argument (argc, argv, "-chiporder"))) {
    remove_argument (N, &argc, argv);
    CHIPORDER = atoi(argv[N]);
    remove_argument (N, &argc, argv);
  }

  SAVEPLOT = FALSE;
  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);
  }

  // by default, require > 10pts to clip
  strcpy (STATMODE, "CHI_INNER_80_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;
  }
  
  // for now, make the default to ignore the photflags
  // XXX make it true by default instead?
  PhotFlagSelect = FALSE;
  if ((N = get_argument (argc, argv, "+photflags"))) {
    remove_argument (N, &argc, argv);
    PhotFlagSelect = TRUE;
  }

  PhotFlagBad = 0;
  if ((N = get_argument (argc, argv, "-photflagbad"))) {
    remove_argument (N, &argc, argv);
    PhotFlagBad = atoi (argv[N]);
    remove_argument (N, &argc, argv);
  }

  PhotFlagPoor = 0;
  if ((N = get_argument (argc, argv, "-photflagpoor"))) {
    remove_argument (N, &argc, argv);
    PhotFlagPoor = atoi (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* 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, "  -update-objects\n");
  fprintf (stderr, "    -pm\n");
  fprintf (stderr, "    -par\n");
  fprintf (stderr, "    -pmpar\n");
  fprintf (stderr, "  -update-simple\n");
  fprintf (stderr, "  -update-chips\n");
  fprintf (stderr, "  -update-mosaics\n");
  fprintf (stderr, "  -time (start)(stop)\n");
  fprintf (stderr, "  +photcode (code)[,code,code...]\n");
  fprintf (stderr, "  -photcode (code)[,code,code...]\n");
  fprintf (stderr, "  -plot\n");
  fprintf (stderr, "  -plotdelay (seconds)\n");
  fprintf (stderr, "  -statmode (mode)\n");
  fprintf (stderr, "  -reset");
  fprintf (stderr, "  -update : apply new fit to database\n");
  fprintf (stderr, "  -params\n");
  fprintf (stderr, "  -plrange\n");
  fprintf (stderr, "  -minerror\n");
  fprintf (stderr, "  -area\n");
  fprintf (stderr, "  -area Xmin Xmax Ymin Ymax\n");
  fprintf (stderr, "  -instmag min max\n\n");
  fprintf (stderr, "  +photflags\n");
  fprintf (stderr, "  -photflagbad\n");
  fprintf (stderr, "  -photflagpoor\n");
  fprintf (stderr, "  -v\n");
  fprintf (stderr, "  \n");
  exit (2);
} 

