// pstampdump  - read a fits table describing a postage stamp request or response file and
//               print the contents to stdout
//
//              Actually should work for any kind of fits table, but I'm not sure what will
//              happen for binary data.

#include <pslib.h>
#include <psmodules.h>
#include <string.h>

static psArray *readRequestTable(psString fileName)
{
    psFits *fitsFile = psFitsOpen(fileName, "r");
    if (fitsFile == NULL) {
        psError(PS_ERR_IO, false, "failed to open %s for output", fileName);
        return NULL;
    }

    psArray *array = psFitsReadTable(fitsFile);

    psFitsClose(fitsFile);

    if (array == NULL) {
        psError(PS_ERR_IO, false, "psFitsReadTable failed for %s", fileName);
        return NULL;
    } 

    return array;
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "usage: %s filename\n", argv[0]);
        return 1;
    }

    psString fileName = argv[1];

    psArray *array = readRequestTable(fileName);
    if (array == NULL) {
        return 1;
    }

    for (int i=0; i<psArrayLength(array); i++) {
        psMetadataPrint(stderr, array->data[i], 0);
    }

    return 0;
}
