(ns uswitch.bifrost.main
  (:require [uswitch.bifrost.system :refer (make-system)]
            [uswitch.bifrost.version :refer (current-version current-build-number)]
            [com.stuartsierra.component :refer (start)]
            [clojure.tools.logging :refer (info)]
            [clojure.tools.cli :refer (parse-opts)]
            [metrics.gauges :refer (gauge)])
  (:gen-class))

(defn wait! []
  (let [s (java.util.concurrent.Semaphore. 0)]
    (.acquire s)))

(def cli-options
  [["-c" "--config CONFIG" "Path to EDN configuration file"
    :default "./etc/config.edn"
    :validate [string?]]
   ["-h" "--help"]])

(defn -main [& args]
  (let [{:keys [options summary]} (parse-opts args cli-options)]
    (when (:help options)
      (println summary)
      (System/exit 0))
    (let [{:keys [config]} options]
      (info "Bifrost" (current-version))
      (when (current-build-number)
        (gauge "build-number" (current-build-number)))
      (info "Starting Bifrost with config" config)
      (start (make-system (read-string (slurp config))))
      (wait!))))

(ns uswitch.bifrost.main
  (:require [uswitch.bifrost.system :refer (make-system)]
            [uswitch.bifrost.version :refer (current-version current-build-number)]
            [com.stuartsierra.component :refer (start)]
            [clojure.tools.logging :refer (info)]
            [clojure.tools.cli :refer (parse-opts)]
            [metrics.gauges :refer (gauge)])
  (:gen-class))

(defn wait! []
  (let [s (java.util.concurrent.Semaphore. 0)]
    (.acquire s)))

(def cli-options
  [["-c" "--config CONFIG" "Path to EDN configuration file"
    :default "./etc/config.edn"
    :validate [string?]]
   ["-h" "--help"]])

(defn credentials
  [config]
  (let [{{:keys [access-key secret-key]
          :or {access-key (System/getenv "AWS_ACCESS_KEY_ID")
               secret-key (System/getenv "AWS_SECRET_ACCESS_KEY")}} :credentials} config]
    (update-in config [:credentials]
               assoc :access-key access-key :secret-key secret-key)))

(defn -main [& args]
  (let [{:keys [options summary]} (parse-opts args cli-options)]
    (when (:help options)
      (println summary)
      (System/exit 0))
    (let [{:keys [config]} options
          config (-> config slurp read-string credentials)]
      (info "Bifrost" (current-version))
      (when (current-build-number)
        (gauge "build-number" (current-build-number)))
      (info "Starting Bifrost with config" config)
      (start (make-system config))
      (wait!))))

Read AWS credentials from environment variables.
Read AWS credentials from environment variables.

If the credentials are not set in the configuration file, bifrost will
look for credentials in the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
environment variables.

These are the same environment variables used by the AWS SDKs as of July 2014.
(http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs)

samsara/bifrost,FundingCircle/bifrost