# Path: fabgis/common.py
# @task
# def add_ubuntugis_ppa():
#     """Ensure we have ubuntu-gis repo."""
#     fabtools.deb.update_index(quiet=True)
#     fabtools.require.deb.package('software-properties-common')
#     fabtools.require.deb.ppa(
#         #'ppa:ubuntugis/ubuntugis-unstable', auto_yes=True)
#         'ppa:ubuntugis/ubuntugis-unstable')
# 
# 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/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/proj4.py
# @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()
# 
#     with cd(code_path):
#         # Dont fail if make clean does not work
#         with settings(warn_only=True):
#             run('make clean')
#         run('./configure')
#         run('make -j %s' % processor_count)
#         sudo('make install')
#     # Write to ld path too so libs are loaded nicely
#     ld_file = '/etc/ld.so.conf.d/usr_local_lib.conf'
#     with settings(warn_only=True):
#         sudo('rm %s' % ld_file)
#     append_if_not_present(ld_file, '/usr/local/lib', use_sudo=True)
#     sudo('ldconfig')


import fabtools
from fabric.contrib.files import exists, append
from fabric.api import fastprint, run, cd, env, task, sudo, settings
from .common import add_ubuntugis_ppa, setup_env
from .system import setup_ccache, get_processor_count
from .proj4 import build_proj4
# coding=utf-8
"""
GDAL Related Tasks.
===================

Module for gdal related tasks"""



@task
def build_gdal(with_ecw=False, with_mrsid=False):
    """Clone or update GDAL from svn then build it.

    :param with_ecw: Whether to build with ecw support.
    :type with_ecw: bool

    :param with_mrsid: Whether to build with mrsid support.
    :type with_mrsid: bool

    """

# Path: fabgis/common.py
# @task
# def add_ubuntugis_ppa():
#     """Ensure we have ubuntu-gis repo."""
#     fabtools.deb.update_index(quiet=True)
#     fabtools.require.deb.package('software-properties-common')
#     fabtools.require.deb.ppa(
#         #'ppa:ubuntugis/ubuntugis-unstable', auto_yes=True)
#         'ppa:ubuntugis/ubuntugis-unstable')
# 
# 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/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/proj4.py
# @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()
# 
#     with cd(code_path):
#         # Dont fail if make clean does not work
#         with settings(warn_only=True):
#             run('make clean')
#         run('./configure')
#         run('make -j %s' % processor_count)
#         sudo('make install')
#     # Write to ld path too so libs are loaded nicely
#     ld_file = '/etc/ld.so.conf.d/usr_local_lib.conf'
#     with settings(warn_only=True):
#         sudo('rm %s' % ld_file)
#     append_if_not_present(ld_file, '/usr/local/lib', use_sudo=True)
#     sudo('ldconfig')


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

# coding=utf-8
"""
GDAL Related Tasks.
===================

Module for gdal related tasks"""



@task
def build_gdal(with_ecw=False, with_mrsid=False):
    """Clone or update GDAL from svn then build it.

    :param with_ecw: Whether to build with ecw support.
    :type with_ecw: bool

    :param with_mrsid: Whether to build with mrsid support.
    :type with_mrsid: bool

    """

    setup_env()