     }
 }
 
-
+private func searchAll(root: XMLIndexer, attributeKey: String, attributeValue: String) -> [XMLIndexer]? {
+    var result = Array<XMLIndexer>()
+    for child in root.children {
+        if let element = child.element where element.attributes[attributeKey] == attributeValue {
+            return [child]
+        }
+        if let found = searchAll(child, attributeKey, attributeValue) {
+            result += found
+        }
+    }
+    return result.count > 0 ? result : nil
+}
+
+//MARK: Objects
 enum OS: String, Printable{
     case iOS = "iOS"
     case OSX = "OSX"

                if let controllerType = os.controllerTypeForElementName(vc.element!.name) {
                    return controllerType
                }
            }
        }
    }
    return nil
}

func findStoryboardOS(storyboardFile: String) -> OS? {
    if let data = NSData(contentsOfFile: storyboardFile) {
        let xml = SWXMLHash.parse(data)
        if let targetRuntime = xml["document"].element?.attributes["targetRuntime"] {
            return OS.fromTargetRuntime(targetRuntime)
        }
    }
    return nil
}

private func storyboardIdentifierExtenstion(viewController: XMLIndexer) -> String? {
    var result:String? = nil
    if let customClass = viewController.element?.attributes["customClass"] {
        var output = String()
        output += "extension \(customClass) {\n"
        if let viewControllerId = viewController.element?.attributes["storyboardIdentifier"] {
            output += "    override class var storyboardIdentifier:String? { return \"\(viewControllerId)\" }\n"
        }
        output += "}"
        result = output
    }
    return result
}

func processStoryboard(storyboardFile: String, os: OS) {
    if let data = NSData(contentsOfFile: storyboardFile) {
        let xml = SWXMLHash.parse(data)

        if let viewControllers = searchAll(xml, "sceneMemberID", "viewController") {
            for viewController in viewControllers {
                if let customClass = viewController.element?.attributes["customClass"] {
                    let segues = viewController["connections"]["segue"].all.filter({ return $0.element?.attributes["identifier"] != nil })


let argument = Process.arguments[1]
var storyboards:[String] = []
let storyboardSuffix = ".storyboard"
if argument.hasSuffix(storyboardSuffix) {
    storyboards = [argument]
} else if let s = findStoryboards(argument, storyboardSuffix) {
    storyboards = s
}
let storyboardFiles: [StoryboardFile] = storyboards.map { StoryboardFile(filePath: $0) }

for os in OS.allValues {
    var storyboardsForOS = storyboardFiles.filter { $0.os == os }
    if !storyboardsForOS.isEmpty {
        if storyboardsForOS.count != storyboardFiles.count { println("#if os(\(os.rawValue))") }
        processStoryboards(storyboardsForOS, os)
        if storyboardsForOS.count != storyboardFiles.count { println("#endif") }
    }
}
