# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# def replace_in_file(filename, old, new, **kwargs):
#     contents = open(filename).read()
#     contents = re.sub(old, new, contents, **kwargs)
#     open(filename, 'w').write(contents)


from ..core import run, replace_in_file
from pathlib import Path
import os
import sys
import os.path
import psutil
import logging

def help():
    return """install_boost <boost_ver> [<base_build_dir>] [<prefix>]'
    
boost_ver: minor version of boost which you want to install, e.g.: 58 will install boost1.58, etc.
base_build_dir: directory in which the source tarball will be downloaded and extracted [default: $HOME]
prefix: directory in which the resulting binaries will be installed [default: $HOME]
"""


def run_command(boost_ver=60, base_build_dir=None, prefix=None):
    # note: do not use default args in the function definition as os.environ['HOME'] is
    #       not defined when running from a non-shell session (eg: supervisord) and it
    #       makes the file un-importable
    if sys.version_info < (3, 5):  # FIXME: remove when we require python 3.5
        base_build_dir = os.path.expanduser(base_build_dir or os.environ['HOME'])
        prefix = os.path.expanduser(prefix or os.environ['HOME'])
    else:
        base_build_dir = Path(base_build_dir or os.environ['HOME']).expanduser()
        prefix = Path(prefix or os.environ['HOME']).expanduser()

    log.info('Downloading boost version 1.{} into: {}'.format(boost_ver, base_build_dir))
    os.chdir(str(base_build_dir))
    if not Path('boost_1_{}_0.tar.gz'.format(boost_ver)).exists():
        run('wget http://downloads.sourceforge.net/project/boost/boost/1.{boost_ver}.0/boost_1_{boost_ver}_0.tar.gz'
            .format(boost_ver=boost_ver))
    else:
        log.info('Not downloading it, file is already present')

    log.info('Decompressing source tarball')

# Path: bts_tools/core.py
# def run(cmd, capture_io=False, verbose=True, log_on_fail=True,
#         run_dir=None, forward_signals=False, pidfile=None, shell=False):
#     r = _run(cmd, capture_io=capture_io, verbose=verbose,
#              run_dir=run_dir, forward_signals=forward_signals, pidfile=pidfile, shell=shell)
#     if r.status != 0:
#         if log_on_fail:
#             log.warning('Failed running: %s' % cmd)
#             if capture_io:
#                 log.warning('\nSTDOUT:\n{}\nSTDERR:\n:{}'.format(r.stdout, r.stderr))
#         raise RuntimeError('Failed running: %s' % cmd)
#     return r
# 
# def replace_in_file(filename, old, new, **kwargs):
#     contents = open(filename).read()
#     contents = re.sub(old, new, contents, **kwargs)
#     open(filename, 'w').write(contents)


# Path: bts_tools/commands/install_boost.py
from ..core import run, replace_in_file
from pathlib import Path
import os
import sys
import os.path
import psutil
import logging


def help():
    return """install_boost <boost_ver> [<base_build_dir>] [<prefix>]'
    
boost_ver: minor version of boost which you want to install, e.g.: 58 will install boost1.58, etc.
base_build_dir: directory in which the source tarball will be downloaded and extracted [default: $HOME]
prefix: directory in which the resulting binaries will be installed [default: $HOME]
"""


def run_command(boost_ver=60, base_build_dir=None, prefix=None):
    # note: do not use default args in the function definition as os.environ['HOME'] is
    #       not defined when running from a non-shell session (eg: supervisord) and it
    #       makes the file un-importable
    if sys.version_info < (3, 5):  # FIXME: remove when we require python 3.5
        base_build_dir = os.path.expanduser(base_build_dir or os.environ['HOME'])
        prefix = os.path.expanduser(prefix or os.environ['HOME'])
    else:
        base_build_dir = Path(base_build_dir or os.environ['HOME']).expanduser()
        prefix = Path(prefix or os.environ['HOME']).expanduser()

    log.info('Downloading boost version 1.{} into: {}'.format(boost_ver, base_build_dir))
    os.chdir(str(base_build_dir))
    if not Path('boost_1_{}_0.tar.gz'.format(boost_ver)).exists():
        run('wget http://downloads.sourceforge.net/project/boost/boost/1.{boost_ver}.0/boost_1_{boost_ver}_0.tar.gz'
            .format(boost_ver=boost_ver))
    else:
        log.info('Not downloading it, file is already present')

    log.info('Decompressing source tarball')

    run('tar xf boost_1_{}_0.tar.gz'.format(boost_ver))