        .getMainAttributes
        (map (fn [[k v]] [(str k) v]))
        (into {})))
+
+(defn get-target-dir
+  [fileset servlet]
+  (if servlet
+    "target"
+    (let [boot-config-edn-files (->> (boot/input-files fileset)
+                                     (boot/by-name [boot-config-edn]))
+          boot-config-edn-f (condp = (count boot-config-edn-files)
+                              0 nil
+                              1 (first boot-config-edn-files)  ;; this is a boot.tmpdir.TmpFile
+                              (throw (Exception.
+                                      (str "only one _boot_config.edn file allowed, found "
+                                           (count boot-config-edn-files)))))
+          boot-config-edn-map (if boot-config-edn-f
+                                (-> (boot/tmp-file boot-config-edn-f) slurp read-string)
+                                nil)]
+      (if boot-config-edn-map
+        (str "target/" (-> boot-config-edn-map :module :name))
+        (let [appengine-edn-fs (->> fileset
+                                    boot/input-files
+                                    (boot/by-name [appengine-edn]))
+
+              appengine-edn-f (condp = (count appengine-edn-fs)
+                                0 (throw (Exception. appengine-edn " file not found"))
+                                1 (first appengine-edn-fs)
+                                (throw (Exception. (str "Only one " appengine-edn " file allowed"))))
+
+              appengine-config-map (-> (boot/tmp-file appengine-edn-f) slurp read-string)]
+          (if servlet "target" (if (-> appengine-config-map :services)
+                                 "target/default"
+                                 (str "target/" (-> appengine-config-map :module :name)))))))))
 
 (defn expand-home [s]
   (if (or (.startsWith s "~") (.startsWith s "$HOME"))

  (if verbose (println "TASK: boot-gae/deploy"))
  (validate-tools-api-jar)
  ;; (println "PARAMS: " *opts*)
  (let [mod      (-> (boot/get-env) :gae :module :name)
        app-dir  (-> (boot/get-env) :gae :app :dir)
        ;; dir      (if service (str "target/" mod) "target")
        opts (merge {:sdk-root (:sdk-root config-map)
                     ;; :use-java7 true
                     :build-dir (str "target/" (if service mod))}
                                  ;; build-dir build-dir (gae-app-dir))} ;; (:build-dir config-map)}
                    *opts*)
        _ (println "OPTS: " opts)
        params (into [] (for [[k v] (remove (comp nil? second)
                                            (dissoc opts :build-dir :verbose :sdk-help))]
                          (str "--" (str/replace (name k) #"-" "_")
                               (if (not (instance? Boolean v)) (str "=" v)))))
        params (if (:sdk-help *opts*)
                 ["help" "update"]
                 (conj params "update" (:build-dir opts)))
        params (into-array String params)
        ;; ClassLoader classLoader = Thread.currentThread().contextClassLoader
        class-loader (-> (Thread/currentThread) (.getContextClassLoader))
        cl (.getParent class-loader)
        app-cfg (Class/forName appcfg-class true class-loader)]

  ;; def appCfg = classLoader.loadClass(APPENGINE_TOOLS_MAIN)
  ;; appCfg.main(params as String[])
    (def method (first (filter #(= (. % getName) "main") (. app-cfg getMethods))))
    (def invoke-args (into-array Object [params]))
    (if verbose ;; (or (:verbose *opts*) (:verbose config-map))
      (do (println "CMD: AppCfg")
          (doseq [arg params]
            (println "\t" arg))))
    (. method invoke nil invoke-args)))

(boot/deftask filters
  "generate filters class files"

        (builtin/sift :to-asset #{(re-pattern gen-filters-path)}))
       identity)
     )))

(defn- install-jarfile-internal
  [jarname]
  "Install jarfile from fileset"
  (fn middleware [next-handler]
    (fn handler [fileset]
      (let [jar-tmpfile (first (->> (boot/output-files fileset)
                                    (boot/by-re [(re-pattern (str ".*" jarname))])))
            jar-jfile (boot/tmp-file jar-tmpfile)
            jar-path (.getCanonicalPath jar-jfile)
            jar-name (.getName jar-jfile)
            target-middleware (comp
                               (builtin/install :file jar-path)
                               (builtin/sift :to-source #{(re-pattern (str ".*" jar-name))}))
            target-handler (target-middleware next-handler)]
        (target-handler fileset)))))

(boot/deftask install-service
  "Install service component"
  [p project PROJECT sym "project name"
   r version VERSION str "project version string"
   v verbose bool "Print trace messages"]
  (fn middleware [next-handler]
    (fn handler [fileset]
      (let [e (boot/get-env)
            target-dir (get-target-dir fileset false)
            jarname (util/jarname project version)
            jarpath (str target-dir "/" jarname)
            target-middleware (comp
                               (builtin/pom)
                               (builtin/jar)
                               (install-jarfile-internal jarname))
            target-handler (target-middleware next-handler)]
        (target-handler fileset)))))

(boot/deftask install-sdk
  "Unpack and install the SDK zipfile"
