           (appengine :verbose verbose)
           (builtin/sift :move {#"(.*clj$)" (str classes-dir "/$1")})
           (builtin/sift :move {#"(.*\.class$)" (str classes-dir "/$1")})
+          (builtin/sift :move {#"(^.*)" (str mod "/$1")})
           ;; FIXME: use cache instead?
           ;; (builtin/target :dir #{(str "target/" mod)})
           )))

            [clojure.string :as str]
            [clojure.set :as set]
            [clojure.pprint :as pp]
            [stencil.core :as stencil]
            [boot.user]
            [boot.pod :as pod]
            [boot.core :as boot]
            [boot.util :as util]
            [boot.task.built-in :as builtin])
  (:import [com.google.appengine.tools KickStart]
           [java.io File]
           [java.net URL URLClassLoader]))

          (pod/unpack-jar jar-path (.getParent sdk-dir))))
      fileset)))

;;(core/deftask buber
(defn buber
  [tgt]
  (println "BUBER ")
  (let [;; tgt        (boot/tmp-dir!)
        cache      (boot/cache-dir! ::uber :global true)
        dfl-scopes #{"compile" "runtime"}
        scopes     dfl-scopes
        ;; scopes     (-> dfl-scopes
        ;;                (set/union include-scope)
        ;;                (set/difference exclude-scope))
        _ (println "SCOPES: " dfl-scopes)
        scope?     #(contains? scopes (:scope (util/dep-as-map %)))
        jars       (-> pod/env ;; (boot/get-env)
                       (update-in [:dependencies] (partial filter scope?))
                       pod/resolve-dependency-jars)
        _ (println "JARS: " jars)
        jars       (remove #(.endsWith (.getName %) ".pom") jars)
        checkouts  (->> (boot/get-checkouts)
                        (filter (comp scope? :dep val))
                        (into {}))
        co-jars    (->> checkouts (map (comp :jar val)))
        ;; co-dirs    (->> checkouts (map (comp :dir val)))
        ;; exclude    (or exclude pod/standard-jar-exclusions)
        ;; merge      (or merge pod/standard-jar-mergers)
        ;; reducer    (fn [xs jar]
        ;;              (boot/add-cached-resource
        ;;                xs (digest/md5 jar) (partial pod/unpack-jar jar)
        ;;                :include include :exclude exclude :mergers merge))
        ;; co-reducer #(boot/add-resource
        ;;               %1 %2 :include include :exclude exclude :mergers merge)
        ]
    ;; (boot/with-pre-wrap [fs]
    ;; (println "co jars: " co-jars)
      (when (seq jars)
        (doseq [jar jars] ;; (reduce into [] [jars co-jars])]
          (let [dest (doto (io/file tgt (.getName jar)) io/make-parents)]
            (println "dest: " dest)
            (file/copy-atomically jar dest))))))

      ;;       (when-not (.exists src)
      ;;         (println "CACHING " name)
      ;;         (util/dbug* "Caching jar %s...\n" name)
      ;;         (file/copy-atomically jar src))
      ;;       (util/dbug* "Adding cached jar %s...\n" name)
      ;;       (println (format "Adding cached jar %s...\n" name))
      ;;       (file/hard-link src (io/file tgt name)))))
      ;; #_(boot/commit! (if as-jars
      ;;                 (boot/add-resource fs tgt)
      ;;                 (reduce co-reducer (reduce reducer fs jars) co-dirs)))))

;; FIXME:  for dev phase, we want to include deps in test scope
(boot/deftask libs
  ""
  [v verbose bool "Print trace messages."]
  (let [checkout-vec (-> (boot/get-env) :checkouts)
        cos (map #(assoc (apply hash-map %) :coords [(first %) (second %)]) checkout-vec)

        dfl-scopes #{"compile" "runtime" "provided"}
        scopes dfl-scopes
        ;; scopes     (-> dfl-scopes
        ;;                (set/union include-scope)
        ;;                (set/difference exclude-scope))
        scope?     #(contains? scopes (:scope (util/dep-as-map %)))
        checkouts  (->> (boot/get-checkouts)
                        (filter (comp scope? :dep val))
                        (into {}))
        cache      (boot/cache-dir! ::uber :global true)
        ]
    ;; (println "CACHE: " cache)
    ;; (println "COS: " cos)
    (boot/with-pre-wrap [fileset]
      (let [tmpdir     (boot/tmp-dir!)]
      (doseq [co cos]
        (let [mod (if (:default co) "default"
                      (if-let [mod (:module co)]
                        mod "default"))
              coords (:coords co)
              pod-env (update-in (dissoc (boot/get-env) :checkouts)
                                 [:dependencies] #(identity %2)
                                 (concat '[[boot/core "2.6.0-SNAPSHOT"]
                                           [boot/pod "2.6.0-SNAPSHOT"]]
                                         (vector coords)))
              pod (future (pod/make-pod pod-env))]
          (if verbose (println "MODULE: " mod))
          (pod/with-eval-in @pod
              (require '[boot.pod :as pod]
                       '[boot.task.built-in :as builtin]
                       '[boot.util :as util]
                       '[boot.core :as boot]
                       '[boot.from.digest :as digest]
                       '[boot.file :as file]
                       '[clojure.java.io :as io])
              (let [dfl-scopes #{"compile" "runtime" "test"}
                    scopes dfl-scopes
                    scope? #(contains? scopes (:scope (util/dep-as-map %)))
                    co-jar (pod/resolve-dependency-jar (boot/get-env) '~coords)
                    jars   (remove #(= (str co-jar) (.getPath %))
                                   (-> pod/env
                                       (update-in [:dependencies] (partial filter scope?))
                                       (pod/resolve-dependency-jars true)))]
                (doseq [jar jars]
                  (let [hash (digest/md5 jar)
                        name (str hash "-" (.getName jar))
                        tmpjar-path (str ~mod "/WEB-INF/lib/" (.getName jar))
                        cached-jar  (io/file ~(.getPath cache) hash)]
                    (when-not (.exists cached-jar)
                      (if ~verbose (util/info "Caching jar %s...\n" name))
                      (file/copy-atomically jar cached-jar))
                    (if ~verbose (util/info "Adding cached jar %s...\n" tmpjar-path))
                    (file/hard-link cached-jar (doto (io/file ~(.getPath tmpdir) tmpjar-path)
                                                 io/make-parents))))))))
      (boot/commit! (boot/add-resource fileset tmpdir))))))

                  ;; (recur (rest jars)
                  ;;        fs

              ;;     ;;(println "dest: " dest)
              ;;     (file/hard-link jar dest)
              ;;     #_(file/copy-atomically jar dest)))
              ;; ;;~(buber out-dir) ;; :as-jars true :exclude-scope #{"provided"})
              ;; ))))))

            ;; #_(pod/copy-dependency-jar-entries pod/env
            ;;                                    ~(.getPath out-dir)
            ;;                                    '~coords
            ;;                                    #"^.*clj$"
            ;;                                    )))))))

    ;; #_(comp
    ;;  (builtin/uber :as-jars true :exclude-scope #{"provided"})
    ;;  ;; (builtin/sift :include #{#"zip$"} :invert true)
    ;;  ;; (builtin/sift :include #{#".*appengine-api-.*jar$"} :invert true)
    ;;  (builtin/sift :move {#"(.*\.jar$)" (str mod "/" lib-dir "/$1")})
    ;;  (builtin/sift :include #{#".*jar$"}))))

;; FIXME: drive this from logging.edn
(boot/deftask logging
