# include "dvoshell.h"

int gcat (int argc, char **argv) {
  
  int i, N;
  struct stat filestat;
  double Ra, Dec, Radius;
  SkyTable *sky;
  SkyList *skylist;
  void *Signal;

  int ShowHost = FALSE;
  if ((N = get_argument (argc, argv, "-host"))) {
    remove_argument (N, &argc, argv);
    ShowHost = TRUE;
  }
  int ShowBackup = FALSE;
  if ((N = get_argument (argc, argv, "-backup"))) {
    remove_argument (N, &argc, argv);
    ShowBackup = TRUE;
  }
  int ShowFlags = FALSE;
  if ((N = get_argument (argc, argv, "-flags"))) {
    remove_argument (N, &argc, argv);
    ShowFlags = TRUE;
  }

  if ((argc != 3) && (argc != 4)) {
    gprint (GP_ERR, "USAGE: gcat RA DEC [Radius] [-host] [-backup] [-flags]\n");
    return (FALSE);
  }

  /* load sky from correct table */
  Ra = atof (argv[1]);
  Dec = atof (argv[2]);
  if (argc == 4) 
    Radius = atof(argv[3]);
  else 
    Radius = 0.0001;

  sky = GetSkyTable ();
  skylist = SkyListByRadius (sky, -1, Ra, Dec, Radius);

  // prepare to handle interrupt signals
  Signal = signal (SIGINT, handle_interrupt);
  interrupt = FALSE;

  for (i = 0; (i < skylist[0].Nregions) && !interrupt; i++) {
    gprint (GP_ERR, "%3d %s", i, skylist[0].regions[i][0].name);
    if (stat (skylist[0].filename[i], &filestat) != -1) {
      gprint (GP_ERR, " *");
    } else {
      gprint (GP_ERR, "  ");
    } 
    if (ShowHost) {
      gprint (GP_ERR, "  %3d", skylist[0].regions[i][0].hostID);
    } else {
      gprint (GP_ERR, "     ");
    }
    if (ShowBackup) {
      gprint (GP_ERR, "  %3d", skylist[0].regions[i][0].backupID);
    } else {
      gprint (GP_ERR, "     ");
    }
    if (ShowFlags) {
      gprint (GP_ERR, "  0x%04x", skylist[0].regions[i][0].hostFlags);
    } else {
      gprint (GP_ERR, "        ");
    }
    gprint (GP_ERR, "\n");
    set_str_variable ("CATNAME", skylist[0].filename[i]);
  }

  return (TRUE);
  SkyListFree (skylist);
}
