#!/usr/bin/env python
# Copyright (c) 2012 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.

"""Script to install ARM root image for cross building of ARM chrome on linux.
This script can be run manually but is more often run as part of gclient
hooks. When run from hooks this script should be a no-op on non-linux
platforms.

The sysroot image could be constructed from scratch based on the current
state or precise/arm but for consistency we currently use a pre-built root
image which was originally designed for building trusted NaCl code. The image
will normally need to be rebuilt every time chrome's build dependancies are
changed.

Steps to rebuild the arm sysroot image:

- cd $SRC/native_client
- ./tools/trusted_cross_toolchains/trusted-toolchain-creator.armel.precise.sh \
    UpdatePackageLists
- ./tools/trusted_cross_toolchains/trusted-toolchain-creator.armel.precise.sh \
    BuildJail $SRC/out/arm-sysroot.tar.gz
- gsutil cp -a public-read $SRC/out/arm-sysroot.tar.gz \
    nativeclient-archive2/toolchain/$NACL_REV/sysroot-arm-trusted.tgz
"""

# TODO(sbc): merge this script into:
#  chrome/installer/linux/sysroot_scripts/install-debian.wheezy.sysroot.py

import hashlib
import os
import shutil
import subprocess
import sys


SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
URL_PREFIX = 'https://storage.googleapis.com'
URL_PATH = 'chrome-linux-sysroot/toolchain'
REVISION = 285950
TARBALL = 'debian_wheezy_arm_sysroot.tgz'
TARBALL_SHA1SUM = 'fc2f54db168887c5190c4c6686c869bedf668b4e'


def get_sha1(filename):
  sha1 = hashlib.sha1()
  with open(filename, 'rb') as f:
    while True:
      # Read in 1mb chunks, so it doesn't all have to be loaded into memory.
      chunk = f.read(1024*1024)
      if not chunk:
        break
      sha1.update(chunk)
  return sha1.hexdigest()


def main(args):
  if '--linux-only' in args:
    # This argument is passed when run from the gclient hooks.
    # In this case we return early on non-linux platforms
    # or if GYP_DEFINES doesn't include target_arch=arm
    if not sys.platform.startswith('linux'):
      return 0

    if "target_arch=arm" not in os.environ.get('GYP_DEFINES', ''):
      return 0

  src_root = os.path.dirname(os.path.dirname(SCRIPT_DIR))
  sysroot = os.path.join(src_root, 'arm-sysroot')
  url = "%s/%s/%s/%s" % (URL_PREFIX, URL_PATH, REVISION, TARBALL)

  stamp = os.path.join(sysroot, ".stamp")
  if os.path.exists(stamp):
    with open(stamp) as s:
      if s.read() == url:
        print "ARM root image already up-to-date: %s" % sysroot
        return 0

  print "Installing ARM root image: %s" % sysroot
  if os.path.isdir(sysroot):
    shutil.rmtree(sysroot)
  os.mkdir(sysroot)
  tarball = os.path.join(sysroot, TARBALL)
  curl = ['curl', '--fail', '-L', url, '-o', tarball]
  if os.isatty(sys.stdout.fileno()):
    curl.append('--progress')
  else:
    curl.append('--silent')
  subprocess.check_call(curl)
  sha1sum = get_sha1(tarball)
  if sha1sum != TARBALL_SHA1SUM:
    print 'Tarball sha1sum is wrong.'
    print 'Expected %s, actual: %s' % (TARBALL_SHA1SUM, sha1sum)
    return 1
  subprocess.check_call(['tar', 'xf', tarball, '-C', sysroot])
  os.remove(tarball)

  with open(stamp, 'w') as s:
    s.write(url)
  return 0


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))