# Path: cloudbio/custom/shared.py
# def _set_default_config(env, install_dir, sym_dir_name="default"):
#     """
#     Sets up default galaxy config directory symbolic link (if needed). Needed
#     when it doesn't exists or when installing a new version of software.
#     """
#     version = env["tool_version"]
#     if env.safe_exists(install_dir):
#         install_dir_root = "%s/.." % install_dir
#         sym_dir = "%s/%s" % (install_dir_root, sym_dir_name)
#         replace_default = False
#         if not env.safe_exists(sym_dir):
#             replace_default = True
#         if not replace_default:
#             default_version = env.safe_sudo("basename `readlink -f %s`" % sym_dir)
#             if version > default_version:  # Bug: Wouldn't work for 1.9 < 1.10
#                 print("default version %s is older than version %s just installed, replacing..." % (default_version, version))
#                 replace_default = True
#         if replace_default:
#             env.safe_sudo("rm -rf %s; ln -f -s %s %s" % (sym_dir, install_dir, sym_dir))
# 
# def _add_to_profiles(line, profiles=[], use_sudo=True):
#     """
#     If it's not already there, append ``line`` to shell profiles files.
#     By default, these are ``/etc/profile`` and ``/etc/bash.bashrc`` but can be
#     overridden by providing a list of file paths to the ``profiles`` argument.
#     """
#     if not profiles:
#         profiles = ['/etc/bash.bashrc', '/etc/profile']
#     for profile in profiles:
#         if not env.safe_contains(profile, line):
#             env.safe_append(profile, line, use_sudo=use_sudo)


import os
import six
import yaml
from string import Template
from cloudbio.custom.bio_general import *
from cloudbio.custom.bio_nextgen import *
from cloudbio.custom.bio_proteomics import *
from cloudbio.custom.shared import _set_default_config, _add_to_profiles
from cloudbio.galaxy.applications import *
from cloudbio.galaxy.r import _install_r_packages
from cloudbio.galaxy.utils import _chown_galaxy, _read_boolean
    """
    Setup galaxy tool config files (env.sh-es) and default version
    symbolic links.
    """
    install_dir = tool_env["system_install"]
    env_path = os.path.join(install_dir, "env.sh")
    bin_paths = [os.path.join(install_dir, bin_dir) for bin_dir in bin_dirs]
    path_pieces = [bin_path for bin_path in bin_paths if env.safe_exists(bin_path)]
    if len(path_pieces) > 0 and not env.safe_exists(env_path):
        path_addtion = ":".join(path_pieces)
        # Standard bin install, just add it to path
        env.safe_sudo("echo 'PATH=%s:$PATH' > %s" % (path_addtion, env_path))
        venv_path = "%s/%s" % (install_dir, "venv")
        if env.safe_exists(venv_path):
            #  Have env.sh activate virtualdirectory
            env.safe_sudo("echo '. %s/bin/activate' >> %s" % (venv_path, env_path))
        env.safe_sudo("chmod +x %s" % env_path)
        for env_var, env_var_value in env_vars.iteritems():
            env_var_template = Template(env_var_value)
            expanded_env_var_value = env_var_template.substitute(tool_env)
            env.safe_sudo("echo 'export %s=%s' >> %s" % (env_var, expanded_env_var_value, env_path))
        env.logger.debug("Added Galaxy env.sh file: %s" % env_path)

    # TODO: If a direct install (i.e. tool_install_dir specified instead of galaxy_tools_dir)
    # default is still setup. This is not really desired.
    _set_default_config(tool_env, install_dir)
    if _read_boolean(tool_env, "autoload_galaxy_tools", False) and env.safe_exists(env_path):
        # In this case, the web user (e.g. ubuntu) should auto-load all of
        # galaxy's default env.sh files so they are available for direct use
        # as well.

# Path: cloudbio/custom/shared.py
# def _set_default_config(env, install_dir, sym_dir_name="default"):
#     """
#     Sets up default galaxy config directory symbolic link (if needed). Needed
#     when it doesn't exists or when installing a new version of software.
#     """
#     version = env["tool_version"]
#     if env.safe_exists(install_dir):
#         install_dir_root = "%s/.." % install_dir
#         sym_dir = "%s/%s" % (install_dir_root, sym_dir_name)
#         replace_default = False
#         if not env.safe_exists(sym_dir):
#             replace_default = True
#         if not replace_default:
#             default_version = env.safe_sudo("basename `readlink -f %s`" % sym_dir)
#             if version > default_version:  # Bug: Wouldn't work for 1.9 < 1.10
#                 print("default version %s is older than version %s just installed, replacing..." % (default_version, version))
#                 replace_default = True
#         if replace_default:
#             env.safe_sudo("rm -rf %s; ln -f -s %s %s" % (sym_dir, install_dir, sym_dir))
# 
# def _add_to_profiles(line, profiles=[], use_sudo=True):
#     """
#     If it's not already there, append ``line`` to shell profiles files.
#     By default, these are ``/etc/profile`` and ``/etc/bash.bashrc`` but can be
#     overridden by providing a list of file paths to the ``profiles`` argument.
#     """
#     if not profiles:
#         profiles = ['/etc/bash.bashrc', '/etc/profile']
#     for profile in profiles:
#         if not env.safe_contains(profile, line):
#             env.safe_append(profile, line, use_sudo=use_sudo)


# Path: cloudbio/galaxy/tools.py
import os
import six
import yaml
from string import Template
from cloudbio.custom.bio_general import *
from cloudbio.custom.bio_nextgen import *
from cloudbio.custom.bio_proteomics import *
from cloudbio.custom.shared import _set_default_config, _add_to_profiles
from cloudbio.galaxy.applications import *
from cloudbio.galaxy.r import _install_r_packages
from cloudbio.galaxy.utils import _chown_galaxy, _read_boolean

    """
    Setup galaxy tool config files (env.sh-es) and default version
    symbolic links.
    """
    install_dir = tool_env["system_install"]
    env_path = os.path.join(install_dir, "env.sh")
    bin_paths = [os.path.join(install_dir, bin_dir) for bin_dir in bin_dirs]
    path_pieces = [bin_path for bin_path in bin_paths if env.safe_exists(bin_path)]
    if len(path_pieces) > 0 and not env.safe_exists(env_path):
        path_addtion = ":".join(path_pieces)
        # Standard bin install, just add it to path
        env.safe_sudo("echo 'PATH=%s:$PATH' > %s" % (path_addtion, env_path))
        venv_path = "%s/%s" % (install_dir, "venv")
        if env.safe_exists(venv_path):
            #  Have env.sh activate virtualdirectory
            env.safe_sudo("echo '. %s/bin/activate' >> %s" % (venv_path, env_path))
        env.safe_sudo("chmod +x %s" % env_path)
        for env_var, env_var_value in env_vars.iteritems():
            env_var_template = Template(env_var_value)
            expanded_env_var_value = env_var_template.substitute(tool_env)
            env.safe_sudo("echo 'export %s=%s' >> %s" % (env_var, expanded_env_var_value, env_path))
        env.logger.debug("Added Galaxy env.sh file: %s" % env_path)

    # TODO: If a direct install (i.e. tool_install_dir specified instead of galaxy_tools_dir)
    # default is still setup. This is not really desired.
    _set_default_config(tool_env, install_dir)
    if _read_boolean(tool_env, "autoload_galaxy_tools", False) and env.safe_exists(env_path):
        # In this case, the web user (e.g. ubuntu) should auto-load all of
        # galaxy's default env.sh files so they are available for direct use
        # as well.

        _add_to_profiles(". %s" % env_path, profiles=["~/.bashrc"])