# include "dvo.h"

int lcat (int argc, char **argv) {
  
  double Radius;
  int i, N, Nregions, ShowAll;
  RegionFile *regions;
  char filename[128], exists;
  struct stat filestat;
  char catdir[256];
  Graphdata graphmode;
  int Ngraph;

  Ngraph = 0;
  if (!GetGraphData (&graphmode, NULL, &Ngraph)) return (FALSE);

  VarConfig ("CATDIR", "%s", catdir);

  ShowAll = FALSE;
  if (N = get_argument (argc, argv, "-all")) {
    remove_argument (N, &argc, argv);
    ShowAll = TRUE;
  }
  if (argc != 1) {
    fprintf (stderr, "USAGE: lcat [-all]\n");
    return (FALSE);
  }

  Radius = MAX (fabs(graphmode.xmax), fabs(graphmode.ymax));
  regions = find_regions (graphmode.coords.crval1, graphmode.coords.crval2, Radius, &Nregions);

  for (i = 0; i < Nregions; i++) {
    sprintf (filename, "%s/%s", catdir, regions[i].name);
    exists = 'Y';
    if (stat (filename, &filestat) == -1) exists = 'N';
    if (ShowAll) {
      fprintf (stderr, "%3d %s  %c\n", i, regions[i].name, exists);
    } else {
      if (exists == 'Y') {
	fprintf (stderr, "%3d %s\n", i, regions[i].name);
      }
    }
  }

  return (TRUE);

  free (regions);

}

