# include "relastro.h"

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

  char  *config, *file;
  char CatdirPhotcodeFile[256];
  char MasterPhotcodeFile[256];
  struct stat filestat;
  int status;

  /*** 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, "RELASTRO_SIGMA_LIM",         "%lf", 0, &SIGMA_LIM); // exclude measurements on this basis
  GetConfig (config, "RELASTRO_SRC_MEAS_TOOFEW",   "%d",  0, &SRC_MEAS_TOOFEW);

  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NITER",    "%d",  0, &IMFIT_CLIP_NITER))    IMFIT_CLIP_NITER  = 3;
  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NSIGMA",   "%lf", 0, &IMFIT_CLIP_NSIGMA))   IMFIT_CLIP_NSIGMA = 3.0;
  if (!ScanConfig (config, "RELASTRO_IMFIT_SYS_SIGMA_LIM", "%lf", 0, &IMFIT_SYS_SIGMA_LIM)) IMFIT_SYS_SIGMA_LIM = 0.01;

  GetConfig (config, "PM_DT_MIN",              "%lf", 0, &PM_DT_MIN);
  GetConfig (config, "PM_TOOFEW",              "%d",  0, &PM_TOOFEW);
  GetConfig (config, "POS_TOOFEW",             "%d",  0, &POS_TOOFEW);

  GetConfig (config, "PAR_FACTOR_MIN",         "%lf", 0, &PAR_FACTOR_MIN);

  GetConfig (config, "RELASTRO_MAP_NX",        "%d",  0, &NX_MAP);
  GetConfig (config, "RELASTRO_MAP_NY",        "%d",  0, &NY_MAP);
  GetConfig (config, "RELASTRO_DPOS_MAX",      "%lf", 0, &DPOS_MAX);
  GetConfig (config, "ADDSTAR_RADIUS",         "%lf", 0, &ADDSTAR_RADIUS);

  // force CATDIR to be absolute (so parallel mode will work)
  GetConfig (config, "CATDIR",                 "%s",  0, CATDIR);
  char *tmpcatdir = abspath (CATDIR, DVO_MAX_PATH);
  strcpy (CATDIR, tmpcatdir);
  free (tmpcatdir);

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

  // Defaults for WHERE_A are established in db_utils.c: intializeConstraints
  ScanConfig(config, "WHERE_A",                "%s",  0, WHERE_A);
  ScanConfig(config, "WHERE_B",                "%s",  0, WHERE_B);

  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;
  }

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

  // check for existence of CATDIR
  status = stat (CATDIR, &filestat);
  if (status == -1) {
    fprintf (stderr, "directory %s does not exist, giving up\n", CATDIR);
    exit (1);
  }

  /* update master photcode table if not defined */
  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 (25.0);

  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;
}
