#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ppbgrestore.h"

#define TIMER_TOTAL "PPBGRESTORE_TOTAL"

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

    psLibInit(NULL);

    psTimerStart(TIMER_TOTAL);

    // Parse the configuration and arguments
    // Open the input image(s)
    // Determine camera, format from header if not already defined
    // Construct camera in preparation for reading
    pmConfig *config = ppbgrestoreArguments(argc, argv);
    if (config == NULL) {
        psErrorStackPrint(stderr, "Unable to parse command-line arguments.");
        ppbgrestoreCleanup(config, NULL);
        exit(PS_EXIT_CONFIG_ERROR);
    }

    ppbgrestoreVersionPrint();

    // define recipe options
    // define the active I/O files
    ppbgrestoreOptions *options = ppbgrestoreParseCamera(config);
    if (options == NULL) {
        psErrorStackPrint(stderr, "Unable to parse camera.");
        ppbgrestoreCleanup(config, options);
        exit(PS_EXIT_CONFIG_ERROR);
    }

    // Image Arithmetic Loop
    if (!ppbgrestoreLoop(config, options)) {
        psErrorStackPrint(stderr, "Unable to loop over input");
        ppbgrestoreCleanup(config, options);
        exit(PS_EXIT_SYS_ERROR);
    }

    psLogMsg("ppbgrestore", PS_LOG_INFO, "Complete ppbgrestore run: %f sec\n", psTimerMark(TIMER_TOTAL));

    // Cleaning up
    ppbgrestoreCleanup(config, options);

    return PS_EXIT_SUCCESS;
}
