##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	1995/12/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Jens Hamisch, Jens.Hamisch@Strawberry.COM
#
#   Description:	Testuite testsequence
#   Command:		unload
#   Modulefiles:	prepend/1.0
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the reverse 'prepend-path' module subcommand for
#			all allowed shell types.
#		}C%
#
##############################################################################

#
#  Variables. This test forces a module unload command. It will result in the
#    environment variables "_LMFILES_", "LOADEDMODULES" and "PATH" to
#    be set up
#

set module "prepend/2.0"
set modulefile "$env(MODULEPATH)/$module"

set old_path $env(PATH)
regsub -all {([ ()])} $old_path {\\\1} old_path_esc
set ts_path "/this/is/the/modules/testsuite"
set new_path "$ts_path:$old_path"

#
#  Set up the environment for reversal ...
#

set env(PATH) "$new_path"
set env(_LMFILES_) "$modulefile"
set env(LOADEDMODULES) "$module"

#
#  For the different shells ...
#

set lmf_sh "unset _LMFILES_;"
set lm_sh "unset LOADEDMODULES;"
set ts_sh "PATH=$old_path_esc ;export PATH;"

set lmf_csh "unsetenv _LMFILES_;"
set lm_csh "unsetenv LOADEDMODULES;"
set ts_csh "setenv PATH $old_path_esc ;"

set lmf_perl "delete \$ENV{'_LMFILES_'};"
set lm_perl "delete \$ENV{'LOADEDMODULES'};"
set ts_perl "\$ENV{'PATH'} = '$old_path';"

set lmf_cmake "unset(ENV{_LMFILES_})"
set lm_cmake "unset(ENV{LOADEDMODULES})"
set ts_cmake "set(ENV{PATH} \"$old_path\")"

#
#  The tests
#

test_cmd "sh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "ksh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "zsh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "csh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "tcsh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "perl" "unload $module" "$ts_perl$lm_perl$lmf_perl"
test_cmd "cmake" "unload $module" "$ts_cmake\n$lm_cmake\n$lmf_cmake"

#
# Try the empty string
#

set module "prepend/0.1"
set modulefile "$env(MODULEPATH)/$module"

set ts_path ""
set new_path "$ts_path:$old_path"

#
#  Set up the environment for reversal ...
#

set env(PATH) "$new_path"
set env(_LMFILES_) "$modulefile"
set env(LOADEDMODULES) "$module"

#
#  For the different shells ...
#

set lmf_sh "unset _LMFILES_;"
set lm_sh "unset LOADEDMODULES;"
set ts_sh "PATH=$old_path_esc ;export PATH;"

set lmf_csh "unsetenv _LMFILES_;"
set lm_csh "unsetenv LOADEDMODULES;"
set ts_csh "setenv PATH $old_path_esc ;"

set lmf_perl "delete \$ENV{'_LMFILES_'};"
set lm_perl "delete \$ENV{'LOADEDMODULES'};"
set ts_perl "\$ENV{'PATH'} = '$old_path';"

set lmf_cmake "unset(ENV{_LMFILES_})"
set lm_cmake "unset(ENV{LOADEDMODULES})"
set ts_cmake "set(ENV{PATH} \"$old_path\")"

#
#  The tests
#

test_cmd "sh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "ksh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "zsh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "csh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "tcsh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "perl" "unload $module" "$ts_perl$lm_perl$lmf_perl"
test_cmd "cmake" "unload $module" "$ts_cmake\n$lm_cmake\n$lmf_cmake"

#
# Try just the colon
#

set module "prepend/0.2"
set modulefile "$env(MODULEPATH)/$module"

set ts_path ":"
set new_path "$ts_path:$old_path"

#
#  Set up the environment for reversal ...
#

set env(PATH) "$new_path"
set env(_LMFILES_) "$modulefile"
set env(LOADEDMODULES) "$module"

#
#  For the different shells ...
#

set lmf_sh "unset _LMFILES_;"
set lm_sh "unset LOADEDMODULES;"
set ts_sh "PATH=$old_path_esc ;export PATH;"

set lmf_csh "unsetenv _LMFILES_;"
set lm_csh "unsetenv LOADEDMODULES;"
set ts_csh "setenv PATH $old_path_esc ;"

set lmf_perl "delete \$ENV{'_LMFILES_'};"
set lm_perl "delete \$ENV{'LOADEDMODULES'};"
set ts_perl "\$ENV{'PATH'} = '$old_path';"

set lmf_cmake "unset(ENV{_LMFILES_})"
set lm_cmake "unset(ENV{LOADEDMODULES})"
set ts_cmake "set(ENV{PATH} \"$old_path\")"

#
#  The tests
#

test_cmd "sh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "ksh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "zsh" "unload $module" "$ts_sh$lm_sh$lmf_sh"
test_cmd "csh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "tcsh" "unload $module" "$ts_csh$lm_csh$lmf_csh"
test_cmd "perl" "unload $module" "$ts_perl$lm_perl$lmf_perl"
test_cmd "cmake" "unload $module" "$ts_cmake\n$lm_cmake\n$lmf_cmake"

#
#  Clean up the environment
#

set env(PATH) "$old_path"
unset env(_LMFILES_)
unset env(LOADEDMODULES)

#
#  Cleanup
#

unset new_path
unset ts_path
unset old_path
unset old_path_esc

unset ts_sh
unset lm_sh
unset lmf_sh

unset ts_csh
unset lm_csh
unset lmf_csh

unset ts_perl
unset lm_perl
unset lmf_perl

unset ts_cmake
unset lm_cmake
unset lmf_cmake

unset modulefile
unset module
