# Path: fabgis/system.py
# @task
# def setup_ccache():
#     """Setup ccache."""
#     fabtools.require.deb.package('ccache')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/gcc')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/g++')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/cc')
# 
# def get_processor_count():
#     return run('cat /proc/cpuinfo | grep ^processor | wc -l')
# 
# Path: fabgis/common.py
# def setup_env():
#     """Things to do regardless of whether command is local or remote.
# 
#     Todo - many of these env.fg settings should be module specific...
# 
#     """
#     if env.fg is not None:
#         fastprint('Environment already set!\n')
#         return
# 
#     fastprint('Setting environment!\n')
#     env.fg = fdict()
#     with hide('output'):
#         env.fg.user = run('whoami')
#         # Workaround for
#         env.fg.hostname = run('hostname')
#         # this which fails in docker - see
#         # https://github.com/dotcloud/docker/issues/1301
#         #env.fg.hostname = fabtools.system.get_hostname()
#         env.fg.home = os.path.join('/home/', env.fg.user)
#         env.fg.workspace = os.path.join(env.fg.home, 'dev')
#         env.fg.inasafe_git_url = 'git://github.com/AIFDR/inasafe.git'
#         env.fg.qgis_git_url = 'git://github.com/qgis/QGIS.git'
#         env.fg.kandan_git_url = 'git://github.com/kandanapp/kandan.git'
#         env.fg.gdal_svn_url = 'https://svn.osgeo.org/gdal/trunk/gdal'
#         env.fg.inasafe_checkout_alias = 'inasafe-fabric'
#         env.fg.qgis_checkout_alias = 'qgis-fabric'
#         env.fg.inasafe_code_path = os.path.join(
#             env.fg.workspace, env.fg.inasafe_checkout_alias)
#         env.fg.qgis_code_path = os.path.join(
#             env.fg.workspace, env.fg.qgis_checkout_alias)
# 
# Path: fabgis/utilities.py
# def append_if_not_present(filename, text, use_sudo=False):
#     """Append to a file if an equivalent line is not already there.
#     :param filename: Name of file to append to.
#     :type filename: str
# 
#     :param text: Text to append.
#     :type text: str
# 
#     :param use_sudo: Run the command as sudo
#     :type use_sudo: bool
#     """
#     if not contains(filename, text):
#         append(filename, text, use_sudo=use_sudo)


import fabtools
from fabric.contrib.files import exists
from fabric.api import fastprint, run, cd, env, task, sudo, settings
from .system import setup_ccache, get_processor_count
from .common import setup_env
from .utilities import append_if_not_present




@task
def build_proj4(version='4.8.0'):
    """Get proj4 from tarball and build it.

    :param version: Proj version to build. The version should be consistent
        with a downloadable tar file from the project web site. Default is
        the current stable release.
    :type version: str
    """
    setup_env()
    fabtools.require.deb.package('build-essential')
    setup_ccache()

    code_base = '%s/cpp' % env.fg.workspace
    filename = 'proj-%s' % version
    source_url = 'http://download.osgeo.org/proj/%s.tar.gz' % filename
    code_path = '%s/%s' % (code_base, filename)

    if not exists(code_path):
        fastprint('Extracted tarball does not exist, creating.')
        with cd(code_base):
            if exists('%s.tar.gz' % filename):
                run('rm %s.tar.gz' % filename)
            run('wget %s' % source_url)
            run('tar xfz %s.tar.gz' % filename)


# Path: fabgis/system.py
# @task
# def setup_ccache():
#     """Setup ccache."""
#     fabtools.require.deb.package('ccache')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/gcc')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/g++')
#     sudo('ln -fs /usr/bin/ccache /usr/local/bin/cc')
# 
# def get_processor_count():
#     return run('cat /proc/cpuinfo | grep ^processor | wc -l')
# 
# Path: fabgis/common.py
# def setup_env():
#     """Things to do regardless of whether command is local or remote.
# 
#     Todo - many of these env.fg settings should be module specific...
# 
#     """
#     if env.fg is not None:
#         fastprint('Environment already set!\n')
#         return
# 
#     fastprint('Setting environment!\n')
#     env.fg = fdict()
#     with hide('output'):
#         env.fg.user = run('whoami')
#         # Workaround for
#         env.fg.hostname = run('hostname')
#         # this which fails in docker - see
#         # https://github.com/dotcloud/docker/issues/1301
#         #env.fg.hostname = fabtools.system.get_hostname()
#         env.fg.home = os.path.join('/home/', env.fg.user)
#         env.fg.workspace = os.path.join(env.fg.home, 'dev')
#         env.fg.inasafe_git_url = 'git://github.com/AIFDR/inasafe.git'
#         env.fg.qgis_git_url = 'git://github.com/qgis/QGIS.git'
#         env.fg.kandan_git_url = 'git://github.com/kandanapp/kandan.git'
#         env.fg.gdal_svn_url = 'https://svn.osgeo.org/gdal/trunk/gdal'
#         env.fg.inasafe_checkout_alias = 'inasafe-fabric'
#         env.fg.qgis_checkout_alias = 'qgis-fabric'
#         env.fg.inasafe_code_path = os.path.join(
#             env.fg.workspace, env.fg.inasafe_checkout_alias)
#         env.fg.qgis_code_path = os.path.join(
#             env.fg.workspace, env.fg.qgis_checkout_alias)
# 
# Path: fabgis/utilities.py
# def append_if_not_present(filename, text, use_sudo=False):
#     """Append to a file if an equivalent line is not already there.
#     :param filename: Name of file to append to.
#     :type filename: str
# 
#     :param text: Text to append.
#     :type text: str
# 
#     :param use_sudo: Run the command as sudo
#     :type use_sudo: bool
#     """
#     if not contains(filename, text):
#         append(filename, text, use_sudo=use_sudo)


# Path: fabgis/proj4.py
import fabtools
from fabric.contrib.files import exists
from fabric.api import fastprint, run, cd, env, task, sudo, settings
from .system import setup_ccache, get_processor_count
from .common import setup_env
from .utilities import append_if_not_present





@task
def build_proj4(version='4.8.0'):
    """Get proj4 from tarball and build it.

    :param version: Proj version to build. The version should be consistent
        with a downloadable tar file from the project web site. Default is
        the current stable release.
    :type version: str
    """
    setup_env()
    fabtools.require.deb.package('build-essential')
    setup_ccache()

    code_base = '%s/cpp' % env.fg.workspace
    filename = 'proj-%s' % version
    source_url = 'http://download.osgeo.org/proj/%s.tar.gz' % filename
    code_path = '%s/%s' % (code_base, filename)

    if not exists(code_path):
        fastprint('Extracted tarball does not exist, creating.')
        with cd(code_base):
            if exists('%s.tar.gz' % filename):
                run('rm %s.tar.gz' % filename)
            run('wget %s' % source_url)
            run('tar xfz %s.tar.gz' % filename)


    processor_count = get_processor_count()