# 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)


import os
from fabgis.fabgis import sed
from fabric.api import cd, task, sudo, fastprint, run
from fabric.colors import green
from fabric.contrib.files import upload_template
from fabtools import require
from .common import setup_env


@task
def setup_apache(
        site_name,
        web_root,
        template_dir=None,
        **kwargs):
    """Set up the apache server for this site.

    :param site_name: Name of the site e.g. cam.linfiniti.com. Should be a
        single word with only alpha characters in it.
    :type site_name: str

    :param web_root: Directory where the content lives.
    :type web_root: str

    :param template_dir: Directory where the template files live. If none
        will default to ``resources/server_config/apache``. Must be a
        relative path to the fabfile you are running.
    :type template_dir: str

    :param kwargs: Any extra keyword arguments that should be appended to the
        token list that will be used when rendering the apache config template.
        Use this to pass in sensitive data such as passwords.
    :type kwargs: dict

    :returns: Path to the apache conf file.
    :rtype: str
    """

# 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/motion.py
import os
from fabgis.fabgis import sed
from fabric.api import cd, task, sudo, fastprint, run
from fabric.colors import green
from fabric.contrib.files import upload_template
from fabtools import require
from .common import setup_env



@task
def setup_apache(
        site_name,
        web_root,
        template_dir=None,
        **kwargs):
    """Set up the apache server for this site.

    :param site_name: Name of the site e.g. cam.linfiniti.com. Should be a
        single word with only alpha characters in it.
    :type site_name: str

    :param web_root: Directory where the content lives.
    :type web_root: str

    :param template_dir: Directory where the template files live. If none
        will default to ``resources/server_config/apache``. Must be a
        relative path to the fabfile you are running.
    :type template_dir: str

    :param kwargs: Any extra keyword arguments that should be appended to the
        token list that will be used when rendering the apache config template.
        Use this to pass in sensitive data such as passwords.
    :type kwargs: dict

    :returns: Path to the apache conf file.
    :rtype: str
    """

    setup_env()