Index: /trunk/ppbgrestore/configure.ac
===================================================================
--- /trunk/ppbgrestore/configure.ac	(revision 25972)
+++ /trunk/ppbgrestore/configure.ac	(revision 25973)
@@ -2,5 +2,5 @@
 AC_PREREQ(2.61)
 
-AC_INIT([ppbgrestore], [0.1.1], [ipp-support@ifa.hawaii.edu])
+AC_INIT([ppbgrestore], [1.0.0], [ipp-support@ifa.hawaii.edu])
 AC_CONFIG_SRCDIR([src])
 
@@ -33,4 +33,7 @@
 IPP_STDOPTS
 CFLAGS="${CFLAGS} -Wall -Werror"
+
+IPP_VERSION
+
 # echo "PPSTAMP_CFLAGS: $PPSTAMP_CFLAGS"
 # echo "PPSTAMP_LIBS: $PPSTAMP_LIBS"
Index: /trunk/ppbgrestore/src/Makefile.am
===================================================================
--- /trunk/ppbgrestore/src/Makefile.am	(revision 25972)
+++ /trunk/ppbgrestore/src/Makefile.am	(revision 25973)
@@ -4,4 +4,28 @@
 noinst_HEADERS = \
 	ppbgrestore.h
+
+if HAVE_SVNVERSION
+PPBGRESTORE_VERSION=`$(SVNVERSION) ..`
+else
+PPBGRESTORE_VERSION="UNKNOWN"
+endif
+
+if HAVE_SVN
+PPBGRESTORE_BRANCH=`$(SVN) info .. | $(SED) -n -e '/URL:/ h' -e '/Repository Root:/ { x; H; x; s|Repository Root: \(.*\)\nURL: \1\(.*\)|\2| ; s|^/|| ; s|/[a-zA-Z]*/src.*|| ; p }'`
+PPBGRESTORE_SOURCE=`$(SVN) info | $(SED) -n -e 's/Repository UUID: // p'`
+else
+PPBGRESTORE_BRANCH="UNKNOWN"
+PPBGRESTORE_SOURCE="UNKNOWN"
+endif
+
+# Force recompilation of ppbgrestoreVersion.c, since it gets the version information
+ppbgrestoreVersion.c: ppbgrestoreVersionDefinitions.h
+ppbgrestoreVersionDefinitions.h: ppbgrestoreVersionDefinitions.h.in FORCE
+	-$(RM) ppbgrestoreVersionDefinitions.h
+	$(SED) -e "s|@PPBGRESTORE_VERSION@|\"$(PPBGRESTORE_VERSION)\"|" -e "s|@PPBGRESTORE_BRANCH@|\"$(PPBGRESTORE_BRANCH)\"|" -e "s|@PPBGRESTORE_SOURCE@|\"$(PPBGRESTORE_SOURCE)\"|" ppbgrestoreVersionDefinitions.h.in > ppbgrestoreVersionDefinitions.h
+FORCE: ;
+
+
+BUILT_SOURCES = ppbgrestoreVersionDefinitions.h
 
 ppbgrestore_CPPFLAGS = $(PSPHOT_CFLAGS)  $(PSMODULE_CFLAGS) $(PSLIB_CFLAGS)
Index: /trunk/ppbgrestore/src/ppbgrestore.h
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestore.h	(revision 25972)
+++ /trunk/ppbgrestore/src/ppbgrestore.h	(revision 25973)
@@ -15,4 +15,5 @@
 #include "psastro.h"
 #include "ppStats.h"
+#include "ppbgrestoreVersionDefinitions.h"
 
 // Options for ppbgrestore processing
