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

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

#include "ppSub.h"

int main(int argc, char *argv[])
{
    psExit exitValue = PS_EXIT_SUCCESS; // Exit value
    psTimerStart("ppSub");
    psLibInit(NULL);

    pmConfig *config = pmConfigRead(&argc, argv, PPSUB_RECIPE); // Configuration
    if (!config) {
        psErrorStackPrint(stderr, "Error reading configuration.");
        exitValue = PS_EXIT_CONFIG_ERROR;
        goto die;
    }

    if (!pmModelClassInit()) {
        psErrorStackPrint(stderr, "Error initialising model classes.\n");
        exitValue = PS_EXIT_PROG_ERROR;
        goto die;
    }

    if (!ppSubArgumentsSetup(argc, argv, config)) {
        psErrorStackPrint(stderr, "Error reading arguments.\n");
        exitValue = PS_EXIT_CONFIG_ERROR;
        goto die;
    }

    if (!ppSubCamera(config)) {
        psErrorStackPrint(stderr, "Error setting up camera.\n");
        exitValue = PS_EXIT_CONFIG_ERROR;
        goto die;
    }

    if (!ppSubArgumentsParse(config)) {
        psErrorStackPrint(stderr, "Error reading arguments.\n");
        exitValue = PS_EXIT_CONFIG_ERROR;
        goto die;
    }

    if (!ppSubLoop(config)) {
        psErrorStackPrint(stderr, "Error performing subtraction.\n");
        exitValue = PS_EXIT_PROG_ERROR;
        goto die;
    }

 die:
    psTrace("ppSub", 1, "Finished at %f sec\n", psTimerMark("ppSub"));
    psTimerStop();

    psFree(config);
    pmModelClassCleanup();
    pmConfigDone();
    psLibFinalize();

    exit(exitValue);
}
