#!/usr/bin/env python
# Copyright 2013 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.

"""
Replaces gyp files in tree with files from here that
make the build use system libraries.
"""


import os.path
import shutil
import sys


REPLACEMENTS = {
  'use_system_expat': 'third_party/expat/expat.gyp',
  'use_system_ffmpeg': 'third_party/ffmpeg/ffmpeg.gyp',
  'use_system_flac': 'third_party/flac/flac.gyp',
  'use_system_harfbuzz': 'third_party/harfbuzz-ng/harfbuzz.gyp',
  'use_system_icu': 'third_party/icu/icu.gyp',
  'use_system_jsoncpp': 'third_party/jsoncpp/jsoncpp.gyp',
  'use_system_libevent': 'third_party/libevent/libevent.gyp',
  'use_system_libjpeg': 'third_party/libjpeg/libjpeg.gyp',
  'use_system_libpng': 'third_party/libpng/libpng.gyp',
  'use_system_libusb': 'third_party/libusb/libusb.gyp',
  'use_system_libvpx': 'third_party/libvpx/libvpx.gyp',
  'use_system_libwebp': 'third_party/libwebp/libwebp.gyp',
  'use_system_libxml': 'third_party/libxml/libxml.gyp',
  'use_system_libxslt': 'third_party/libxslt/libxslt.gyp',
  'use_system_opus': 'third_party/opus/opus.gyp',
  'use_system_re2': 'third_party/re2/re2.gyp',
  'use_system_snappy': 'third_party/snappy/snappy.gyp',
  'use_system_speex': 'third_party/speex/speex.gyp',
  'use_system_sqlite': 'third_party/sqlite/sqlite.gyp',
  'use_system_v8': 'v8/tools/gyp/v8.gyp',
  'use_system_zlib': 'third_party/zlib/zlib.gyp',
}


def DoMain(argv):
  my_dirname = os.path.dirname(__file__)
  source_tree_root = os.path.abspath(
    os.path.join(my_dirname, '..', '..', '..'))

  for flag, path in REPLACEMENTS.items():
    # Accept arguments in gyp command-line syntax, and ignore other
    # parameters, so that the caller can re-use command-line for this
    # script and gyp.
    if '-D%s=1' % flag not in argv:
      continue

    # Copy the gyp file from directory of this script to target path.
    shutil.copyfile(os.path.join(my_dirname, os.path.basename(path)),
                    os.path.join(source_tree_root, path))

  return 0


if __name__ == '__main__':
  sys.exit(DoMain(sys.argv))

@@ -278,16 +278,32 @@
 .%0A%22%22%22%0A%0A%0A
+import optparse%0A
 import o
@@ -1656,50 +1656,42 @@
 %0A%0A  
-for flag, path in REPLACEMENTS.items():%0A  
+parser = optparse.OptionParser()%0A%0A
   # 
@@ -1738,43 +1738,8 @@
 tax,
- and ignore other%0A    # parameters,
  so 
@@ -1764,16 +1764,20 @@
 n re-use
+%0A  #
  command
@@ -1790,22 +1790,16 @@
 for this
-%0A    #
  script 
@@ -1806,16 +1806,213 @@
 and gyp.
+%0A  parser.add_option('-D', dest='defines', action='append')%0A%0A  parser.add_option('--undo', action='store_true')%0A%0A  options, args = parser.parse_args(argv)%0A%0A  for flag, path in REPLACEMENTS.items():
 %0A    if 
@@ -2012,18 +2012,16 @@
     if '
--D
 %25s=1' %25 
@@ -2036,34 +2036,499 @@
  in 
-argv:%0A      continue%0A%0A
+options.defines:%0A      continue%0A%0A    if options.undo:%0A      # Restore original file, and also remove the backup.%0A      # This is meant to restore the source tree to its original state.%0A      os.rename(os.path.join(source_tree_root, path + '.orig'),%0A                os.path.join(source_tree_root, path))%0A    else:%0A      # Create a backup copy for --undo.%0A      shutil.copyfile(os.path.join(source_tree_root, path),%0A                      os.path.join(source_tree_root, path + '.orig'))%0A%0A 
     
+ 
 # Co
@@ -2589,16 +2589,18 @@
 t path.%0A
+  
     shut
@@ -2661,16 +2661,18 @@
 path)),%0A
+  
         

make it possible to undo changes made by replace_gyp_files.py