Index: /trunk/ppbgrestore/src/ppbgrestoreArguments.c
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestoreArguments.c	(revision 25972)
+++ /trunk/ppbgrestore/src/ppbgrestoreArguments.c	(revision 25973)
@@ -8,4 +8,13 @@
 pmConfig *ppbgrestoreArguments(int argc, char *argv[])
 {
+    if (argc == 1) {
+        usage();
+    }
+
+    if (psArgumentGet(argc, argv, "-version")) {
+        ppbgrestoreVersionPrint();
+        exit(0);
+    }
+
     pmConfig *config = pmConfigRead(&argc, argv, NULL);
     if (config == NULL) {
Index: /trunk/ppbgrestore/src/ppbgrestoreCleanup.c
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestoreCleanup.c	(revision 25972)
+++ /trunk/ppbgrestore/src/ppbgrestoreCleanup.c	(revision 25973)
@@ -3,4 +3,24 @@
 void ppbgrestoreCleanup(pmConfig *config, ppbgrestoreOptions *options)
 {
-    // XXX: todo
+    // Free memory used by ppbgrestore
+    psFree(options);
+    psFree(config);
+
+    // Free memory used by psModules
+    pmSourceFitSetDone ();
+    pmConceptsDone();
+    pmConfigDone();
+    pmModelClassCleanup();
+
+    // Free memory used by psLib
+    psLibFinalize();
+
+    // psMemBlock **memblocks;
+    // int Nleaks = psMemCheckLeaks (0, &memblocks, stderr, false);
+    // fprintf (stderr, "Found %d leaks at %s\n", Nleaks, "ppImage");
+
+    // fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "ppImage");
+
+    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "ppbgrestore");
+
 }
Index: /trunk/ppbgrestore/src/ppbgrestoreLoop.c
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestoreLoop.c	(revision 25972)
+++ /trunk/ppbgrestore/src/ppbgrestoreLoop.c	(revision 25973)
@@ -91,8 +91,4 @@
         output->save = true;
 
-#ifdef notdef
-        psImageBinning *binning = psMetadataLookupPtr(&status, modelRO->analysis,
-                                                  "PSPHOT.BACKGROUND.BINNING"); // Binning for model
-#endif
         psImageBinning *binning = psphotBackgroundBinning(image, config);
         if (!binning) {
@@ -105,6 +101,8 @@
         if (!psImageUnbin (background->image, modelRO->image, binning)) {
             psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
+            psFree(binning);
             return false;
         }
+        psFree(binning);
         psF32 **backData = background->image->data.F32;
 
Index: /trunk/ppbgrestore/src/ppbgrestoreVersion.c
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestoreVersion.c	(revision 25972)
+++ /trunk/ppbgrestore/src/ppbgrestoreVersion.c	(revision 25973)
@@ -1,7 +1,104 @@
 #include "ppbgrestore.h"
 
-void ppbgrestoreVersionPrint()
+#ifndef PPBGRESTORE_VERSION
+#error "PPBGRESTORE_VERSION is not set"
+#endif
+#ifndef PPBGRESTORE_BRANCH
+#error "PPBGRESTORE_BRANCH is not set"
+#endif
+#ifndef PPBGRESTORE_SOURCE
+#error "PPBGRESTORE_SOURCE is not set"
+#endif
+
+psString ppbgrestoreVersion(void)
 {
-    // XXX: TODO
-    printf("version 0.0\n");
+    char *value = NULL;
+    psStringAppend(&value, "%s@%s", PPBGRESTORE_BRANCH, PPBGRESTORE_VERSION);
+    return value;
 }
+
+psString ppbgrestoreSource(void)
+{
+    return psStringCopy(PPBGRESTORE_SOURCE);
+}
+
+psString ppbgrestoreVersionLong(void)
+{
+    psString version = ppbgrestoreVersion();  // Version, to return
+    psString source = ppbgrestoreSource(); // Source
+
+    psStringPrepend(&version, "ppbgrestore ");
+    psStringAppend(&version, " from %s, built %s, %s", source, __DATE__, __TIME__);
+    psFree(source);
+
+#ifdef __OPTIMIZE__
+    psStringAppend(&version, " optimised");
+#else
+    psStringAppend(&version, " unoptimised");
+#endif
+
+    return version;
+};
+
+bool ppbgrestoreVersionHeader(psMetadata *header)
+{
+#ifdef notyet
+    PS_ASSERT_METADATA_NON_NULL(header, false);
+
+    psTime *time = psTimeGetNow(PS_TIME_TAI); // The time now
+    psString timeString = psTimeToISO(time); // The time in an ISO string
+    psFree(time);
+    psString history = NULL;               // History string
+    psStringAppend(&history, "ppbgrestore at %s", timeString);
+    psFree(timeString);
+    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, history);
+    psFree(history);
+
+    psLibVersionHeader(header);
+    psModulesVersionHeader(header);
+    psphotVersionHeader(header);
+    psastroVersionHeader(header);
+    ppStatsVersionHeader(header);
+
+    psString version = ppbgrestoreVersion(); // ppbgrestore software version
+    psString source  = ppbgrestoreSource();  // ppbgrestore software source
+
+    psStringPrepend(&version, "ppbgrestore version: ");
+    psStringPrepend(&source, "ppbgrestore source: ");
+
+    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, version);
+    psMetadataAddStr(header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, source);
+
+    psFree(version);
+    psFree(source);
+#endif // notyet
+
+    return true;
+}
+
+
+void ppbgrestoreVersionPrint(void)
+{
+    psTime *time = psTimeGetNow(PS_TIME_TAI); // The time now
+    psString timeString = psTimeToISO(time); // The time in an ISO string
+    psFree(time);
+    psLogMsg("ppbgrestore", PS_LOG_INFO, "ppbgrestore at %s", timeString);
+    psFree(timeString);
+
+    psString pslib = psLibVersionLong();// psLib version
+    psString psmodules = psModulesVersionLong(); // psModules version
+    psString psphot = psphotVersionLong(); // psphot version
+    psString ppbgrestore = ppbgrestoreVersionLong(); // ppbgrestore version
+
+    psLogMsg("ppbgrestore", PS_LOG_INFO, "%s", pslib);
+    psLogMsg("ppbgrestore", PS_LOG_INFO, "%s", psmodules);
+    psLogMsg("ppbgrestore", PS_LOG_INFO, "%s", psphot);
+    psLogMsg("ppbgrestore", PS_LOG_INFO, "%s", ppbgrestore);
+
+    psFree(pslib);
+    psFree(psmodules);
+    psFree(psphot);
+    psFree(ppbgrestore);
+
+    return;
+}
Index: /trunk/ppbgrestore/src/ppbgrestoreVersionDefinitions.h.in
===================================================================
--- /trunk/ppbgrestore/src/ppbgrestoreVersionDefinitions.h.in	(revision 25973)
+++ /trunk/ppbgrestore/src/ppbgrestoreVersionDefinitions.h.in	(revision 25973)
@@ -0,0 +1,9 @@
+#ifndef PPBGRESTORE_VERSION_DEFINITIONS_H
+#define PPBGRESTORE_VERSION_DEFINITIONS_H
+
+#define PPBGRESTORE_VERSION @PPBGRESTORE_VERSION@ // SVN version
+#define PPBGRESTORE_BRANCH  @PPBGRESTORE_BRANCH@  // SVN branch
+#define PPBGRESTORE_SOURCE  @PPBGRESTORE_SOURCE@  // SVN source
+
+#endif
+
