    This is a command-line tool for importing data into PuppetDB. It expects
    as input a tarball generated by the PuppetDB `export` command-line tool."
   (:import [org.apache.commons.compress.archivers.tar TarArchiveEntry]
-           [puppetlabs.puppetdb.archive TarGzReader])
+           [puppetlabs.puppetdb.archive TarGzReader]
+           [java.io File])
   (:require [clojure.java.io :as io]
             [clojure.tools.logging :as log]
             [puppetlabs.puppetdb.archive :as archive]

            [puppetlabs.puppetdb.schema :refer [defn-validated]]
            [puppetlabs.puppetdb.utils :as utils]))

(defn file-pattern
  [entity]
  (re-pattern (str "^" (.getPath (io/file utils/export-root-dir entity ".*\\.json")) "$")))

(def after-certname-length
  "The format is <certname>-<date hash><suffix>. This is a count of
  the characters after the certname"
  (+ 1
     40
     (count export/file-suffix)))

(def export-root-dir-length
  (count utils/export-root-dir))

(defn extract-certname [^String command-type ^String path]
  (let [path-length (.length path)
        prefix-length (+ export-root-dir-length
                         (.length command-type)
                         2)]
    (subs path prefix-length (- path-length after-certname-length))))

(defn extract-facts-certname [^String path]
  (let [prefix-length (+ export-root-dir-length
                         (.length "facts")
                         2)]
    (subs path prefix-length (- (.length path) (count export/file-suffix)))))

(defn-validated process-tar-entry
  "Determine the type of an entry from the exported archive, and process it

   tar-reader :- TarGzReader
   tar-entry :- TarArchiveEntry
   command-versions]
  (let [path (.getName tar-entry)
        [command-type certname] (command-matcher path)
        command-fn' (fn [command-kwd command-version]
                      (command-fn command-kwd
                                  command-version
                                  certname
                                  (-> tar-reader
                                      utils/read-json-content
                                      json/generate-string
                                      (.getBytes "UTF-8")
                                      java.io.ByteArrayInputStream.)))]
    (case command-type
      "catalogs"
      (do
        (log/infof "Importing catalog from archive entry '%s'" path)
        (command-fn' :replace-catalog
                     (:replace_catalog command-versions)))
      "reports"
      (do
        (log/infof "Importing report from archive entry '%s'" path)
        (command-fn' :store-report
                    (:store_report command-versions)))
      "facts"
      (do
        (log/infof "Importing facts from archive entry '%s'" path)
        (command-fn' :replace-facts
                     (:replace_facts command-versions)))
      nil)))

(def metadata-path
