#!/usr/bin/env python

import argparse
import errno
import os
import subprocess
import sys


SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
VENDOR_DIR = os.path.join(SOURCE_ROOT, 'vendor')
DOWNLOAD_DIR = os.path.join(VENDOR_DIR, 'download')


def main():
  args = parse_args()

  update_submodules()
  download_libchromiumcontent(args.dev, args.commit, args.target_arch, args.url)


def parse_args():
  parser = argparse.ArgumentParser(description='Bootstrap this project')
  parser.add_argument('-c', '--commit', required=True,
                      help='The commit of libchromiumcontent to download.')
  parser.add_argument('-d', '--dev', action='store_true',
                      help='Do not download static_library build')
  parser.add_argument('--target_arch', required=True,
                      help='The arch of libchromiumcontent to download.')
  parser.add_argument('url', help='The base URL from which to download '
                      'libchromiumcontent (i.e., the URL you passed to '
                      'libchromiumcontent\'s script/upload script')
  return parser.parse_args()


def update_submodules():
  return (subprocess.call(['git', 'submodule', 'sync', '--quiet']) or
          subprocess.call(['git', 'submodule', 'update', '--init',
                           '--recursive']))


def download_libchromiumcontent(is_dev, commit, target_arch, url):
  mkdir_p(DOWNLOAD_DIR)
  download = os.path.join(VENDOR_DIR, 'libchromiumcontent', 'script',
                          'download')
  target_dir = os.path.join(DOWNLOAD_DIR, 'libchromiumcontent')
  args = ['-f', '-c', commit, '--target_arch', target_arch, url, target_dir]
  if is_dev:
    subprocess.check_call([sys.executable, download] + args)
  else:
    subprocess.check_call([sys.executable, download, '-s'] + args)


def mkdir_p(path):
  try:
    os.makedirs(path)
  except OSError as e:
    if e.errno != errno.EEXIST:
      raise


if __name__ == '__main__':
  sys.exit(main())