 let fabricConfig     = "config/fabric.json"
 let gaConfig         = "config/google_analytics.json"
 
-let args = NSProcessInfo.processInfo().arguments as [String]
+let args = ProcessInfo.processInfo.arguments as [String]
 var _task   = args[args.count - 2]
 var _target = args[args.count - 1]
 if _task == "--" {


func nextVersion() {
    print("-------- increment versions --------")
    guard let currentVersion = Int(shell("agvtool what-version -terse").trim()) else {
      print("Failed to get currentVersion")
      return
    }
    run("agvtool next-version -all", silent: true)
    print("update bundle version: \(currentVersion) -> \(currentVersion + 1)")
    print("-------- update version strings  --------")
    run("agvtool new-marketing-version \(version)", silent: true)
}

func main(task task: Task, target: Target) {
    switch task {
    case .Config:  target.config()
    case .Prepare: target.prepare()

    var appfileDst:          String { return "fastlane/Appfile" }
    func config() {
        print("------ setup config files for \(self) --------")
        let _ = shell(cp, args: "\(feedlyConfig)"    , "\(feedlyConfigDst)")
        let _ = shell(cp, args: "\(youtubeConfig)"   , "\(youtubeConfigDst)")
        let _ = shell(cp, args: "\(soundCloudConfig)", "\(soundCloudConfigDst)")
        let _ = shell(cp, args: "\(fabricConfig)"    , "\(fabricConfigDst)")
        let _ = shell(cp, args: "\(deliverfile)"     , "\(deliverfileDst)")
        let _ = shell(cp, args: "\(appfile)"         , "\(appfileDst)")
    }
    func prepare() {
        install_lib()
    }
    func clean() {
        let _ = shell(git, args: "checkout", "HEAD", "\(feedlyConfigDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(youtubeConfigDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(soundCloudConfigDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(fabricConfigDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(gaConfigDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(deliverfileDst)")
        let _ = shell(git, args: "checkout", "HEAD", "\(appfileDst)")
    }
    func build() {
        let _ = shell(xcodebuild, args: "-workspace", "MusicFav.xcworkspace", "-scheme", "MusicFav", "archive", "-archivePath", "archives/MusicFav-`date +%Y%m%d%H%M`")
    }
    func test() {
        let _ = shell(xcodebuild, args: "-workspace", "MusicFav.xcworkspace", "-scheme", "UnitTests", "test")
    }
    func install_lib() {
        let _ = shell("bundle", args: "install")
        let _ = shell("bundle", args: "exec", "pod", "install")
        switch self {
        case .Production:
          let _ = shell(carthage, args: "checkout", "--use-submodules", "--use-ssh", "--no-use-binaries")
          let _ = shell(carthage, args: "build", "--platform", "iOS", "--configuration", "Release")
        case .Sandbox:
          let _ = shell(carthage, args: "checkout", "--use-submodules", "--use-ssh", "--no-use-binaries")
          let _ = shell(carthage, args: "build", "--platform", "iOS", "--configuration", "Debug")
        }
    }
}

func shell(_ command : String, args : String...) -> String {
    var output: [String] = []
    
    let task = Process()
    task.launchPath = command
    task.arguments = args
    
    let outpipe = Pipe()
    task.standardOutput = outpipe
    let errpipe = Pipe()
    task.standardError = errpipe
    
    task.launch()
    
    let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String(data: outdata, encoding: .utf8) {
        string = string.trimmingCharacters(in: .newlines)
        output = string.components(separatedBy: "\n")
    }
    
    let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String(data: errdata, encoding: .utf8) {
        string = string.trimmingCharacters(in: .newlines)
    }
    
    task.waitUntilExit()
    let _ = task.terminationStatus
    
    return output[0]
}

func showUsage() {
