 ;;;;   limitations under the License.
 
 (ns biblecli.commands.bucketsync
-  (:require-macros [cljs.core.async.macros :refer [go]])
-  (:require [cljs.core.async :refer [to-chan chan put! tap mult <!]]
+  (:require-macros [cljs.core.async.macros :refer [go go-loop]])
+  (:require [cljs.core.async :refer [to-chan chan put! tap mult <! buffer onto-chan] :as async]
             [cljs.nodejs :refer [require process]]
             [clojure.string :as s]))
 

    (AWS.S3. (clj->js cfg))))

(defn partition-files [s3keys files]
  [(->> s3keys (filter (fn [[key _]] (not (contains? files key)))) (vec))
   (->> files (filter (fn [[key _]] (not (contains? s3keys key)))) (vec))
   (->> files
        (map (fn [[key path]] (let [etag (get s3keys key :notfound)] [key path etag])))
        (filter (fn [[_ _ etag]] (not= etag :notfound)))
        (vec))])

(defn delete-files [s3 onlyremote delete whatif verbose]
  (go
    (println onlyremote)
    (let [parts (partition-all 1000 onlyremote)]
      (loop [part (first parts)
             next (rest parts)]
        (if (not part)
          [nil true]
          (do
            (doseq [key part]
              (println (str (if whatif "Whatif: " "") "Deleting '" key "' from remote bucket.")))
            (recur (first next) (rest next))))))))

(defn copy-files [s3 onlylocal whatif]
  (go))

(defn update-files-if-changed [s3 filelist whatif force verbose]
  (go))

(defn bucketsync
  {:summary "Synchronize a remote Amazon S3 bucket with a local asset directory."

                  :default {:bucket "kingjames-beta"
                            :region "us-east-1"
                            :profile "default"}}}
  [{dir :_ force :force whatif :whatif delete :delete bucket :bucket region :region profile :profile verbose :verbose :as f}]
  (go
    (if (not= (count dir) 1)
      ["<dir> parameter required." nil]
