# Path: fabgis/system.py
# @task
# def setup_qt4_developer_tools():
#     """Install various useful tools needed for developers."""
#     fabtools.require.deb.package('qtcreator')
#     fabtools.require.deb.package('qt4-designer')
#     fabtools.require.deb.package('qt4-linguist-tools')
# 
# @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')
# 
# Path: fabgis/git.py
# @task
# def update_git_checkout(code_path, url, repo_alias, branch='master', tag=None):
#     """Make sure there is a read only git checkout.
# 
#     :param code_path: Path to where the repo should be checked out.
#     :type code_path: str
# 
#     :param url: Complete http url for cloning/checking out the repo.
#     :type url: str
# 
#     :param repo_alias: Alias name under which the repo should be checked out.
#     :type repo_alias: str
# 
#     :param branch: The name of the branch to build from. Defaults to 'master'.
#     :type branch: str
# 
#     :param tag: Tag to checkout. If None this is ignored. If not none will be
#         used in preference to branch.
#     :type tag: None, str
# 
#     To run e.g.::
# 
#         fab -H foo:1234 remote update_git_checkout
# 
#     """
#     fastprint(cyan('Updating git checkout.\n'))
#     fastprint(cyan('code_path: %s\n' % code_path))
#     fastprint(cyan('url: %s\n' % url))
#     fastprint(cyan('repo_alias: %s\n' % repo_alias))
#     fastprint(cyan('branch: %s\n' % branch))
#     fastprint(cyan('tag: %s\n' % tag))
#     setup_env()
#     repo_path = os.path.join(code_path, repo_alias)
# 
#     fabtools.require.deb.package('git')
#     if not exists(repo_path):
#         fastprint(green('Repository does not exist, creating.\n'))
#         fabtools.require.directory(code_path, use_sudo=True, owner=env.user)
#         with cd(code_path):
#             run('git clone %s %s' % (url, repo_alias))
#     else:
#         fastprint(green('Repo checkout does exist, updating.'))
#         with cd(repo_path):
#             # Get any updates first
#             run('git fetch')
#             if tag is not None:
#                 tags = run('git tag --list')
#                 if tag in tags:
#                     fastprint(green('Checking out tag.'))
#                     run('git checkout %s' % tag)
#                 else:
#                     fastprint(red('Checking out tag failed - unknown tag.'))
#                     raise Exception('Git checkout failed for tag')
# 
#             elif branch != 'master':
#                 fastprint(green('Checking out branch.'))
#                 branches = run('git branch')
#                 if branch not in branches:
#                     print branches
#                     run('git branch --track %s origin/%s' % (branch, branch))
#                 if ('* %s' % branch) not in branches:
#                     run('git checkout %s' % branch)
#             else:
#                 fastprint(green('Checking out master.'))
#                 run('git checkout master')
#             run('git pull')
#     fastprint(green('Checking out tag completed.'))
# 
# Path: fabgis/qgis.py
# @task
# def install_qgis2(gdal_from_source=False):
#     """Install QGIS 2 under /usr/local/qgis-2.0.
# 
#     :param gdal_from_source: Whether gdal should be built from source.
#         Default False.
#     :type gdal_from_source: bool
# 
#     TODO: create one function from this and the 1.8 function above for DRY.
# 
#     """
#     setup_env()
#     setup_ccache()
#     add_ubuntugis_ppa()
#     sudo('apt-get build-dep -y qgis')
# 
#     #fabtools.require.deb.package('python-pyspatialite')
#     fabtools.require.deb.package('python-psycopg2')
#     fabtools.require.deb.package('python-qscintilla2')
#     fabtools.require.deb.package('libqscintilla2-dev')
#     fabtools.require.deb.package('libspatialindex-dev')
# 
#     clone_qgis(branch='release-2_0')
#     workspace = '%s/cpp' % env.fg.workspace
#     code_path = '%s/QGIS' % workspace
#     build_path = '%s/build-qgis2-fabgis' % code_path
#     build_prefix = '/usr/local/qgis-2.0'
#     compile_qgis(build_path, build_prefix, gdal_from_source)


