 //  Copyright (c) 2015 Giovanni Collazo. All rights reserved.
 //
 
+import Foundation
 import Cocoa
+
+public let appName = "MongoDB.app"
 
 @NSApplicationMain
 class AppDelegate: NSObject, NSApplicationDelegate {
     @IBOutlet weak var updater: SUUpdater!
     
-    var paths = NSSearchPathForDirectoriesInDomains(
-        FileManager.SearchPathDirectory.documentDirectory,
-        FileManager.SearchPathDomainMask.userDomainMask, true)
-    
-    var documentsDirectory: AnyObject
+    static var userApplicationSupportDirectory =
+        NSSearchPathForDirectoriesInDomains(
+            FileManager.SearchPathDirectory.applicationSupportDirectory,
+            FileManager.SearchPathDomainMask.userDomainMask, true
+        ).first!
+    
     var dataPath: String
     var logPath: String
     

        if (!FileManager.default.fileExists(atPath: self.dataPath)) {
            do {
                try FileManager.default
                    .createDirectory(atPath: self.dataPath, withIntermediateDirectories: false, attributes: nil)
            } catch {
                print("Something went wrong creating dataPath")
            }
        }

        if (!FileManager.default.fileExists(atPath: self.logPath)) {
            do {
                try FileManager.default
                    .createDirectory(atPath: self.logPath, withIntermediateDirectories: false, attributes: nil)
            } catch {
                print("Something went wrong creating logPath")
            }
        }


    var updatesMenuItem: NSMenuItem = NSMenuItem()
    
    override init() {

        let appSupport = URL(fileURLWithPath: AppDelegate.userApplicationSupportDirectory)

        // App version follows mongoDB version. Add it to the directory
        guard let appVersion = Bundle.main.infoDictionary?["CFBundleVersion"] as? String else {
            fatalError("Unable to determine application version from Info.plist")
        }

        let dataDirectory = appSupport
            .appendingPathComponent(appName)
            .appendingPathComponent(appVersion)

        self.dataPath = dataDirectory.appendingPathComponent("Data").path
        self.logPath = dataDirectory.appendingPathComponent("Logs").path

        self.file = self.pipe.fileHandleForReading

        super.init()
    }
    
