#!/usr/bin/env python3

# Copyright 2017 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import hashlib
import os
import shutil
import stat
import sys
import platform

if sys.version_info[0] < 3:
  from urllib2 import urlopen
else:
  from urllib.request import urlopen

os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))

def fetch(target):
  deps_path = 'buildtools/DEPS'
  try:
    with open(deps_path, 'r') as contents:
      deps_globals = {}
      exec(contents.read(), deps_globals)
      deps = deps_globals['deps']
  except:
    print(deps_path, 'could not be read/parsed. Did you run `tools/git-sync-deps`?')
    exit(1)

  if 'arm' in platform.processor():
      if 'darwin' in sys.platform:
        obj = 'mac_arm64-format'
        target_path = 'buildtools/mac_arm64/'
      else:
        print('unsupported platform')
        exit(1)
  else:
      if 'linux' in sys.platform:
        obj = 'linux64-format'
        target_path = 'buildtools/linux64/'
      elif 'darwin' in sys.platform:
        obj = 'mac-format'
        target_path = 'buildtools/mac/clang-format'
      else:
        obj = 'win-format'
        target_path = 'buildtools\\win\\'

  sha256sum = deps[obj]['objects'][0]['sha256sum']
  object_name = deps[obj]['objects'][0]['object_name']
  output_file = os.path.join('bin', deps[obj]['objects'][0]['output_file'])
  target_path = os.path.join(target_path, deps[obj]['objects'][0]['output_file'])

  def sha256_of_file(path):
    h = hashlib.sha256()
    if os.path.isfile(path):
      with open(path, 'rb') as f:
        h.update(f.read())
    return h.hexdigest()

  if sha256_of_file(output_file) != sha256sum:
    with open(output_file, 'wb') as f:
      url = 'https://chromium-%s.storage-download.googleapis.com/%s' % (target, object_name)
      f.write(urlopen(url).read())

    os.chmod(output_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
                          stat.S_IRGRP                | stat.S_IXGRP |
                          stat.S_IROTH                | stat.S_IXOTH )
    if sha256_of_file(output_file) != sha256sum:
      print(output_file, 'was corrupted during download. Please try again.')
      exit(1)

  if sha256_of_file(target_path) != sha256sum:
    shutil.copy(output_file, target_path)

fetch('clang-format')
