# include "psphotStandAlone.h"

void psphotCleanup (pmConfig *config) {

    // Dump configuration if requested
    bool status;
    psString dump_file = psMetadataLookupStr(&status, config->arguments, "DUMP_CONFIG");
    if (dump_file) {
        (void)pmConfigCamerasCull(config,NULL);
	(void)pmConfigRecipesCull(config,NULL);
        pmConfigDump(config, dump_file);
    }

    psFree (config);

    psThreadPoolFinalize ();
    psTimerStop ();
    psMemCheckCorruption (stderr, true);
    pmModelClassCleanup ();
    psTimeFinalize ();
    pmSourceFitSetDone ();
    pmConceptsDone ();
    pmConfigDone ();
    // fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "psphot");
    fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "psphot");
    return;
}

psExit psphotGetExitStatus (void) {

    psErrorCode err = psErrorCodeLast ();
    switch (err) {
      case PS_ERR_NONE:
        return PS_EXIT_SUCCESS;
      case PSPHOT_ERR_SYS:
        return PS_EXIT_SYS_ERROR;
      case PSPHOT_ERR_CONFIG:
        return PS_EXIT_CONFIG_ERROR;
      case PSPHOT_ERR_PROG:
        return PS_EXIT_PROG_ERROR;
      case PSPHOT_ERR_DATA:
        return PS_EXIT_DATA_ERROR;
      default:
        return PS_EXIT_UNKNOWN_ERROR;
    }
    return PS_EXIT_UNKNOWN_ERROR;
}
