 import Foundation
 
 let currentPath = NSFileManager.defaultManager().currentDirectoryPath
-let defaultSourceIconPath = currentPath.stringByAppendingPathComponent("icon.png")
-let iconResolutions = [29, 40, 58, 76, 80, 87, 120, 152, 180]
+let defaultSourceIconPath = NSURL.fileURLWithPath(currentPath).URLByAppendingPathComponent("icon.png")
+let iconResolutions = [29, 40, 48, 50, 55, 57, 58, 60, 72, 76, 80, 87, 88, 100, 114, 120, 144, 152, 167, 171, 172, 180, 196]
 
-func outputDirPathForIconPath(iconPath: String) -> String
+func outputDirPathForIconPath(iconPath: NSURL) -> NSURL
 {
-    return iconPath.stringByDeletingLastPathComponent.stringByAppendingPathComponent("\(iconPath.lastPathComponent.stringByDeletingPathExtension)_output")
+    let a = iconPath.URLByDeletingPathExtension?.lastPathComponent
+    let b = iconPath.URLByDeletingLastPathComponent
+    let c = b!.URLByAppendingPathComponent("\(a!)_output")
+    return c
 }
 
-func scaleIcon(iconPath: String, resolution: Int)
+func scaleIcon(iconPath: NSURL, resolution: Int)
 {
     let task = NSTask()
     task.launchPath = "/usr/bin/sips"
-    task.arguments = ["-Z", "\(resolution)", iconPath]
+    task.arguments = ["-Z", "\(resolution)", iconPath.path!]
     task.launch()
 }
 
-func createIconsForFilePaths(filePaths : Array<String>)
+func createIconsForFilePaths(filePaths : Array<NSURL>)
 {
     var failedInputsCheck = false
     for filePath in filePaths
     {
-        if NSFileManager.defaultManager().fileExistsAtPath(filePath) == false {
+        if NSFileManager.defaultManager().fileExistsAtPath(filePath.path!) == false {
             failedInputsCheck = true
-            println("Issue: file does not exist \(filePath)")
+            print("Issue: file does not exist \(filePath)")
         }
         
-        if NSFileManager.defaultManager().fileExistsAtPath(outputDirPathForIconPath(filePath)) {
+        if NSFileManager.defaultManager().fileExistsAtPath(outputDirPathForIconPath(filePath).absoluteString) {
             failedInputsCheck = true
-            println("Issue: this directory exists and will be re-written \(outputDirPathForIconPath(filePath))")
+            print("Issue: this directory exists and will be re-written \(outputDirPathForIconPath(filePath))")
         }
     }
     

    var inputFilenames = Process.arguments
    inputFilenames.removeAtIndex(0)
    
    var filePathsArr = Array<String>()
    
    for filename in inputFilenames
    {
        if filename[0] == "/" {
            filePathsArr.append(filename)
            continue
        }
        
        if filename[0] == "~" {
            filePathsArr.append(filename.stringByExpandingTildeInPath)
            continue
        }
        

    
    for iconPath in filePaths
    {
        print("Processing file: \(iconPath)")
        
        let outputDir = outputDirPathForIconPath(iconPath)
        
        if !NSFileManager.defaultManager().fileExistsAtPath(outputDir.absoluteString) {
            
            do {
                try NSFileManager.defaultManager().createDirectoryAtPath(outputDir.path!, withIntermediateDirectories: false, attributes: [:])
            } catch {
                print("Error creating image folder")
            }
        }
        
        
        for resolution in iconResolutions
        {
            let targetIconPath = outputDir.URLByAppendingPathComponent("icon-\(resolution).png")
            do {
                try NSFileManager.defaultManager().copyItemAtPath(iconPath.path!, toPath: targetIconPath.path!)
            } catch {
                print("Error copying images")
            }
            
            
            scaleIcon(targetIconPath, resolution: resolution)
        }
    }
}

// =======
