 (defn start! [site-config]
   (swap! config merge site-config)
   (when r/is-client
-    (let [conf (when (exists? js/pageConfig)
-                 (js->clj js/pageConfig :keywordize-keys true))]
-      (swap! config merge conf)
-      (init-history)
-      (r/render-component (:body @config)
-                          (js/document.getElementById (:main-div @config))))))
+    (let [page-conf (when (exists? js/pageConfig)
+                      (js->clj js/pageConfig :keywordize-keys true))
+          conf (swap! config merge page-conf)]
+      (init-history (:page-name conf))
+      (r/render-component (:body conf)
+                          (js/document.getElementById (:main-div conf))))))


;;; History

(defn init-history []
  (when-not history
    (let [page (:page-name @config)
          html5 (and page
                     (.isSupported Html5History)
                     (#{"http:" "https:"} js/location.protocol))]
      (doto (set! history

                               :page-conf {:page-name page-name}
                               :body-html bhtml)))))

(defn fs [] (js/require "fs"))
(defn path [] (js/require "path"))

(defn mkdirs [f]
  (let [items (as-> f _
                (.' (path) normalize _)
                (string/split _ #"/"))]
    (doseq [d (reductions #(str %1 "/" %2) items)]
      (when-not (.' (fs) existsSync d)
        (.' (fs) mkdirSync d)))))

(defn write-file [f content]
  (mkdirs (.' (path) dirname f))
  (.' (fs) writeFileSync f content))

(defn read-file [f]
  (.' (fs) readFileSync f))

(defn path-join [& paths]
  (apply (.' (path) :join) paths))

(defn read-files [files]
  (string/join "\n" (map read-file files)))
