#!/usr/bin/env python

import os
import shutil
import subprocess

script_dir = os.path.dirname(os.path.realpath(__file__))
chromium_dir = os.path.abspath(os.path.join(script_dir, os.pardir, 'chromium'))

overrides_dir = os.path.abspath(os.path.join(script_dir, 'overrides'))
patches_dir = os.path.abspath(os.path.join(script_dir, 'patches'))

def Override(path):
  """Override chromium's original file with the file located at path."""
  dst_path = os.path.join(chromium_dir, os.path.relpath(path, overrides_dir))
  dst_path = os.path.abspath(dst_path)

  if os.path.isdir(path):
    if not os.path.exists(dst_path):
      os.mkdir(dst_path)
    for item in os.listdir(path):
      # Recursive override
      Override(os.path.join(path, item))

  else:
    shutil.copy(path, dst_path)
    print 'override', dst_path

def Patch(path):
  """Apply a patch aginst chromimu's origin source tree."""
  if os.path.isdir(path):
    for item in os.listdir(path):
      # Recursive override
      Patch(os.path.join(path, item))

  else:
    # Relative patch path.
    relpath = os.path.relpath(path, chromium_dir)
    subprocess.call(['patch', '-f', '-d', chromium_dir, '-p0', '-i', relpath])

Override(overrides_dir)
Patch(patches_dir)

@@ -1178,16 +1178,242 @@
 path%5D)%0A%0A
+def CreateSym():%0A  %22%22%22Create chromium shortcut. Chromium's source tree must live in src dir.%22%22%22%0A  shortpath = os.path.join(chromium_dir, 'chrome')%0A  if not os.path.exists(shortpath):%0A    os.symlink(chromium_dir, shortpath)%0A  %0A
 Override
@@ -1439,16 +1439,29 @@
 ch(patches_dir)%0A
+CreateSym()%0A%0A

Add CreateSym() to create shortcut.