   [{id :id}]
   (get all-files-by-id id))
 
-(defn ready-productions
-  "All productions with state \"ready\""
-  [productions]
-  (filter #(= (:state %) "ready") productions))
+(defn ready? [production] (= (:state production) "ready"))
+(defn archived? [production] (= (:state production) "archived"))
+(defn recording? [production] (= (:state production) "recording"))
+(defn recording-no-wav? [production] (and (recording? production)
+                                          (not-any? wav-file? (get-all-files production))))
+(defn recording-no-wav-with-struct? [production] (and (recording-no-wav? production)
+                                                      (some struct-file? (get-all-files production))))
+(defn recording-with-wav? [production] (and (recording? production)
+                                            (some wav-file? (get-all-files production))))
+(defn recording-with-obi? [production] (and (recording? production)
+                                            (some wav-file? (get-all-files production))
+                                            (some obi-project? (get-all-files production))))
+(defn recording-with-wav-no-obi? [production] (and (recording? production)
+                                                   (some wav-file? (get-all-files production))
+                                                   (not-any? obi-project? (get-all-files production))))
 
 (defn create-ready-productions!
   "Create all productions with state \"ready\""
-  []
-  (doseq [p (ready-productions all-productions)]
-    (-> p prod/create!)))
-
-(defn archived-productions
-  "All productions with state \"archived\""
-  [productions]
-  (filter #(= (:state %) "archived") productions))
+  [productions]
+  (doseq [p (filter ready? productions)]
+    (if (:library_number p)
+      (-> p prod/create! prod/set-state-structured! dtbook/dtbook-file)
+      (-> p prod/create!))))
 
 (defn create-archived-productions!
   "Create all productions with state \"archived\""
-  []
-  (->> (archived-productions all-productions)
+  [productions]
+  (->> (filter archived? productions)
        (map #(assoc % :state "archived"))
        (map prod/add-default-meta-data)
        (apply jdbc/insert! db :production)))
-
-(def recording-productions-without-wav
-  "All productions with state \"recording\" that do not contain any wav file"
-  (filter #(and (= (:state %) "recording")
-                (not (contains-wav-files (get-all-files %)))) all-productions))
-
-(def recording-productions-with-struct
-  "All productions with state \"recording\" that do not contain any wav but a struct file"
-  (filter #(contains-struct-file (get-all-files %)) recording-productions-without-wav))
 
 (defn create-recording-productions-with-struct!
   "Create all productions with state \"recording\" that do not contain
   any wav but a struct file. Add to the db, create the directories and
   create the config file. Finally convert the \"struct.html\""
-  []
-  (doseq [p recording-productions-with-struct]
+  [productions]
+  (doseq [p (filter #(recording-no-wav-with-struct? %) productions)]
     (-> p
         prod/create!
         prod/set-state-structured!

          (copy-struct-file!)
          (convert-struct-file!)))))

(def recording-productions-with-wav
  "All productions with state \"recording\" that contain wav files"
  (filter #(and (= (:state %) "recording")
                (contains-wav-files (get-all-files %)))
          all-productions))

(def recording-productions-with-obi
  "All productions with state \"recording\" with wav files that contain an obi project"
  (filter #(contains-obi-project (get-all-files %))
          recording-productions-with-wav))

(defn create-recording-productions-with-obi!
  "Create all productions with state \"recording\" with wav files that
  contain an obi project. Add to the db and create the directories.
  Copy the obi project to the new place. Place an obi config file
  inside the obi project"
  []
  (doseq [p recording-productions-with-obi]
    (-> p
        prod/create!
        prod/set-state-structured!

  (with-open [r (clojure.java.io/reader f)]
    (group-by extract-id (line-seq r))))

(defn wav-file? [f] (re-matches #".*\.wav$" f))
(defn struct-file? [f] (re-matches #".*/struct.html$" f))
(defn obi-project? [f] (re-matches #".*/project.obi$" f))

(def abacus-csv "/home/eglic/src/mdr2/samples/ABACUS_export_Books_27032015.csv")
(def all-productions (-> abacus-csv io/file productions))
