      [:hr]
      [:ul
       (map #(vector :li [:a {:href (fs/join resource-dir (args :dir) %)} %])
-           files)]))
-  )
+           files)])))
 
 
 (def dbg (atom {}))
-
-(declare
- get-jobinfo
- get-toolinfo)
 
 (defn htseq-file-get [args reqmap]
   (infof "Args %s\nParams %s" args (reqmap :params))

  {:author "Jon Anthony"}

  (:require
   [clojure.java.io          :as io]
   [clojure.string           :as str]
   [clojure.set              :as set]
   [clojure.data.json        :as json]
   [clojure.tools.reader.edn :as edn]

   [ring.middleware.defaults]
   [ring.util.codec :as ruc]

   [compojure.core     :as comp
    :refer (defroutes GET POST)]

   [compojure.route    :as route]
   [hiccup.core        :as hiccup]

   [clojure.core.async :as async
    :refer (<! <!! >! >!! put! chan go go-loop alt! timeout alt! alts!!)]

   [taoensso.timbre    :as timbre
    :refer (tracef debugf infof warnf errorf)]
   [taoensso.sente     :as sente]

   [org.httpkit.server :as http-kit]
   [taoensso.sente.server-adapters.http-kit :refer (sente-web-server-adapter)]

   ;; Watch tool config directory to automatically configure new tools
   [clojure-watch.core :refer [start-watch]]

   ;; BinaryJS msg protocol
   [aerial.msgpacket.binaryjs :as bjs :refer [newBinaryJsMsg]]

   [aerial.fs :as fs]
   [aerial.utils.misc :as aum]
   [aerial.utils.io   :as aio]
   [aerial.utils.string :as austr]
   [aerial.utils.coll :as coll :refer [in takev-until dropv-until ensure-vec]]


         [:hr]
         [:p (str "Params" params)]
         [:hr]
         [:p (str "User Agent: " user-agent)])))))


(def aerobio-routes
  (apply routes
         (conj (srv/hanasu-handlers)
               (GET "/" request (landing-handler request))
               (GET "/htseq/cmd"   [:as reqmap & args]
                    (htseq-file-get args reqmap))
               (route/resources "/"))))

(def aerobio-handler
  (-> aerobio-routes
      #_(ring.middleware.defaults/wrap-defaults
       ring.middleware.defaults/site-defaults)
      #_(wrap-cljsjs)
      #_(wrap-gzip)))


;;; ------------------------------------------------------------------------;;;
;;;                   Aerobio Application Messaging...                      ;;;
;;; ------------------------------------------------------------------------;;;

(defmulti user-msg :op)

(defmethod user-msg :default [msg]
  (infof "ERROR: unknown user message %s" msg)
  #_(srv/send-msg
   (msg :ws) {:op "error" :payload "ERROR: unknown user message"}))


;;; ------------------------------------------------------------------------;;;
;;;                Control  starting and stoping Webserver                  ;;;
;;; ------------------------------------------------------------------------;;;

(defn msg-handler [msg]
  (infof ":MSG-HANDLER :MSG %s" msg)
  (let [{:keys [op data]} (msg :data)]
    (case op
      ;;null for now
      (user-msg (msg :data)))))


(defn on-open [ch op payload]
  (let [ws payload
        uid-name ((-> :idfn get-adb first))
        connfn (-> :connfn get-adb first)
        uuid (uuid)
        uid {:uuid uuid :name uid-name}
        data (connfn {:uid uid})
        name-uuids (or (get-adb uid-name) [])]
    (infof ":SRV :open %s" uid)
    (update-adb [uuid :ws] ws, [uuid :name] uid-name
                [uuid :rcvcnt] 0, [uuid :sntcnt] 0
                [ws :uuid] uuid
                uid-name (conj name-uuids uuid))
    (srv/send-msg ws {:op :register :data data})))


(defn server-dispatch [ch op payload]
  (case op
    :msg (let [{:keys [ws]} payload]
           (msg-handler payload)
           (update-adb :rcvcnt inc, [ws :rcvcnt] inc))

    :open (on-open ch op payload)
    :close (let [{:keys [ws status]} payload
                 uuid (get-adb [ws :uuid])
                 uuid-name (get-adb [uuid :name])
                 uuids (get-adb uuid-name)]
             (infof ":SRV :close :uuid %s, :uuid-name %s" uuid uuid-name)
             (update-adb ws :rm, uuid :rm
                         uuid-name (->> uuids (remove #(= uuid %)) vec)))

    :bpwait (let [{:keys [ws msg encode]} payload
                  uuid (get-adb [ws :uuid])]
              (infof ":SRV :uuid %s - Waiting to send msg %s" uuid msg)
              (update-adb [uuid :bpdata] {:msg msg, :encode encode}))
    :bpresume (let [{:keys [ws msg]} payload
                    uuid (get-adb [ws :uuid])
                    encode ((get-adb [uuid :bpdata]) :encode)]
                (infof ":SRV BP Resume :uuid %s" (get-adb [ws :uuid]))
                (srv/send-msg ws msg :encode encode))

    :sent (let [{:keys [ws msg]} payload]
            #_(infof ":SRV Sent msg %s" msg)
            (update-adb :sntcnt inc, [ws :sntcnt] 0))
    :failsnd (infof ":SRV Failed send for {:op %s :payload %s}" op payload)

    :stop (let [{:keys [cause]} payload]
            (infof ":SRV Stopping reads... Cause %s" cause)
            (update-adb)
            (srv/stop-server))
    (infof ":SRV :WTF {:op %s :payload %s}" op payload)))

(defn start-server
  [port]
  (let [ch (srv/start-server port :main-handler aerobio-handler)]
    (infof "Server start, reading msgs from %s" ch)
    (update-adb :chan ch
                :idfn [(partial gensym "aerobio-")]
                :connfn [identity])
    (go-loop [msg (<! ch)]
      (let [{:keys [op payload]} msg]
        (future (server-dispatch ch op payload))
        (when (not= op :stop)
          (recur (<! ch)))))))

(defn stop-server []
  (async/>!! (get-adb :chan) {:op :stop :payload {:cause :userstop}}))


(defn start! [port]
  (timbre/set-level! :info) ; :debug
  (start-tool-watcher)
  (start-job-watcher)
  (start-server port))

(defn stop! []
  (stop-tool-watcher)
  (stop-job-watcher)
  (stop-server))

