 	
 	apiClient.key = "HNA2dbrFtyTZxeHN6rThNg(("
 	apiClient.defaultFilter = "withbody"
+
+	let client = Client(host: .StackOverflow)
+
+	
+	redundaKey = try? String(contentsOfFile: "redunda_key.txt").trimmingCharacters(in: .whitespacesAndNewlines)
+	if redundaKey != nil {
+		//standby until Redunda tells us not to
+		var shouldStandby = false
+		var isFirst = true
+		repeat {
+			do {
+				let response = try sendStatusPing(client: client)
+				if response.shouldStandby {
+					isFirst = false
+					print("FireAlarm started in standby mode.")
+					sleep(30)
+				}
+			} catch {
+				handleError(error, "while sending a status ping to Redunda")
+			}
+		} while shouldStandby
+		if !isFirst {
+			print("FireAlarm activating...")
+		}
+	}
 	
 	
 	//Log in
-	let client = Client(host: .StackOverflow)
-	
 	let env =  ProcessInfo.processInfo.environment
 	
 	if !client.loggedIn {

		}
	}
	
	
	DispatchQueue.global().async(execute: inputFileMonitor)
	
	
	repeat {


let saveDirURL = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent(".firealarm", isDirectory: true)

var redundaKey: String?


fileprivate var listener: ChatListener!

var filter: Filter!

enum RedundaError: Error {
	case invalidJSON(json: Any)
}
struct RedundaResponse {
	let shouldStandby: Bool
}

func sendStatusPing(client: Client) throws -> RedundaResponse {
	let response = try client.parseJSON(try client.post("https://redunda.erwaysoftware.com/status.json", ["key":redundaKey!]))
	guard let json = response as? [String:Any] else {
		throw RedundaError.invalidJSON(json: response)
	}
	
	guard let shouldStandby = json["should_standby"] as? Bool else {
		throw RedundaError.invalidJSON(json: response)
	}
	
	return RedundaResponse(shouldStandby: shouldStandby)
}

func main() throws {
	print("FireAlarm starting...")
