# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

ios_app_script = "//build/config/ios/ios_app.py"

template("code_sign_ios") {
  assert(defined(invoker.entitlements_path),
         "The path to the entitlements .xcent file")
  assert(defined(invoker.identity),
         "The code signing identity")
  assert(defined(invoker.application_path),
         "The application to code sign")
  assert(defined(invoker.deps))

  action(target_name) {
    sources = [
      invoker.entitlements_path,
    ]

    _application_path = invoker.application_path

    script = ios_app_script

    outputs = [
      "$_application_path/_CodeSignature/CodeResources"
    ]

    args = [
      "codesign",
      "-p",
      rebase_path(invoker.application_path, root_build_dir),
      "-i",
      invoker.identity,
      "-e",
      rebase_path(invoker.entitlements_path, root_build_dir),
    ]

    deps = invoker.deps
  }
}

template("xcode_harness_ios") {
  assert(defined(invoker.deps),
         "The dependencies must be specified")
  assert(defined(invoker.app_bundle),
         "The app bundle must be defined")
  assert(defined(invoker.app_name),
         "The application name must be defined")
  app_name = invoker.app_name

  xcode_project_gen_target_name = app_name + "_xcode"
  copy(xcode_project_gen_target_name) {
    sources = [
      "//build/config/ios/XcodeHarness/FakeMain.m",
      "//build/config/ios/XcodeHarness/Harness.xcodeproj",
    ]
    outputs = [
      "$root_build_dir/$xcode_project_gen_target_name/{{source_file_part}}",
    ]
  }

  bundle_copy_gen_target_name = app_name + "_bundle_copy"
  copy(bundle_copy_gen_target_name) {
    sources = [
      invoker.app_bundle
    ]
    outputs = [
      "$root_build_dir/$xcode_project_gen_target_name/Application",
    ]

    deps = invoker.deps
  }

  group(target_name) {
    deps = [
      ":$xcode_project_gen_target_name",
      ":$bundle_copy_gen_target_name",
    ]
  }
}

template("resource_copy_ios") {
  assert(defined(invoker.resources),
         "The source list of resources to copy over")
  assert(defined(invoker.bundle_directory),
         "The directory within the bundle to place the sources in")
  assert(defined(invoker.app_name),
         "The name of the application")

  _bundle_directory = invoker.bundle_directory
  _app_name = invoker.app_name
  _resources = invoker.resources

  copy(target_name) {
    set_sources_assignment_filter([])
    sources = _resources
    outputs = [ "$root_build_dir/$_app_name.app/$_bundle_directory/{{source_file_part}}" ]

    if (defined(invoker.deps)) {
      deps = invoker.deps
    }
  }
}

template("ios_app") {

  assert(defined(invoker.deps),
         "Dependencies must be specified for $target_name")
  assert(defined(invoker.info_plist),
         "The application plist file must be specified for $target_name")
  assert(defined(invoker.app_name),
         "The name of iOS application for $target_name")
  assert(defined(invoker.entitlements_path),
         "The entitlements path must be specified for $target_name")
  assert(defined(invoker.code_signing_identity),
         "The entitlements path must be specified for $target_name")

  # We just create a variable so we can use the same in interpolation
  app_name = invoker.app_name

  # Generate the project structure

  struct_gen_target_name = target_name + "_struct"

  action(struct_gen_target_name) {

    script = ios_app_script

    sources = []
    outputs = [ "$root_build_dir/$app_name.app" ]

    args = [
      "structure",
      "-d",
      rebase_path(root_build_dir),
      "-n",
      app_name
    ]

  }

  # Generate the executable

  bin_gen_target_name = target_name + "_bin"

  executable(bin_gen_target_name) {
    libs = [
      "AudioToolbox.framework",
      "AVFoundation.framework",
      "OpenGLES.framework",
      "QuartzCore.framework",
      "UIKit.framework",
    ]
    deps = invoker.deps
    output_name = app_name
  }

  # Process the Info.plist

  plist_gen_target_name = target_name + "_plist"

  action(plist_gen_target_name) {

    script = ios_app_script

    sources = [ invoker.info_plist ]
    outputs = [ "$root_build_dir/plist/$app_name/Info.plist" ]

    args = [
      "plist",
      "-i",
      rebase_path(invoker.info_plist, root_build_dir),
      "-o",
      rebase_path("$root_build_dir/plist/$app_name"),
    ]
  }

  # Copy the generated binaries and assets to their appropriate locations

  copy_gen_target_name = target_name + "_copy"
  copy(copy_gen_target_name) {
    sources = [
      "$root_build_dir/plist/$app_name/Info.plist",
      "$root_build_dir/$app_name",
    ]

    outputs = [
      "$root_build_dir/$app_name.app/{{source_file_part}}"
    ]

    deps = [
      ":$struct_gen_target_name",
      ":$bin_gen_target_name",
      ":$plist_gen_target_name",
    ]
  }

  # Generate the Xcode Harness for Profiling

  xcode_harness_gen_target_name = app_name + "_harness"
  xcode_harness_ios(xcode_harness_gen_target_name) {
    app_bundle = "$root_build_dir/$app_name.app"
    deps = [
      ":$bin_gen_target_name",
      ":$struct_gen_target_name",
      ":$copy_gen_target_name",
    ]
  }

  # Perform Code Signing

  code_sign_gen_target_name = target_name + "_codesign"
  code_sign_ios(code_sign_gen_target_name) {
    entitlements_path = invoker.entitlements_path
    identity = invoker.code_signing_identity
    application_path = "$root_build_dir/$app_name.app"
    deps = [ ":$copy_gen_target_name" ]
  }

  # Top level group

  group(target_name) {
    # Skip code signing if no identity is provided. This is useful for simulator
    # builds
    deps = [ ":$xcode_harness_gen_target_name" ]
    if (invoker.code_signing_identity == "") {
      deps += [ ":$copy_gen_target_name" ]
    } else {
      deps += [ ":$code_sign_gen_target_name" ]
    }
  }

}
