//
//  Path+AppSupport.swift
//  swiftgen
//
//  Created by Olivier HALLIGON on 11/10/2017.
//  Copyright © 2017 AliSoftware. All rights reserved.
//

import Foundation
import PathKit

extension Path {
  static let applicationSupport: Path = {
    let paths = NSSearchPathForDirectoriesInDomains(
      .applicationSupportDirectory,
      .userDomainMask,
      true
    )
    guard let path = paths.first else {
      fatalError("Unable to locate the Application Support directory on your machine!")
    }
    return Path(path)
  }()
}

let templatesRelativePath: String = {
  if let path = Bundle.main.object(forInfoDictionaryKey: "TemplatePath") as? String, !path.isEmpty {
    return path
  } else if let path = Bundle(for: BundleToken.self).path(forResource: "templates", ofType: nil) {
    return path
  } else {
    return "../templates"
  }
}()

private final class BundleToken {}

let appSupportTemplatesPath = Path.applicationSupport + "SwiftGen/templates"
let bundledTemplatesPath = Path(ProcessInfo.processInfo.arguments[0]).parent() + templatesRelativePath

//
//  Path+AppSupport.swift
//  swiftgen
//
//  Created by Olivier HALLIGON on 11/10/2017.
//  Copyright © 2017 AliSoftware. All rights reserved.
//

import Foundation
import PathKit

extension Path {
  static let applicationSupport: Path = {
    let paths = NSSearchPathForDirectoriesInDomains(
      .applicationSupportDirectory,
      .userDomainMask,
      true
    )
    guard let path = paths.first else {
      fatalError("Unable to locate the Application Support directory on your machine!")
    }
    return Path(path)
  }()
}

let templatesRelativePath: String = {
  if let path = Bundle.main.object(forInfoDictionaryKey: "TemplatePath") as? String, !path.isEmpty {
    return path
  } else if let path = Bundle(for: BundleToken.self).path(forResource: "templates", ofType: nil) {
    return path
  } else {
    return "../templates"
  }
}()

private final class BundleToken {}

let appSupportTemplatesPath = Path.applicationSupport + "SwiftGen/templates"
let binaryPath: Path = {
  var binaryPath = Path(ProcessInfo.processInfo.arguments[0])
  do {
    while binaryPath.isSymlink {
      binaryPath = try binaryPath.symlinkDestination()
    }
  } catch {
    print("Warning: could not resolve symlink of \(binaryPath) with error \(error)")
  }
  return binaryPath
}()
let bundledTemplatesPath = binaryPath.parent() + templatesRelativePath

Resolve binary concrete path if it is a symbolic link
Resolve binary concrete path if it is a symbolic link

Seems like there is an issue if we call swiftgen using a symbolic link.
The issue does not come from swiftgen implementation but seems to be an
Apple bug which does not return the same `arguments[0]` depending on
where you call the symbolic link from.
[This sample](https://github.com/Liquidsoul/apple-radars/tree/master/symlink-arg0)
shows the issue.
In my opinion, the value should always be the symbolic link path,
meaning that bundled templates resolution would not work. So, in order
to solve this, swiftgen needs to go to the concrete swiftgen path.