import os
import fabtools
from fabric.api import task, fastprint, env
from fabric.colors import blue, green
from .system import setup_qt4_developer_tools, setup_ccache
from .git import update_git_checkout
from .qgis import install_qgis2
"""



@task
def setup_inasafe():
    """Setup requirements for InaSAFE."""
    fastprint(blue('Setting up InaSAFE dependencies\n'))
    setup_qt4_developer_tools()
    setup_ccache()
    install_qgis2()
    fabtools.require.deb.packages([
        'pep8',
        'pylint',
        'python-nose',
        'python-nosexcover',
        'python-pip',
        'python-numpy',
        'python-qt4',
        'python-nose',
        'gdal-bin',
        'rsync',
        'python-coverage',
        'python-gdal',
        'pyqt4-dev-tools',
        'pyflakes',

    ])
    code_path = os.path.join('/home', env.user, 'dev', 'python')


# Path: fabgis/system.py
# @task
# def setup_qt4_developer_tools():
#     """Install various useful tools needed for developers."""
#     fabtools.require.deb.package('qtcreator')
#     fabtools.require.deb.package('qt4-designer')
#     fabtools.require.deb.package('qt4-linguist-tools')
# 
# @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')
# 
# Path: fabgis/git.py
# @task
# def update_git_checkout(code_path, url, repo_alias, branch='master', tag=None):
#     """Make sure there is a read only git checkout.
# 
#     :param code_path: Path to where the repo should be checked out.
#     :type code_path: str
# 
#     :param url: Complete http url for cloning/checking out the repo.
#     :type url: str
# 
#     :param repo_alias: Alias name under which the repo should be checked out.
#     :type repo_alias: str
# 
#     :param branch: The name of the branch to build from. Defaults to 'master'.
#     :type branch: str
# 
#     :param tag: Tag to checkout. If None this is ignored. If not none will be
#         used in preference to branch.
#     :type tag: None, str
# 
#     To run e.g.::
# 
#         fab -H foo:1234 remote update_git_checkout
# 
#     """
#     fastprint(cyan('Updating git checkout.\n'))
#     fastprint(cyan('code_path: %s\n' % code_path))
#     fastprint(cyan('url: %s\n' % url))
#     fastprint(cyan('repo_alias: %s\n' % repo_alias))
#     fastprint(cyan('branch: %s\n' % branch))
#     fastprint(cyan('tag: %s\n' % tag))
#     setup_env()
#     repo_path = os.path.join(code_path, repo_alias)
# 
#     fabtools.require.deb.package('git')
#     if not exists(repo_path):
#         fastprint(green('Repository does not exist, creating.\n'))
#         fabtools.require.directory(code_path, use_sudo=True, owner=env.user)
#         with cd(code_path):
#             run('git clone %s %s' % (url, repo_alias))
#     else:
#         fastprint(green('Repo checkout does exist, updating.'))
#         with cd(repo_path):
#             # Get any updates first
#             run('git fetch')
#             if tag is not None:
#                 tags = run('git tag --list')
#                 if tag in tags:
#                     fastprint(green('Checking out tag.'))
#                     run('git checkout %s' % tag)
#                 else:
#                     fastprint(red('Checking out tag failed - unknown tag.'))
#                     raise Exception('Git checkout failed for tag')
# 
#             elif branch != 'master':
#                 fastprint(green('Checking out branch.'))
#                 branches = run('git branch')
#                 if branch not in branches:
#                     print branches
#                     run('git branch --track %s origin/%s' % (branch, branch))
#                 if ('* %s' % branch) not in branches:
#                     run('git checkout %s' % branch)
#             else:
#                 fastprint(green('Checking out master.'))
#                 run('git checkout master')
#             run('git pull')
#     fastprint(green('Checking out tag completed.'))
# 
# Path: fabgis/qgis.py
# @task
# def install_qgis2(gdal_from_source=False):
#     """Install QGIS 2 under /usr/local/qgis-2.0.
# 
#     :param gdal_from_source: Whether gdal should be built from source.
#         Default False.
#     :type gdal_from_source: bool
# 
#     TODO: create one function from this and the 1.8 function above for DRY.
# 
#     """
#     setup_env()
#     setup_ccache()
#     add_ubuntugis_ppa()
#     sudo('apt-get build-dep -y qgis')
# 
#     #fabtools.require.deb.package('python-pyspatialite')
#     fabtools.require.deb.package('python-psycopg2')
#     fabtools.require.deb.package('python-qscintilla2')
#     fabtools.require.deb.package('libqscintilla2-dev')
#     fabtools.require.deb.package('libspatialindex-dev')
# 
#     clone_qgis(branch='release-2_0')
#     workspace = '%s/cpp' % env.fg.workspace
#     code_path = '%s/QGIS' % workspace
#     build_path = '%s/build-qgis2-fabgis' % code_path
#     build_prefix = '/usr/local/qgis-2.0'
#     compile_qgis(build_path, build_prefix, gdal_from_source)


# Path: fabgis/inasafe.py
import os
import fabtools
from fabric.api import task, fastprint, env
from fabric.colors import blue, green
from .system import setup_qt4_developer_tools, setup_ccache
from .git import update_git_checkout
from .qgis import install_qgis2

"""



@task
def setup_inasafe():
    """Setup requirements for InaSAFE."""
    fastprint(blue('Setting up InaSAFE dependencies\n'))
    setup_qt4_developer_tools()
    setup_ccache()
    install_qgis2()
    fabtools.require.deb.packages([
        'pep8',
        'pylint',
        'python-nose',
        'python-nosexcover',
        'python-pip',
        'python-numpy',
        'python-qt4',
        'python-nose',
        'gdal-bin',
        'rsync',
        'python-coverage',
        'python-gdal',
        'pyqt4-dev-tools',
        'pyflakes',

    ])
    code_path = os.path.join('/home', env.user, 'dev', 'python')


    update_git_checkout(