 (defn nh-stop [watcher]
   (.close (:watch-service @watcher)))
 
+(defn insert-xlogfile-line-db [line]
+  (let [data (parse-line line)]
+    (let [record (assoc data :death_uniq (.replaceAll (:death data) ", while .*$", ""))]
+      (sql/insert-records :xlogfile
+                        record))))
+
+(defn nh-init-db []
+  (.info logger "re-initializing database..")
+  (if (.exists (File. "/tmp/nh.db"))
+    (.delete (File. "/tmp/nh.db")))
+  (sql/with-connection db
+    (sql/transaction
+     (sql/create-table
+      :xlogfile
+      [:id "INTEGER PRIMARY KEY AUTOINCREMENT"]
+      [:version "TEXT"]
+      [:points "INT"]
+      [:deathdnum "INT"]
+      [:deathlev "INT"]
+      [:maxlvl "INT"]
+      [:dlev_name "TEXT"]
+      [:hp "INT"]
+      [:maxhp "INT"]
+      [:deaths "INT"]
+      [:deathdate "DATE"]
+      [:birthdate "DATE"]
+      [:uid "INT"]
+      [:role "TEXT"]
+      [:race "TEXT"]
+      [:gender "TEXT"]
+      [:align "TEXT"]
+      [:name "TEXT"]
+      [:death "TEXT"]
+      [:death_uniq "TEXT"]
+      [:conduct "TEXT"]
+      [:turns "INT"]
+      [:achieve "TEXT"]
+      [:realtime "INT"]
+      [:starttime "TIMESTAMP"]
+      [:endtime "TIMESTAMP"]
+      [:gender0 "TEXT"]
+      [:align0 "TEXT"])
+     (let [lines (read-lines (str (:un-dir config) "xlogfile"))]
+       (doseq [line lines]
+         (insert-xlogfile-line-db line)))))
+  (.info logger "database initialized"))
+
 (defn nh-start [watcher]
+  (nh-init-db)
   (.start (:thread @watcher)))

    
    :killed_uniq (str (:player data) " killed " (:killed_uniq data) " after " (:turns data) " turns.")
    
    :killed_shopkeeper (str (:player data) " killed the shopkeeper " (:killed_shopkeeper data) " after " (:turns data))
    
    :shoplifted (format "%s stole %s zorkmids worth of merchandise from %s%s %s after %s turns"
                        (:player data)
                        (:shoplifted data)
                        (:shopkeeper data)
                        (possessive (:shopkeeper data))
                        (:shop data)
                        (:turns data))

                      (map #(File. (str "/srv/un.nethack.nu/users/" nick "/dumps/" nick ".last" %)) exts)))]
    (if file
      (let [fp (.getCanonicalPath file)]
        (str "http://un.nethack.nu/user/" nick "/dumps" (.substring fp (.lastIndexOf fp "/"))))
      nil)))
        
(defn last-dump-hook [irc object match]
