# include "dvoverify.h"

int main (int argc, char **argv) {

  int Nbad;
  SkyTable *sky;
  SkyList *skylist;

  // check various options
  dvoverify_client_args (&argc, argv);
  CATDIR = argv[1];

  Nbad = 0;

  // load the sky table for the existing database
  sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, FALSE, SKY_DEPTH_HST, VERBOSE);
  myAssert(sky, "can't read SkyTable");
  SkyTableSetFilenames (sky, CATDIR, "cpt");
  skylist = SkyListByPatch (sky, -1, &UserPatch);
  
  if (CHECK_IMAGE_ID) {
    // XXX in client mode, we should be reading a reduced table generated by the calling
    // serial program
    char filename[DVO_MAX_PATH];
    snprintf (filename, DVO_MAX_PATH, "%s/ImageIDs.fits", CATDIR);
    if (!LoadImageIDsSmall (filename)) {
      fprintf (stderr, "ERROR: failure to load image IDs\n");
      exit (2);
    }
  }

  dvoverify_catalogs (skylist, &Nbad);

  // write result to file
  FILE *results = fopen (RESULTS, "w");
  fprintf (results, "Nbad: %d NNotSorted: %d\n", Nbad, NNotSorted);

  int i, Nfailures;
  char **failures = GetFailures (&Nfailures);
  fprintf (results, "---- files with errors ---- \n");
  for (i = 0; i < Nfailures; i++) {
    fprintf (results, "%s\n", failures[i]);
  }
  fclose (results);

  if (Nbad > 0) {
    fprintf (stderr, "ERROR: %d files are bad\n", Nbad);
    exit (1);
  }

  fprintf (stderr, "SUCCESS on %d: no files are bad\n", HOST_ID);
  if (NNotSorted) {
    fprintf (stderr, "NOTE: %d files are not sorted\n", NNotSorted);
  }
  exit (0);
}

