 }
 
 
-//MARK: Functions Storyboards
+//MARK: Functions
+
+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
+}
 
 func findStoryboards(rootPath: String, suffix: String) -> [String]? {
     var result = Array<String>()

    }
}

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"
    
    static func fromString(string: String) -> OS? {
        for os in OS.allValues {
            if  NSString(string: os.rawValue).caseInsensitiveCompare(string) == NSComparisonResult.OrderedSame {
                return os
            }
        }
        return nil
    }
    
    static func fromTargetRuntime(targetRuntime: String) -> OS? {
        for os in OS.allValues {
            if  os.targetRuntime == targetRuntime {
                return os
            }
        }
        return nil
    }
    
    static let allValues = [iOS, OSX]
    
    var description: String {return self.rawValue}
    
    var framework: String {
        switch self {
        case iOS: return "UIKit"
        case OSX: return "Cocoa"
        }
    }
    
    var targetRuntime: String {
        switch self {
        case iOS: return "iOS.CocoaTouch"
        case OSX: return "MacOSX.Cocoa"
        }
    }
    
    var typePrefix: String {
        switch self {
        case iOS: return "UI"
        case OSX: return "NS"
        }
    }
    
    var storyboardType: String {
        return typePrefix + "Storyboard"
    }

    var storyboardSegueType: String {
        return typePrefix + "StoryboardSegue"
    }
    
    var storyboardTypeUnwrap: String {
        switch self {
        case iOS: return ""
        case OSX: return "!"
        }
    }
    
    var storyboardControllerTypes: [String] {
        switch self {
        case iOS: return ["UIViewController"]
        case OSX: return ["NSViewController", "NSWindowController"]
        }
    }
    
    var storyboardControllerSignatureType: String {
        switch self {
        case iOS: return "ViewController"
        case OSX: return "Controller" // NSViewController or NSWindowController
        }
    }
    
    var storyboardControllerReturnType: String {
        switch self {
        case iOS: return "UIViewController"
        case OSX: return "AnyObject" // NSViewController or NSWindowController
        }
    }
    
    var storyboardControllerInitialReturnTypeCast: String {
        switch self {
        case iOS: return "as? \(self.storyboardControllerReturnType)"
        case OSX: return ""
        }
    }
    
    var storyboardControllerReturnTypeCast: String {
        switch self {
        case iOS: return " as! \(self.storyboardControllerReturnType)"
        case OSX: return "!"
        }
    }
    
    func storyboardControllerInitialReturnTypeCast(initialClass: String) -> String {
        switch self {
        case iOS: return "as! \(initialClass)"
        case OSX: return ""
        }
    }


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")
        }
    }
}
