 
 (defn encode
   [production]
-  ;; encode the production
-  (encode-production production)
-  ;; create an iso
-  (create-iso production)
-  (if (fit-on-one-cd? production)
-    ;; set the state to encoded
-    (prod/update! (assoc production :state :encoded))
+  (if-let [bitrate (ideal-bitrate production)]
+    (do
+      ;; encode the production
+      (encode-production production bitrate)
+      ;; downgrade to daisy202
+
+      ;; create an iso
+      (create-iso production)
+      ;; set the state to encoded
+      (prod/update! (assoc production :state :encoded)))
     ;; otherwise move the production to state :pending-volume-split
-    ;; FIXME: clean up first
-    (do
-      (clean-up production)
-      (prod/update! (assoc production :state :pending-volume-split)))))
+    (prod/update! (assoc production :state :pending-volume-split))))

            [me.raynes.fs :as fs]
            [mdr2.production :as prod]
            [mdr2.production.path :as path]
            [mdr2.pipeline1 :as pipeline]))

(defn encode-production
  "Encode a `production`, i.e. convert the wav files to mp3"
  [production]
  (let [output (path/encoded-path production)
        manifest (path/manifest-path production)]
    (fs/mkdir output)
    (pipeline/audio-encoder {:input (.getPath manifest) :output (.getPath output)})))

(defn create-iso
  "Pack a production in an iso file"

        "-J" ; Generate Joliet directory records in addition to regular ISO9660 filenames.
        "-o" iso-name encoded-path)))

(def bitrates
  "Possible bitrates for encoding"
  [128 64 48])

(defn ideal-bitrate
  "Calculate the ideal bitrate based on the size of a production and
  how much will generally fit on a CD-ROM. It will first try a higher
  bitrate (see `bitrates`). If the production still doesn't fit on one
  CD it will subsequently try lesser bitrates. Return nil if the
  content doesn't even fit on one CD with the lowest bitrate."
  [production]
  (let [dtb (path/recorded-path production)
        duration (dtb/audio-length dtb)
        sampling-ratio (if (dtb/mono? dtb) 1/2 1)
        max-bitrate (/ (* (/ max-size duration sampling-ratio) 8) 1000)]
    (loop [bitrates bitrates]
      (when-first [bitrate bitrates]
        (if (> max-bitrate bitrate)
          bitrate
          (recur (rest bitrates)))))))

(defn clean-up
  "Clean up temporary files of a production, namely the mp3 encoded
