 extension String {
     func trim() -> String
     {
-        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
+        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
     }
 }
 
-let version = "0.4.7"
+let git        = "/usr/local/bin/"
+let cp         = "/bin/cp"
+let xcodebuild = "/usr/bin/xcodebuild"
+let carthage   = "/usr/local/bin/carthage"
+let agvtool    = "/usr/bin/agvtool"
+let version    = "0.4.8"
 
 enum Task: String {
     case Config  = "config"

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

func shell(command: String) -> String {
    let fp = popen(command, "r")
    var buf = Array<CChar>(count: 128, repeatedValue: 0)
    var result = ""
    while fgets(&buf, CInt(buf.count), fp) != nil,
          let str = String.fromCString(buf) {
              result.appendContentsOf(str)
    }
    fclose(fp)
    return result
}

func run(command: String, silent: Bool = false) {
    if !silent { print(command) }
    system(command)
}

func showUsage() {


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

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