 
 (defn prep-for-stencil
   [& configs]
-  ;; (println "prep-for-stencil: " configs)
+  (println "prep-for-stencil: " configs)
   (let [nss (set (map #(-> % :ns) configs))
-        ;; _ (println "nss: " nss)
+        _ (println "nss: " nss)
         res (into [] (for [ns- nss]
+                       (do (println "ns- " ns-)
                        (let [ns-cfgs (filter #(= ns- (-> % :ns)) configs)]
                          ;; (println "XXXXXXXXXXXXXXXX config-ns: " ns-)
                          {:config-ns ns-
                           :config
                           (into [] (for [ns-cfg ns-cfgs]
-                                     ns-cfg))})))]
+                                     ns-cfg))}))))]
     ;; (println "FOR STENCIL:")
     ;; (pp/pprint res)
     res))
 
 (defn- merge-config-maps
   [ms]
+  (println "merge-config-maps: " ms)
   (let [ks (set (map #(symbol (:config-ns %)) ms))
         merged-maps (into []
                           (for [k (seq ks)]

                                ;;                    config-ns (count config-specs)))
                                ]
                            config-specs)))))
            config-nss (merge-config-maps config-maps)]
        ;; (println "CONFIG-MAPS: " (count config-nss))
        ;; (pp/pprint config-nss)
        (doseq [config-map config-nss]
            (let [config-file-name (str outdir "/" (ns->filestr (-> config-map :config-ns)) ".clj")
                  ;; _ (println "writing: " config-file-name)
                  config-file (stencil/render-file "boot_bowdlerize/bower.mustache"


(defn- normalize-configs
  [base configs]
  (println "normalize-configs: " configs)
  (let [bowers (filter #(:bower %) configs)
        _ (println "BOWERS: " bowers)
        normbowers (flatten (into [] (for [bower bowers]
                              (if (map? (:runtime bower))
                                (into [] (for [[sym uri] (:runtime bower)]
                                           (do ;; (println "FOO: "bower)
                                             (merge bower
                                                    {:runtime sym
                                                     :uri uri
                                                     :ns (namespace sym)
                                                     :name (name sym)}))))
                                (do ;; (println "BAR: " bower)

                                (let [m (bower-meta (:bower bower))
                                      kw (keyword (-> m :latest :name))
                                      _ (println "kw: " kw)
                                      poly (str/starts-with? (:bower bower) "Polymer")
                                      _ (println "poly: " poly)
                                      uris (bower->uris base m)]
                                  (merge bower (if (> (count uris) 1)
                                                 (throw (Exception.
                                                         (str "too many uris for bower pkg '"
                                                              (:bower bower)
                                                              "'; run bower info -j on the package and create a config for each latest.main entry"))))
                                         {:ns (namespace (:runtime bower))
                                          :name (name (:runtime bower))}
                                         ;; (if (str/ends-with? (first uris) ".js")
                                         ;;   {:js true})
                                         ;; (if (str/ends-with? (first uris) ".css")
                                         ;;   {:css true})
                                         (if poly {:polymer {:kw kw}})
                                         {:uri (first uris)})))))))

                                    ;; #_(merge bower
                                    ;;        {:ns (namespace (:runtime bower))
                                    ;;         :name (name (:runtime bower))})
        _ (println "NORMBOWERS: " normbowers)

        resources (filter #(not (:bower %)) configs)
        _ (println "RESOURCES: " resources)

        normresources (into [] (for [resource resources]
                                 (do (println "RESOURCE: " resource)
                                     (merge resource
                                            ;; (if (str/ends-with? (:uri resource) ".js")
                                            ;;   {:js true})
                                            ;; (if (str/ends-with? (:uri resource) ".css")
                                            ;;   {:css true})
                                            {:ns (namespace (:runtime resource))
                                             :name (name (:runtime resource))}))))
        _ (println "NORMRESOURCES: " normresources)

        simples (filter #(and (not (symbol? (first %)))
                              (or (symbol? (last %)) (map? (last %)))) configs)
        normsimples (for [[k v] simples]
               (merge {:bower k}
                      (condp apply [v] symbol? {:ns (symbol (namespace v)) :name (symbol (name v))}
                             map?    v)))
        _ (println "NORMSIMPLES: " normsimples)

        compounds (filter #(vector? (:runtime %)) configs)
        _ (println "COMPOUNDS: " compounds)
        normcomp (flatten (into [] (for [[pkg cfgs] compounds]
                                     (into [] (for [cfg cfgs]
                                                (do ;;(println "CFG: " cfg)
                                                (merge
                                                 ;; (if (str/ends-with? (:uri cfg) ".js")
                                                 ;;   {:js true})
                                                 ;; (if (str/ends-with? (:uri cfg) ".css")
                                                 ;;   {:css true})
                                                 {:bower pkg} cfg)))))))
        _ (println "NORMCOMPOUNDS: " normcomp)

        normed (concat normcomp normsimples normresources normbowers)
        _ (println "NORMED: " normed)
        ]
    normed))

(defn- get-config-maps
  "convert config map to data map suitable for stencil"
  [bower-base configs]
  (println "get-config-maps: " configs)
  (let [
        ;; nss (set (flatten (for [[k v] configs]
        ;;                     (if (string? k)
        ;;                       (cond
        ;;                         (symbol? v) (let [ns (namespace v)]
        ;;                                       (if ns (symbol ns)
        ;;                                           (throw (Exception. "var must be namespaced"))))
        ;;                         (map? v) (:ns v)
        ;;                         (vector? v)
        ;;                         (into [] (for [item v] (:ns item)))
        ;;                         ;;FIXME: better error msg
        ;;                         :else (throw (Exception. "config val must be map or vector of maps")))
        ;;                       (if (symbol? k)
        ;;                         (namespace k)
        ;;                         (throw (Exception. "config entries must be {sym {:uri str}} or {str config}")
        ;;                       ))))))
        ;; _ (println "nss: " nss)
        normal-configs (normalize-configs bower-base configs)
        _ (println "NORMAL-CONFIGS:")
        _ (pp/pprint normal-configs)
        ;;now add missing uris
        config-map (into [] (for [ns-config normal-configs]
                              (do (println "ns-config: " ns-config)
                              (if (-> ns-config :uri)
                                ns-config
                                (let [m (bower-meta (:bower ns-config))
                                      kw (keyword (-> m :latest :name))
                                      _ (println "kw: " kw)
                                      poly (str/starts-with? (:bower ns-config) "Polymer")
                                      _ (println "poly: " poly)
                                      uris (bower->uris bower-base m)]
                                  (merge ns-config (if (> (count uris) 1)
                                                     (throw (Exception.
                                                             (str "too many uris for bower pkg '"
                                                             (:bower ns-config)
                                                             "'; run bower info -j on the package and create a config for each latest.main entry")))
                                                     (merge
                                                      ;; (if (str/ends-with? (first uris) ".js")
                                                      ;;   {:js true})
                                                      ;; (if (str/ends-with? (first uris) ".css")
                                                      ;;   {:css true})
                                                      (if poly {:polymer {:kw kw}})
                                                      {:uri (first uris)}))))))))]
    (println "xxxx config-map: " config-map)
    config-map))

(defn- ns->filestr
