##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.00-init/%M%
#   Revision:		%I%
#   First Edition:	2017/11/02
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Sub-Command:
#
#   Comment:	%C{
#			Check runtime quarantine mechanism of environment variables.
#		}C%
#
##############################################################################

# Test handling of quarantine-related variables, full runtime quarantine
# test is made in the install testsuite.
if {$install_quarantinesupport eq y} {

set modfile "$env(TESTSUITEDIR)/modulefiles.2/quar"


# no quarantine setup, nothing should be set
testouterr_cmd "ALL" "source $modfile" "OK" ""


# empty quarantine setup
set quar ""
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUN_QUARANTINE = '$quar'\n"
}
set env(MODULES_RUN_QUARANTINE) $quar

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"

testouterr_cmd "ALL" "source $modfile" "OK" [join $ans "\n"]


# one quarantine variable, no env value, no runtime value: nop
set quar "BAR"
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUN_QUARANTINE = '$quar'\n"
    send_user "\tUnset BAR\n"
}
set env(MODULES_RUN_QUARANTINE) $quar
if {[info exists env(BAR)]} {
    unset env(BAR)
}

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"

testouterr_cmd "ALL" "source $modfile" "OK" [join $ans "\n"]


# one quarantine variable, no env value, with runtime value: envvar set, quarvar unset
set runenv1 "value valpart2 valspe\"ci\"al"
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUNENV_BAR = '$runenv1'\n"
    send_user "\tSetup BAR = '$runenv1'\n"
}
set env(MODULES_RUNENV_BAR) $runenv1
set env(BAR) $runenv1

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"
lappend ans "MODULES_RUNENV_BAR=$runenv1"

set anscsh [list]
lappend anscsh "MODULES_RUN_QUARANTINE=$quar"
lappend anscsh "BAR=$runenv1"
lappend anscsh "MODULES_RUNENV_BAR=$runenv1"

foreach shell $supported_shells {
    switch -- $shell {
        csh - tcsh {
            # no quarantine mechanism support on these shells
            testouterr_cmd "$shell" "source $modfile" "OK" [join $anscsh "\n"]
        }
        default {
            testouterr_cmd "$shell" "source $modfile" "OK" [join $ans "\n"]
        }
    }
}


# one quarantine variable, with env value, no runtime value: envvar empty, quarvar set
set env1 "orig"
if { $verbose > 0 } {
    send_user "\tUnset MODULES_RUNENV_BAR\n"
    send_user "\tSetup BAR_modquar = '$env1'\n"
    send_user "\tSetup BAR = ''\n"
}
unset env(MODULES_RUNENV_BAR)
set env(BAR_modquar) $env1
set env(BAR) ""

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"
lappend ans "BAR=$env1"

set anscsh [list]
lappend anscsh "MODULES_RUN_QUARANTINE=$quar"
lappend anscsh "BAR="
lappend anscsh "BAR_modquar=$env1"

foreach shell $supported_shells {
    switch -- $shell {
        csh - tcsh {
            # no quarantine mechanism support on these shells
            testouterr_cmd "$shell" "source $modfile" "OK" [join $anscsh "\n"]
        }
        default {
            testouterr_cmd "$shell" "source $modfile" "OK" [join $ans "\n"]
        }
    }
}


# one quarantine variable, with env value, with runtime value: envar set, quarvar set
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUNENV_BAR = '$runenv1'\n"
    send_user "\tSetup BAR = '$runenv1'\n"
}
set env(MODULES_RUNENV_BAR) $runenv1
set env(BAR) $runenv1

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"
lappend ans "BAR=$env1"
lappend ans "MODULES_RUNENV_BAR=$runenv1"

set anscsh [list]
lappend anscsh "MODULES_RUN_QUARANTINE=$quar"
lappend anscsh "BAR=$runenv1"
lappend anscsh "MODULES_RUNENV_BAR=$runenv1"
lappend anscsh "BAR_modquar=$env1"

foreach shell $supported_shells {
    switch -- $shell {
        csh - tcsh {
            # no quarantine mechanism support on these shells
            testouterr_cmd "$shell" "source $modfile" "OK" [join $anscsh "\n"]
        }
        default {
            testouterr_cmd "$shell" "source $modfile" "OK" [join $ans "\n"]
        }
    }
}


# one good quarantine variable (with env value & runtime value) among bad variable names
set quar "BAR F:OO  B\\AD 0BAD"
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUN_QUARANTINE = '$quar'\n"
}
set env(MODULES_RUN_QUARANTINE) $quar

set ans [list]
lappend ans "$warn_msgs: Bad variable name set in MODULES_RUN_QUARANTINE (F:OO)"
lappend ans "$warn_msgs: Bad variable name set in MODULES_RUN_QUARANTINE (B\\AD)"
lappend ans "$warn_msgs: Bad variable name set in MODULES_RUN_QUARANTINE (0BAD)"
lappend ans "MODULES_RUN_QUARANTINE=$quar"
lappend ans "BAR=$env1"
lappend ans "MODULES_RUNENV_BAR=$runenv1"

set anscsh [list]
lappend anscsh "MODULES_RUN_QUARANTINE=$quar"
lappend anscsh "BAR=$runenv1"
lappend anscsh "MODULES_RUNENV_BAR=$runenv1"
lappend anscsh "BAR_modquar=$env1"

foreach shell $supported_shells {
    switch -- $shell {
        csh - tcsh {
            # no quarantine mechanism support on these shells
            testouterr_cmd "$shell" "source $modfile" "OK" [join $anscsh "\n"]
        }
        default {
            testouterr_cmd "$shell" "source $modfile" "OK" [join $ans "\n"]
        }
    }
}


# two quarantine variables, first with env and runtime, second with env no runtime
set quar "BAR FOO"
set env2 "orig2 part2 spe\"ci\"al"
if { $verbose > 0 } {
    send_user "\tSetup MODULES_RUN_QUARANTINE = '$quar'\n"
    send_user "\tSetup FOO_modquar = '$env2'\n"
    send_user "\tSetup FOO = ''\n"
}
set env(MODULES_RUN_QUARANTINE) $quar
set env(FOO_modquar) $env2
set env(FOO) ""

set ans [list]
lappend ans "MODULES_RUN_QUARANTINE=$quar"
lappend ans "BAR=$env1"
lappend ans "FOO=$env2"
lappend ans "MODULES_RUNENV_BAR=$runenv1"

set anscsh [list]
lappend anscsh "MODULES_RUN_QUARANTINE=$quar"
lappend anscsh "BAR=$runenv1"
lappend anscsh "FOO="
lappend anscsh "MODULES_RUNENV_BAR=$runenv1"
lappend anscsh "FOO_modquar=$env2"
lappend anscsh "BAR_modquar=$env1"

foreach shell $supported_shells {
    switch -- $shell {
        csh - tcsh {
            # no quarantine mechanism support on these shells
            testouterr_cmd "$shell" "source $modfile" "OK" [join $anscsh "\n"]
        }
        default {
            testouterr_cmd "$shell" "source $modfile" "OK" [join $ans "\n"]
        }
    }
}


#
#  Clean up variables used in this test case
#

reset_test_env

} elseif {$verbose > 0} {
   send_user "\tSkipping quarantine mechanism checks\n"
}
