# include "relphot.h"

void ConfigInit (int *argc, char **argv) {

  double ZERO_POINT;
  char  *config, *file;
  char CatdirPhotcodeFile[256];
  char MasterPhotcodeFile[256];

  /*** load configuration info ***/
  file = SelectConfigFile (argc, argv, "ptolemy");
  config = LoadConfigFile (file);
  if (config == (char *) NULL) {
    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
    if (file != (char *) NULL) free (file);
    exit (0);
  }
  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);

  GetConfig (config, "MAG_LIM",                "%lf", 0, &MAG_LIM);
  GetConfig (config, "SIGMA_LIM",              "%lf", 0, &SIGMA_LIM);
  GetConfig (config, "STAR_SCATTER",           "%lf", 0, &STAR_SCATTER);
  GetConfig (config, "IMAGE_SCATTER",          "%lf", 0, &IMAGE_SCATTER);
  GetConfig (config, "IMAGE_OFFSET",           "%lf", 0, &IMAGE_OFFSET);

  GetConfig (config, "STAR_CHISQ",             "%lf", 0, &STAR_CHISQ);
  GetConfig (config, "GRID_TOOFEW",            "%d",  0, &GRID_TOOFEW);
  GetConfig (config, "STAR_TOOFEW",            "%d",  0, &STAR_TOOFEW);
  GetConfig (config, "IMAGE_TOOFEW",           "%d",  0, &IMAGE_TOOFEW);
  GetConfig (config, "IMAGE_GOOD_FRACTION",    "%lf", 0, &IMAGE_GOOD_FRACTION);

  GetConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
  GetConfig (config, "CATDIR",                 "%s",  0, CATDIR);
  ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
  ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);

  sprintf (ImageCat, "%s/Images.dat", CATDIR);

  if (!ScanConfig (config, "SKY_DEPTH",         "%d",  0, &SKY_DEPTH)) {
    SKY_DEPTH = 2;
  }
  if (!ScanConfig (config, "SKY_TABLE",         "%s",  0, SKY_TABLE)) {
    SKY_TABLE[0] = 0;
  }

  GetConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);

  GetConfig (config, "RELPHOT_GRID_X",         "%d",  0, &RELPHOT_GRID_X);
  GetConfig (config, "RELPHOT_GRID_Y",         "%d",  0, &RELPHOT_GRID_Y);
  GetConfig (config, "RELPHOT_GRID_BINNING",   "%d",  0, &RELPHOT_GRID_BINNING);
  GetConfig (config, "CAMERA_CONFIG",          "%s",  0, CameraConfig);
  GetConfig (config, "MOSAICNAME",             "%s",  0, MOSAICNAME);

  if (*CATMODE == 0) strcpy (CATMODE, "RAW");
  if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");

  /* XXX this does not yet write out the master photcode table */
  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile, TRUE)) {
    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
    exit (1);
  }
  SetZeroPoint (ZERO_POINT);

  free (config);
  free (file);

}

void GetConfig (char *config, char *field, char *format, int N, void *ptr) {

  char *status;

  status = ScanConfig (config, field, format, N, ptr);
  if (status == NULL) {
    fprintf (stderr, "error in config, cannot find %s\n", field);
    exit (1);
  }
  return;
}
