# vim: set ts=2 sw=2 tw=99 noet ft=python: 
import os, sys

UPLOAD_SCRIPT = os.path.join(Extension.sm_root, 'tools', 'buildbot', 'upload_symbols.py')

if 'BREAKPAD_SYMBOL_SERVER' in os.environ:
  symbolServer = os.environ['BREAKPAD_SYMBOL_SERVER']
  builder.SetBuildFolder('breakpad-symbols')
  
  for cxx_task in Extension.extensions:
    if cxx_task.target.platform in ['windows']:
      debug_entry = cxx_task.debug
    else:
      debug_entry = cxx_task.binary

    debug_file = os.path.join(builder.buildPath, debug_entry.path)
    if cxx_task.target.platform == 'linux':
      argv = ['dump_syms', debug_file, os.path.dirname(debug_file)]
    elif cxx_task.target.platform == 'mac':
      # Required once dump_syms is updated on the slaves.
      #argv = ['dump_syms', '-g', debug_file + '.dSYM', debug_file]
      argv = ['dump_syms', debug_file + '.dSYM']
    elif cxx_task.target.platform == 'windows':
      argv = ['dump_syms.exe', debug_file]

    plat_dir = os.path.dirname(debug_file)
    bin_dir = os.path.split(plat_dir)[0]

    symbol_file = '{}-{}-{}.breakpad'.format(
              os.path.split(bin_dir)[1],
              cxx_task.target.platform,
              cxx_task.target.arch)

    argv = [sys.executable, UPLOAD_SCRIPT, symbol_file] + argv
    builder.AddCommand(
      inputs = [UPLOAD_SCRIPT, debug_entry],
      argv = argv,
      outputs = [symbol_file]
    )