#!/usr/bin/env python

import os
import shutil
import subprocess
import sys

import lib.util as util


SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


def main():
  os.chdir(SOURCE_ROOT)
  result = (subprocess.call(['git', 'submodule', 'sync']) or
            subprocess.call(['git', 'submodule', 'update',
                             '--init', '--recursive']))
  if result:
    return result

  # Setup boto.
  with util.scoped_cwd(os.path.join('vendor', 'boto')):
    subprocess.call([sys.executable, 'setup.py', 'build'])
    # On windows python assumes all script names end with .py, if we don't
    # do so some modules like multiprocessing would break.
    shutil.copy(os.path.join('bin', 's3put'), os.path.join('bin', 's3put.py'))


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