        (catch org.apache.tools.ant.BuildException e
          (println "Incorrect Password.")))))
 
-(defn- merge-settings [{env :env :as settings}]
+(defn merge-settings [{env :env :as settings}]
   (let [defaults {:user (or (get config (str env ".user"))
                             (System/getProperty "user.name"))
                   :keys (find-keys env)
                   :passphrase (when (:p opts) (prompt-read "Enter passphrase"))}]
     (merge defaults settings)))
 
-(defn- deploy*
+(defn deploy*
   [settings]
-  (let [{user :user source :source hosts :hosts path :path keys :keys passphrase :passphrase} (merge-settings settings)]
+  (let [{:keys [user source hosts path keys passphrase]} (merge-settings settings)]
     (doseq [host hosts]
       (let [dest (str host ":" path)]
         (println "Attempting to copy:" (.toString source) "to" dest)
-        (transfer user source dest keys passphrase)
+        (try-scp user source dest keys passphrase)
         (when-not @*success*
           (when (and keys (not (:p opts)))
             (println "All keyfiles failed, if a keyfile passphrase is required, use the -p flag"))
-          (transfer user source dest))))))
+          (try-scp user source dest))))))
 
 (defn deploy [& args]
   (if (vector? (first args))


(def *success* (atom false))

(defn- scp
  "call the scp ant task with options"
  [user to-file to-path & options]
  (let [settings {:trust true

        options (into settings options)]
    (ant Scp options)))

(defn- get-key-options
  "check the project options for a keyfile"
  []
  (seq (map file (filter #(not (nil? %))
                         (map #(-> % opts first)
                              [:i :identity])))))

(defn get-key-setting
  "look for a keyfile specified in options or config"
  [env]
  (when-let [kf (get config (str env ".keyfile"))]
    [(file kf)]))

(defn get-keys-in
  "look for id_rsa, id_dsa, or identity in path"
  [path]
  (seq (filter #(.exists %)
               (map #(file (java.io.File. path) ".ssh" %)
                    ["id_rsa" "id_dsa" "identity"]))))

(defn find-keys [& [env]]
  (let [keys (or (get-key-options)
                 (when env (get-key-setting env))
                 (get-keys-in (System/getProperty "user.home")))]
    (when keys (apply println "Using keyfiles:" (map #(-> % .toString .trim) keys)))
    keys))

(defn try-scp
  ([user source dest keys passphrase]
     (doseq [key keys :while (not @*success*)]
       (print (.toString key) "...")
