#!/usr/bin/env vpython
# Copyright 2017 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 argparse
import os
import re
import shutil
import subprocess
import sys
import tempfile
import urllib2


SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_DESTINATION_DIR = os.path.join(SCRIPT_DIR, 'webrtc_cases')

WEBRTC_GITHUB_URL = 'https://github.com/webrtc/'
TEST_PAGES_LOCATION_BY_REPO = {
    'test-pages': {
        'dirs': [
            'src/canvas-capture',
            'src/codec_constraints',
            'src/multiple-peerconnections',
            'src/pause-play',
        ],
        'revision': '9f0ff9343a38dbf16199a964a18d7411d940c601',
    },
    'samples': {
        'dirs': [
            'src/content/datachannel/datatransfer',
            'src/content/getusermedia/resolution',
            'src/content/peerconnection/audio',
        ],
        'files': [
            'src/js/common.js',
        ],
        'revision': '6f9a14c10ee9b990d56c309d86d4b9129a4aa626',
    },
    'adapter': {
        'files': [
            'release/adapter.js',
        ],
        'revision': '5b7ce4bdce79b9cb754fe27b097106e9491e0354',
    },
}

ADDED_SCRIPT_TAGS = (
    '<script src="%s.js"></script>\n'
    '<script src="adapter.js"></script>\n'
    '<script src="common.js"></script>\n'
    '</body></html>'
)

COPYRIGHT_NOTICE = [
    'Copyright 2017 The Chromium Authors. All rights reserved.\n',
    'Use of this source code is governed by a BSD-style license that can be\n',
    'found in the LICENSE file.\n',
]

COPYRIGHT_NOTICE_LENGTH = 8
JS_COPYRIGHT_NOTICE = ' * '.join(['/*\n'] + COPYRIGHT_NOTICE) + ' */\n'
HTML_COPYRIGHT_NOTICE = ' * '.join(['<!--\n'] + COPYRIGHT_NOTICE) + '-->\n'

STRIPPED_TAGS_RE = ('( *<meta.*?>\n?| *<link.*?>\n?|'
                    ' *<script.*>.*?</script>\n?| *</body>.*?</html>)')


class TemporaryDirectory(object):
  def __init__(self):
    self._closed = False
    self._name = None
    self._name = tempfile.mkdtemp()
  def __enter__(self):
    return self._name
  def __exit__(self, exc, value, tb):
    if self._name and not self._closed:
      shutil.rmtree(self._name)
      self._closed = True


def CopyJSFile(origin, destination, has_copyright=True):
  contents = []
  with open(origin) as input_file:
    contents = input_file.readlines()

  if has_copyright:
    contents = contents[COPYRIGHT_NOTICE_LENGTH:]
  contents = [JS_COPYRIGHT_NOTICE] + contents

  with open(destination, 'w') as output_file:
    output_file.writelines(contents)


def CopyHTMLFile(test_name, origin, destination):
  contents = ''
  with open(origin) as input_file:
    contents = input_file.read()

  contents = re.sub(STRIPPED_TAGS_RE, '', contents,
                    flags=re.MULTILINE|re.DOTALL)
  contents += ADDED_SCRIPT_TAGS % test_name

  contents = [line + '\n' for line in contents.split('\n')]
  contents = (contents[:1] + [HTML_COPYRIGHT_NOTICE] +
              contents[COPYRIGHT_NOTICE_LENGTH:])

  with open(destination, 'w') as output_file:
    output_file.writelines(contents)


def main():
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      description=(
          'Update the WebRTC test pages.\n'
          'This script downloads the test pages from the WebRTC GitHub '
          'repository and copies them to the DESTINATION directory after '
          'processing them as follows: \n'
          '  * Adds a copyright notice on top of the HTML and JS files.\n'
          '  * Deletes the <meta> tags.\n'
          '  * Discards the CSS files and corresponding link tags.\n'
          '  * Discards the JS files and corresponding script tags except for '
          'main.js, adapter.js and common.js.\n'
          '  * Renames the index.html and main.js files for each test to '
          'testname.html and testname.js.'))

  parser.add_argument('-d', '--destination', default=DEFAULT_DESTINATION_DIR,
                      type=str, help='Where to save the WebRTC test pages.')

  args = parser.parse_args()

  if not os.path.isdir(args.destination):
    os.makedirs(args.destination)

  with TemporaryDirectory() as temp_dir:
    for repo_name, repo_info in TEST_PAGES_LOCATION_BY_REPO.items():
      p = subprocess.Popen(['git', 'clone', WEBRTC_GITHUB_URL + repo_name],
                           cwd=temp_dir)
      p.wait()

      repo_dir = os.path.join(temp_dir, repo_name)
      p = subprocess.Popen(['git', 'checkout', repo_info.get('revision')],
                           cwd=repo_dir)
      p.wait()

      for test_dir in repo_info.get('dirs', []):
        test_dir = os.path.join(repo_dir, test_dir)
        test_name = os.path.basename(test_dir)

        CopyJSFile(os.path.join(test_dir, 'js', 'main.js'),
                   os.path.join(args.destination, test_name + '.js'))
        CopyHTMLFile(test_name, os.path.join(test_dir, 'index.html'),
                     os.path.join(args.destination, test_name + '.html'))

      for test_file in repo_info.get('files', []):
        file_name = os.path.basename(test_file)
        CopyJSFile(os.path.join(temp_dir, repo_name, test_file),
                   os.path.join(args.destination, file_name), False)


if __name__ == '__main__':
  main()
