'This method works with a combination of three tool dependency definition tag sets, which are defined in
the tool_dependencies.xml file in the order discussed here.  The example for this discussion is the
tool_dependencies.xml file contained in the osra repository, which is available at:
https://testtoolshed.g2.bx.psu.edu/view/bgruening/osra
The first tag set defines a complex repository dependency like this.  This tag set ensures that changeset
revision XXX of the repository named package_graphicsmagick_1_3 owned by YYY in the tool shed ZZZ has been
previously installed::
<tool_dependency>
<package name="graphicsmagick" version="1.3.18">
<repository changeset_revision="XXX" name="package_graphicsmagick_1_3" owner="YYY" prior_installation_required="True" toolshed="ZZZ" />
</package>
By the way, there is an env.sh file associated with version 1.3.18 of the graphicsmagick package which looks
something like this (we\'ll reference this file later in this discussion)::
GRAPHICSMAGICK_ROOT_DIR=/<my configured tool dependency path>/graphicsmagick/1.3.18/YYY/package_graphicsmagick_1_3/XXX/gmagick;
export GRAPHICSMAGICK_ROOT_DIR
The second tag set defines a specific package dependency that has been previously installed (guaranteed by the
tag set discussed above) and compiled, where the compiled dependency is needed by the tool dependency currently
being installed (osra version 2.0.0 in this case) and complied in order for its installation and compilation to
succeed.  This tag set is contained within the <package name="osra" version="2.0.0"> tag set, which implies that
version 2.0.0 of the osra package requires version 1.3.18 of the graphicsmagick package in order to successfully
compile.  When this tag set is handled, one of the effects is that the env.sh file associated with graphicsmagick
version 1.3.18 is "sourced", which undoubtedly sets or alters certain environment variables (e.g. PATH, PYTHONPATH,
etc)::
<!-- populate the environment variables from the dependent repositories -->
<action type="set_environment_for_install">
<repository changeset_revision="XXX" name="package_graphicsmagick_1_3" owner="YYY" toolshed="ZZZ">
<package name="graphicsmagick" version="1.3.18" />
</repository>
</action>
The third tag set enables discovery of the same required package dependency discussed above for correctly compiling
the osra version 2.0.0 package, but in this case the package can be discovered at tool execution time.  Using the
$ENV[] option as shown in this example, the value of the environment variable named GRAPHICSMAGICK_ROOT_DIR (which
was set in the environment using the second tag set described above) will be used to automatically alter the env.sh
file associated with the osra version 2.0.0 tool dependency when it is installed into Galaxy.  * Refer to where we
discussed the env.sh file for version 1.3.18 of the graphicsmagick package above::
<action type="set_environment">
<environment_variable action="prepend_to" name="LD_LIBRARY_PATH">$ENV[GRAPHICSMAGICK_ROOT_DIR]/lib/</environment_variable>
<environment_variable action="prepend_to" name="LD_LIBRARY_PATH">$INSTALL_DIR/potrace/build/lib/</environment_variable>
<environment_variable action="prepend_to" name="PATH">$INSTALL_DIR/bin</environment_variable>
<!-- OSRA_DATA_FILES is only used by the galaxy wrapper and is not part of OSRA -->
<environment_variable action="set_to" name="OSRA_DATA_FILES">$INSTALL_DIR/share</environment_variable>
</action>
The above tag will produce an env.sh file for version 2.0.0 of the osra package when it it installed into Galaxy
that looks something like this.  Notice that the path to the gmagick binary is included here since it expands the
defined $ENV[GRAPHICSMAGICK_ROOT_DIR] value in the above tag set::
LD_LIBRARY_PATH=/<my configured tool dependency path>/graphicsmagick/1.3.18/YYY/package_graphicsmagick_1_3/XXX/gmagick/lib/:$LD_LIBRARY_PATH;
export LD_LIBRARY_PATH
LD_LIBRARY_PATH=/<my configured tool dependency path>/osra/1.4.0/YYY/depends_on/XXX/potrace/build/lib/:$LD_LIBRARY_PATH;
export LD_LIBRARY_PATH
PATH=/<my configured tool dependency path>/osra/1.4.0/YYY/depends_on/XXX/bin:$PATH;
export PATH
OSRA_DATA_FILES=/<my configured tool dependency path>/osra/1.4.0/YYY/depends_on/XXX/share;
export OSRA_DATA_FILES'

def handle_environment_variables(self, install_environment, tool_dependency, env_var_dict, set_prior_environment_commands):

    env_var_value = env_var_dict['value']
    if (('$ENV[' in env_var_value) and (']' in env_var_value)):
        inherited_env_var_name = env_var_value.split('[')[1].split(']')[0]
        to_replace = ('$ENV[%s]' % inherited_env_var_name)
        found = False
        for env_cmd in set_prior_environment_commands:
            if env_cmd.startswith(inherited_env_var_name):
                env_val = env_cmd.split('=')[1]
                env_val = env_val.split(';')[0]
                log.info('Replacing    %s    with    %s    in    env.sh    for    this    repository.', to_replace, env_val)
                env_var_value = env_var_value.replace(to_replace, env_val)
                found = True
                break
        if (not found):
            log.debug('Environment    variable    %s    not    found,    removing    from    set_environment.', inherited_env_var_name)
            env_var_value = env_var_value.replace(to_replace, ('$%s' % inherited_env_var_name))
        env_var_dict['value'] = env_var_value
    return env_var_dict
