             (.closeEntry zip-stream-out))))))
   nil)
 
+(defn- fix-snapshot-version
+  "This method converts SNAPSHOT version numbers into something Alfresco can support. It does this by
+  decrementing the last non-zero version number element by 1 and appending a 999 version component on
+  the end.  It also always returns a version number with at least version components."
+  [version]
+  (if (.endsWith ^String version "-SNAPSHOT")
+    (let [non-snapshot-version      (.substring ^String version 0 (- (.length ^String version) (.length "-SNAPSHOT")))
+          version-number-components (map #(Integer/parseInt %) (s/split non-snapshot-version #"\."))
+          version-number-components (drop-while zero? (reverse version-number-components))
+          _                         (if (empty? version-number-components) (throw (RuntimeException. (str "Invalid version number: " version))))
+          version-number-components (reverse (concat [999 (dec (first version-number-components))] (rest version-number-components)))
+          version-number-components (if (= (count version-number-components) 1)
+                                      (concat version-number-components [999 999])
+                                      (if (= (count version-number-components) 2)
+                                        (concat version-number-components [999])
+                                        version-number-components))]
+      (s/join "." version-number-components))
+    version))
+
+(defn- replace-parameters
+  [project value]
+  (s/replace
+    (s/replace
+      (s/replace
+        value
+        "${project.name}" (str (if (nil? (:group project)) "" (str (:group project) "."))
+                               (:name project)))
+      "${project.version}" (fix-snapshot-version (:version project)))
+    "${project.description}" (:description project)))
+
+(defn- read-module-properties
+  [project module-properties-file]
+  (let [raw-props (java.util.Properties.)
+        _         (with-open [input-stream (io/input-stream module-properties-file)]
+                    (.load raw-props input-stream))]
+    (map-function-on-map-vals raw-props #(replace-parameters project %))))
+
+(defn- write-module-properties!
+  [file module-properties]
+  (doto (java.util.Properties.)
+    (.putAll module-properties)
+    (.store (io/output-stream file) " !!!! DO NOT EDIT -- AUTO-GENERATED FILE -- DO NOT EDIT !!!!"))
+  nil)
+
+(defn- rewrite-module-context!
+  [module-context-file module-id]
+  (let [module-context-content (slurp module-context-file)
+        rewritten-content      (s/replace module-context-content "${project.moduleId}" module-id)]
+    (spit module-context-file rewritten-content))
+  nil)
+
 (defn package-amp!
   [project args]
   (let [project-home           (io/file (:root project))


(defn- mkdir-p
  [directory]
  (io/make-parents (io/file directory ".")))   ; make-parents won't create the last path element of the file

(defn- zip-directory!
  "Recursively compress all files in 'directory' into 'zip-file'."

        _                      (if (not (fexists module-properties-file))
                                 (throw (RuntimeException. (str "Invalid AMP project - " module-properties-file " is missing."))))
        module-properties      (read-module-properties project module-properties-file)
        _                      (println module-properties)
        module-id              (get module-properties "module.id")
        module-version         (get module-properties "module.version")

