# include "dvoshell.h"

/* XXX note : for RegionName or RegionList, we need to free the skylist
   elements, but not in the case of radius selection - this implies 
   information carried back up */

static char *CATDIR  = NULL;
static SkyTable *sky = NULL;

int SetCATDIR (char *path, int verbose) {

  char *newpath;
  char catdir_config[256];
  char gscfile[256];
  char skyfile[256];
  int  skydepth;

  /* find CATDIR in config system */
  if (path == NULL) {
    if (VarConfig ("CATDIR", "%s", catdir_config) == NULL) return (FALSE);
    newpath = catdir_config;
  } else {
    newpath = path;
  }

  if (CATDIR != NULL) free (CATDIR);
  CATDIR = strcreate (newpath);

  if (VarConfig ("GSCFILE",  "%s", gscfile) == NULL) gscfile[0] = 0;
  if (VarConfig ("SKYFILE",  "%s", skyfile) == NULL) skyfile[0] = 0;
  if (VarConfig ("SKYDEPTH", "%d", &skydepth) == NULL) skydepth = 2;

  if (verbose) {
      gprint (GP_ERR, "CATDIR %s\n", CATDIR);
      gprint (GP_ERR, "GSCFILE %s\n", gscfile);
      gprint (GP_ERR, "SKYFILE %s\n", skyfile);
      gprint (GP_ERR, "SKYDEPTH %d\n", skydepth);
  }

  /* load the SkyTable at this point */
  /* set the image path as well */

  if (sky != NULL) SkyTableFree (sky);
  sky = SkyTableLoadOptimal (CATDIR, skyfile, gscfile, skydepth, verbose);
  if (sky == NULL) return FALSE;

  SkyTableSetFilenames (sky, CATDIR, "cpt");

  return (TRUE);
}

char *GetCATDIR () {
  if (CATDIR == NULL) {
    SetCATDIR (NULL, FALSE);
  }
  return (CATDIR);
}

SkyTable *GetSkyTable () {
  return (sky);
}

int SetRegionSelection (int *argc, char **argv, char **RegionName, char **RegionList) {
  
  int N;

  /* check for Region selection */
  *RegionName = NULL;
  if ((N = get_argument (*argc, argv, "-cpt"))) {
    remove_argument (N, argc, argv);
    *RegionName = strcreate (argv[N]);
    remove_argument (N, argc, argv);
    return (TRUE);
  }    
  /* check for Region list */
  *RegionList = NULL;
  if ((N = get_argument (*argc, argv, "-cptlist"))) {
    remove_argument (N, argc, argv);
    *RegionList = strcreate (argv[N]);
    remove_argument (N, argc, argv);
    return (TRUE);
  } 
  if ((*RegionName == NULL) && (*RegionList == NULL)) return (TRUE);

  gprint (GP_ERR, "-cpt and -cptlist are incompatible\n");
  free (*RegionName);
  free (*RegionList);
  *RegionName = NULL;
  *RegionList = NULL;
  return (FALSE);
}

/* given possible options (by name, by list, by graph region), select SkyRegions */
SkyList *SelectRegions (char *RegionName, char *RegionList) {

  double Radius;
  int Ngraph;
  Graphdata graphsky;
  SkyList *skylist;
  char filename[256];

  /* determine region-file names */
  if (RegionName != NULL) {
    ALLOCATE (skylist, SkyList, 1);
    ALLOCATE (skylist[0].regions, SkyRegion *, 1);
    ALLOCATE (skylist[0].regions[0], SkyRegion, 1);
    ALLOCATE (skylist[0].filename, char *, 1);
    
    strcpy (skylist[0].regions[0][0].name, RegionName);
    sprintf (filename, "%s/%s.cpt", CATDIR, RegionName);
    skylist[0].filename[0] = strcreate (filename);
    free (RegionName);
    RegionName = NULL;
    return (skylist);
  } 
  if (RegionList != NULL) {
    skylist = SkyListLoadFile (RegionList);
    free (RegionList);
    RegionList = NULL;
    return (skylist);
  }

  Ngraph = 0;
  if (!GetGraphData (&graphsky, NULL, &Ngraph)) {
    gprint (GP_ERR, "region display not available\n");
    return (NULL);
  }

  Radius = MAX (fabs(graphsky.xmax), fabs(graphsky.ymax));
  skylist = SkyListByRadius (sky, -1, graphsky.coords.crval1, graphsky.coords.crval2, Radius);

  return (skylist);
}

/* returns a list of region files names from file */
SkyList *SkyListLoadFile (char *filename) {
  
  FILE *f;
  int NREGIONS, Nregions;
  SkyList *skylist;

  ALLOCATE (skylist, SkyList, 1);

  f = fopen (filename, "r");
  if (f == NULL) {
    gprint (GP_ERR, "ERROR: can't find region list file %s\n", filename);
    skylist[0].Nregions = 0;
    skylist[0].regions = NULL;
    return (skylist);
  }
  
  Nregions = 0;
  NREGIONS = 50;
  ALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS);
  ALLOCATE (skylist[0].filename, char *, NREGIONS);

  while (fscanf (f, "%s", filename) != EOF) {
    ALLOCATE (skylist[0].regions[Nregions], SkyRegion, 1);
    strcpy (skylist[0].regions[Nregions][0].name, filename);
    sprintf (filename, "%s/%s.cpt", CATDIR, skylist[0].regions[Nregions][0].name);
    skylist[0].filename[Nregions] = strcreate (filename);
    Nregions ++;
    CHECK_REALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS, Nregions, 50);
  }
  skylist[0].Nregions = Nregions;
  return (skylist);
}

