 
 (def ^:dynamic *config*)
 
+;; we should add a compile directory option
+
+;; build option
+;; adds -c option if not available, and no other main options
+;; adds -c (:main build options) fail if no -c option and c main is not available
+;; when other main options merge with other -co options
+;; adds -c option if -r or -s option is present
+
+;; adds -c option
+;; and
+;; - (:main build options is present
+;; - -c option not present and -r or -s option and not other main option
+
+;; else option just adds options
+
+;; interesting case for handling when there is no main function but there is a watch
+
+;; in default compile
+;; takes care of default watch and default repl
+;; merges build-options with provided options
+
+(defn split-at-opt [opt-set args]
+    (split-with (complement opt-set) args))
+
+(defn split-at-main-opt [args]
+  (split-at-opt (set (keys (:main-dispatch cli/default-commands))) args))
+
+(defn get-init-opt [opt-set args]
+  (->> (split-at-main-opt args)
+       first
+       (split-at-opt opt-set)
+       last
+       second))
+
+(defn update-opt [args opt-set f]
+  (let [[mpre mpost] (split-at-main-opt args)
+        [opt-pre opt-post] (split-at-opt opt-set mpre)]
+    (if-not (empty? opt-post)
+      (concat opt-pre [(first opt-post)] [(f (second opt-post))]
+              (drop 2 opt-post) mpost)
+      (concat [(first opt-set) (f nil)] args))))
+
+(def get-build-opt (partial get-init-opt #{"-b" "--build"}))
+(def get-build-once-opt (partial get-init-opt #{"-bo" "--build-once"}))
+(defn get-main-opt-flag [args] (first (second (split-at-main-opt args))))
+
+(defn handle-build-opt [args]
+  (if-let [build-name (or (get-build-opt args) (get-build-once-opt args))]
+    (let [build-options (get-build build-name)
+          main-flag (get-main-opt-flag args)
+          main-ns (:main build-options)]
+      (cond
+        (#{"-c" "--compile" "-m" "--main" "-h" "--help" "-?"} main-flag)
+        args
+        main-ns
+        (let [[pre post] (split-at-main-opt args)]
+          (concat pre ["-c" (str main-ns)] post))
+        :else args))
+    args))
+
 ;; TODO figwheel.core start when not --repl
 (defn -main [& args]
   (let [[pre post] (split-with (complement #{"-re" "--repl-env"}) args)
-        args (if (empty? post) (concat ["-re" "figwheel" ] args) args)]
+        args (if (empty? post) (concat ["-re" "figwheel" ] args) args)
+        args (handle-build-opt args)]
     (with-redefs [cljs.cli/default-compile default-compile]
       (apply cljs.main/-main args))))
 
 (def args
-  (concat ["-co" "{:aot-cache false :asset-path \"out\"}" "-e" "(figwheel.core/start-from-repl)"]
+  (concat ["-co" "{:aot-cache false :asset-path \"out\"}" "-b" "dev" "-e" "(figwheel.core/start-from-repl)"]
           (string/split "-w src -d target/public/out -o target/public/out/mainer.js -c exproj.core -r" #"\s")))
+
+#_(handle-build-opt (concat (first (split-at-main-opt args)) ["-h"]))
 
 #_(apply -main args)
 #_(.stop @server)

   [cljs.build.api :as bapi]
   [hawk.core :as hawk])
  (:import
   [java.io StringReader]))

;; ------------------------------------------------------------
;; Watching
;; ------------------------------------------------------------

(def ^:dynamic *watcher* (atom {:watcher nil :watches {}}))

(defn alter-watches [{:keys [watcher watches]} f]
  (when watcher (hawk/stop! watcher))

           :doc (str "Use Figwheel to auto reload and report compile info.\n"
                     "Only takes effect when watching is happening and the\n"
                     "optimizations level is :none or nil.\n"
                     "Defaults to true.")}
          ["-b" "--build"]
          {:group :cljs.cli/compile :fn build-opt
           :arg "build-name"
           :doc (str "The name of a build config to build.")}
          ["-bo" "--build-once"]
          {:group :cljs.cli/compile :fn build-once-opt
           :arg "build-name"
           :doc (str "The name of a build config to build once.")}}})

(alter-var-root #'cli/default-commands cli/add-commands
                figwheel-commands)
