# include "photdbc.h"

int success = TRUE;

void WarnConfig (char *config, char *key, char * mode, int N, void *var) {
  if (!ScanConfig (config, key, mode, N, var)) {
    fprintf (stderr, "missing config variable %s\n", key);
    success = FALSE;
  }
}

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

  double ZERO_POINT, tmp;
  char *config, *file;

  /*** 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);

  WarnConfig (config, "JOIN_RADIUS",            "%lf", 0, &JOIN_RADIUS);
  WarnConfig (config, "UNIQ_RADIUS",            "%lf", 0, &UNIQ_RADIUS);

  WarnConfig (config, "XMIN",                   "%lf", 0, &XMIN);
  WarnConfig (config, "XMAX",                   "%lf", 0, &XMAX);
  WarnConfig (config, "YMIN",                   "%lf", 0, &YMIN);
  WarnConfig (config, "YMAX",                   "%lf", 0, &YMAX);

  WarnConfig (config, "RMIN",                   "%lf", 0, &RMIN);
  WarnConfig (config, "RMAX",                   "%lf", 0, &RMAX);
  WarnConfig (config, "DMIN",                   "%lf", 0, &DMIN);
  WarnConfig (config, "DMAX",                   "%lf", 0, &DMAX);

  WarnConfig (config, "DMCAL_MIN",              "%lf", 0, &tmp);  DMCAL_MIN = 1000*tmp;
  WarnConfig (config, "MMIN",                   "%lf", 0, &tmp);  MMIN      = 1000*tmp;
  WarnConfig (config, "MMAX",                   "%lf", 0, &tmp);  MMAX      = 1000*tmp;
  WarnConfig (config, "DMSYS",                  "%lf", 0, &tmp);  DMSYS     = SQ(1000*tmp);
  WarnConfig (config, "DMGAIN",                 "%lf", 0, &DMGAIN); 
  WarnConfig (config, "SIGMA_MAX",              "%lf", 0, &SIGMA_MAX); SIGMA_MAX *= 1000;

  WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
  WarnConfig (config, "NMEAS_MIN",              "%d",  0, &NMEAS_MIN);

  WarnConfig (config, "IMAGE_CATALOG",          "%s",  0, ImageCat);
  WarnConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
  WarnConfig (config, "CATDIR",                 "%s",  0, CATDIR);
  WarnConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
  WarnConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);

  if (!success) { 
    fprintf (stderr, "missing config parameter\n");
    exit (1);
  }

  SetZeroPoint (ZERO_POINT);

  if (!LoadPhotcodes (PhotCodeFile)) {
    fprintf (stderr, "error loading photcodes\n");
    exit (1);
  }

  free (config);
  free (file);

}
