# Copyright 2016 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.

import("//remoting/build/config/remoting_build.gni")
import("//remoting/host/installer/win/generate_clsids.gni")
import("//build/toolchain/win/midl.gni")
import("//build/win/message_compiler.gni")

action("remoting_me2me_host_archive") {
  script = "//remoting/host/installer/build-installer-archive.py"

  deps = [
    "//remoting/host:remoting_native_messaging_manifests",
    "//remoting/host:remoting_start_host",
    "//remoting/host/installer:credits",
    "//remoting/host/it2me:remote_assistance_host",
    "//remoting/host/it2me:remote_assistance_host_uiaccess",
    "//remoting/host/win:remoting_core",
    "//remoting/host/win:remoting_desktop",
    "//remoting/host/win:remoting_me2me_host",
    "//remoting/host/win:remoting_native_messaging_host",
    "//third_party/icu:icudata",
  ]

  public_deps = [
    "//remoting/host/security_key:remote_security_key",
  ]

  _output = "$root_out_dir/remoting-me2me-host-$target_os.zip"

  outputs = [
    _output,
  ]
  if (is_chrome_branded) {
    _branding = "Chrome"
  } else {
    _branding = "Chromium"
  }

  if (is_official_build) {
    _official_build = "1"
  } else {
    _official_build = "0"
  }

  # Due to GN build issue http://crbug.com/633650, we need to actively set
  # inputs parameter to tell GN to depend on these files. So change to each
  # following files will trigger this target to be rebuilt.
  # TODO(zijiehe): Remove inputs parameter once bug 633650 has been
  # addressed.
  inputs = [
    "$root_out_dir/remote_assistance_host.exe",
    "$root_out_dir/remote_assistance_host_uiaccess.exe",
    "$root_out_dir/remote_security_key.exe",
    "$root_out_dir/remoting_core.dll",
    "$root_out_dir/remoting_desktop.exe",
    "$root_out_dir/remoting_host.exe",
    "$root_out_dir/remoting_native_messaging_host.exe",
    "$root_out_dir/remoting_start_host.exe",
    "$root_gen_dir/remoting/CREDITS.txt",
    "$root_out_dir/remoting/com.google.chrome.remote_assistance.json",
    "$root_out_dir/remoting/com.google.chrome.remote_desktop.json",
    "$root_out_dir/icudtl.dat",
  ]

  _generated_files = rebase_path(inputs, root_build_dir)
  _generated_files += [ rebase_path("//remoting/resources/chromoting.ico") ]

  # _generated_dst_files must contain the same files in the same order as
  # _generated_files, otherwise the Windows MSI will not be built correctly.
  _generated_dst_files = [
    "files/remote_assistance_host.exe",
    "files/remote_assistance_host_uiaccess.exe",
    "files/remote_security_key.exe",
    "files/remoting_core.dll",
    "files/remoting_desktop.exe",
    "files/remoting_host.exe",
    "files/remoting_native_messaging_host.exe",
    "files/remoting_start_host.exe",
    "files/CREDITS.txt",
    "files/com.google.chrome.remote_assistance.json",
    "files/com.google.chrome.remote_desktop.json",
    "files/icudtl.dat",
    "files/chromoting.ico",
  ]

  args = [
           rebase_path("$root_gen_dir/remoting_installation", root_build_dir),
           rebase_path(_output, root_build_dir),
           "--source-file-roots",
           rebase_path("//remoting/host/installer/win"),
           "--source-files",
           rebase_path("//remoting/host/installer/win/chromoting.wxs"),
           rebase_path("//remoting/host/installer/win/parameters.json"),

           # Input files
           "--generated-files",
         ] + _generated_files +
         [
           # Position of files in zip file
           "--generated-files-dst",
         ] + _generated_dst_files +
         [
           # Defs
           "--defs",
           "BRANDING=$_branding",
           "DAEMON_CONTROLLER_CLSID={$daemon_controller_clsid}",
           "RDP_DESKTOP_SESSION_CLSID={$rdp_desktop_session_clsid}",
           "VERSION=$chrome_version_full",
           "OFFICIAL_BUILD=$_official_build",
         ]
}

# We do not release a 64 bits binary. So to avoid any potential
# misunderstanding, we only build 32 bits MSI file.
if (is_chrome_branded && !is_component_build && target_cpu == "x86") {
  # The script uses "ia32" instead of "x86".
  msi_script_arch = "ia32"

  action("remoting_host_installation") {
    deps = [
      "//remoting/host:remoting_me2me_host_archive",
    ]
    script = "//remoting/tools/zip2msi.py"
    outputs = [
      "$root_out_dir/chromoting.msi",
    ]
    args = [
      "--wix_path",
      rebase_path("//third_party/wix"),
      "--intermediate_dir",
      rebase_path("$root_gen_dir/remoting_installation", root_build_dir),
      "--target_arch",
      msi_script_arch,
      rebase_path("$root_out_dir/remoting-me2me-host-$current_os.zip",
                  root_build_dir),
      rebase_path(outputs[0], root_build_dir),
    ]
  }
} else {
  group("remoting_host_installation") {
    # We still want to build host archive under other configurations.
    deps = [
      "//remoting/host:remoting_me2me_host_archive",
    ]
  }
}
