##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2017/08/29
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload, display, help, test, avail, whatis,
#                   path, paths
#   Modulefiles:	.hidden, loc_rc8, loc_rc9
#   Sub-Command:
#
#   Comment:	%C{
#		}C%
#
##############################################################################

# ensure avail tests are made using in depth mode
setenv_var MODULES_AVAIL_INDEPTH 1

# ensure auto symbolic versions are not set for these tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 0

set datetime_re "\[0-9\/]{10} \[0-9:]{8}"

set modsym_1 ".hidden"
set module_1 ".hidden/1.0"
set modulefile_1 "$modpath/$module_1"
set modulefilere_1 "$modpathre/$module_1"
set modavail_1 "$module_1\\s+$datetime_re"
set nodefault_1 "$err_nodefault'$modsym_1'"

set modsym_2 ".hidden/1.0"
set module_2 "$module_1"
set modulefile_2 "$modpath/$module_2"
set modulefilere_2 "$modpathre/$module_2"
set modavail_2 "$modavail_1"

set modsym_3 "loc_rc8/.2.0"
set module_3 "$modsym_3"
set modalt_3 loc_rc8/foo&al|loc_rc8/0.9
set modulefile_3 "$modpath/$module_3"
set modulefilere_3 "$modpathre/$module_3"
set modavail_3 "$module_3\\s+foo\\s+$datetime_re"

set modsym_4 "loc_rc8/foo"
set module_4 "$module_3"
set modalt_4 $modalt_3
set modulefile_4 "$modpath/$module_4"
set modulefilere_4 "$modpathre/$module_4"
set modavail_4 "$modavail_3"

set modsym_5 "loc_rc8/0.9"
set module_5 "$module_3"
set modalt_5 $modalt_3
set modulefile_5 "$modpath/$module_5"
set modulefilere_5 "$modpathre/$module_5"
set modavail_5 "$modsym_5 -> loc_rc8/.2.0\\s+"

set modsym_6 "loc_rc8"
set module_6 "loc_rc8/1.0"
set modulefile_6 "$modpath/$module_6"
set modulefilere_6 "$modpathre/$module_6"
set modavail_6 "$modavail_5\n$module_6\\s+$datetime_re"
set modwhatis_6 "\\s+$module_3: $module_3\n\\s+$module_6: $module_6"
set modpaths_6 [list $modulefile_3 $modulefile_6]
set nodefault_6 "$err_nodefault'$modsym_6'"

set modsym_7 "loc_rc8/.subdir/4.0"
set module_7 "$modsym_7"
set modulefile_7 "$modpath/$module_7"
set modulefilere_7 "$modpathre/$module_7"
set modavail_7 "$module_7\\s+$datetime_re"

set modsym_8 "loc_rc8/.subdir"
set module_8 "$module_7"
set modulefile_8 "$modpath/$module_8"
set modulefilere_8 "$modpathre/$module_8"
set modavail_8 "$modavail_7"
set nodefault_8 "$err_nodefault'$modsym_8'"

set modsym_9 "loc_rc9"
set module_9 "loc_rc9/.1.0"
set modulefile_9 "$modpath/$module_9"
set modulefilere_9 "$modpathre/$module_9"
set modavail_9 ""
set modwhatis_9 ""
set modpaths_9 [list]
set moderr_9 "$err_path'$modsym_9'"

# working with virtual modules, loading target file
set modsym_10 "loc_rcv1/.base"
set module_10 "$modsym_10"
set modulefile_10 "$modpath/$module_10"
set modulefilere_10 "$modpathre/$module_10"
set modavail_10 "$module_10\\s+$datetime_re"

# hidden module is set default
set modsym_11 loc_rc010
set module_11 $modsym_11/.1.0
set modalt_11 loc_rc010/default&loc_rc010
set modulefile_11 "$modpath/$module_11"
set modulefilere_11 "$modpathre/$module_11"
set modavail_11 "$modsym_11/2.0\\s+$datetime_re"
set modwhatis_11 "\\s+$modsym_11/2.0: $modsym_11/2.0"
# does not appear on avail/whatis commands, but appear on paths (every hidden modules targeted by non-hidden symbol)
set modpaths_11 [list $modulefile_11 $modpath/$modsym_11/2.0 $modpath/$modsym_11/subdir/.subdir/3.0]

# hidden virtual version
set modsym_12 loc_rcv1/.hidvirt
set module_12 $modsym_12
set modulefile_12 $modpath/$module_10
set modulefilere_12 $modpathre/$module_10
set modavail_12 $module_12\\s+$datetime_re

# hidden symbolic version (on hidden virtual)
set modsym_13 loc_rcv1/.hidsym
set module_13 $module_12
set modalt_13 loc_rcv1/.hidsym
set modulefile_13 $modulefile_12
set modulefilere_13 $modulefilere_12
set modavail_13 $module_12\\s+.hidsym\\s+$datetime_re

# hidden symbolic version (on regular virtual)
set modsym_14 loc_rcv1/.hidsym2
set module_14 loc_rcv1/1.1
set modalt_14 $modsym_14
set modulefile_14 $modpath/$module_10
set modulefilere_14 $modpathre/$module_10
set modavail_14 $module_14\\s+.hidsym2\\s+$datetime_re

# hidden alias
set modsym_15 loc_rc010/.2.2
set module_15 loc_rc010/2.0
set modalt_15 al|$modsym_15
set modulefile_15 $modpath/$module_15
set modulefilere_15 $modpathre/$module_15
set modavail_15 "$modsym_15 -> $module_15\\s+"

# regular module in an hidden directory (hidden directory targeted by non-hidden symbol)
set modsym_16 loc_rc010/subdir
set module_16 loc_rc010/subdir/.subdir/3.0
set modalt_16 $modsym_16/default&$modsym_16
set modulefile_16 $modpath/$module_16
set modulefilere_16 $modpathre/$module_16
set modavail_16 {}
set modwhatis_16 {}
set nodefault_16 "$err_nodefault'$modsym_16/.subdir'"
set moderrwh_16 "$err_path'$modsym_16'"

# regular module in an hidden directory (hidden directory targeted by hidden symbol)
set modsym_17 loc_rc010/subdir/.hidsub
set module_17 loc_rc010/subdir/.hidsub/3.0
set modulefile_17 $modpath/$module_17
set modulefilere_17 $modpathre/$module_17
set modavail_17 "$module_17\\s+$datetime_re"
set nodefault_17 "$err_nodefault'$modsym_17'"


set help_pre "$modlin\nModule Specific Help for "
set help_post ":\n\n$modlin"
set test_pre "$modlin\nModule Specific Test for "
set test_post ":\n\nTest result: PASS\n$modlin"
set disp_pre "$modlin\n"
set disp_wi "module-whatis\\s+"
set disp_post "\nsetenv\\s+testsuite\\s+yes\n$modlin"
set header_long "- Package/Alias $modlin.- Versions $modlin.- Last mod. $modlin"
set avail_pre "$header_long\n$modpathre:"
set whatis_pre "$modlin $modpathre $modlin\n"


#
#  The tests
#

for {set i 1} {$i <= 17} {incr i} {
    set modsym [set "modsym_${i}"]
    set module [set "module_${i}"]
    if {[info exists "modalt_${i}"]} {
        set modalt $module&[set "modalt_${i}"]
    } else {
        set modalt ""
    }
    set modulefile [set "modulefile_${i}"]
    set modulefilere [set "modulefilere_${i}"]
    set modavail [set "modavail_${i}"]
    if {[info exists "modwhatis_${i}"]} {
        set modwhatis [set "modwhatis_${i}"]
    } else {
        set modwhatis "\\s*$module: $module"
    }
    set ans_paths [list]
    if {[info exists "modpaths_${i}"]} {
        lappend ans_paths [list text [set "modpaths_${i}"]]
    } else {
        lappend ans_paths [list text $modulefile]
    }
    if {[info exists "moderrwh_${i}"]} {
        set moderrwh [set "moderrwh_${i}"]
    } elseif {[info exists moderrwh]} {
        unset moderrwh
    }
    if {[info exists "moderr_${i}"]} {
        set moderr [set "moderr_${i}"]
    } elseif {[info exists moderr]} {
        unset moderr
    }
    if {[info exists "::nodefault_${i}"]} {
        set nodefault [set "::nodefault_${i}"]
    } else {
        set nodefault {}
    }

    # setup clean environment
    unsetenv_loaded_module
    unsetenv_path_var MODULES_LMALTNAME

    if {[info exists moderr]} {
        testouterr_cmd "sh" "load $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "display $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "help $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "test $modsym" "ERR" "$moderr"
    } else {
        set ans [list]
        lappend ans [list set testsuite "yes"]
        lappend ans [list setpath LOADEDMODULES $module]
        lappend ans [list setpath _LMFILES_ $modulefile]
        if {$modalt ne ""} {
            lappend ans [list setpath MODULES_LMALTNAME $modalt]
        }

        set displayerr "$disp_pre$modulefilere:\n\n$disp_wi$module$disp_post"
        set helperr "$help_pre$modulefilere$help_post"
        set testerr "$test_pre$modulefilere$test_post"

        if {$nodefault eq {}} {
            testouterr_cmd "sh" "load $modsym" $ans ""
            testouterr_cmd_re "sh" "display $modsym" "OK" $displayerr
            testouterr_cmd_re "sh" "help $modsym" "OK" $helperr
            testouterr_cmd_re "sh" "test $modsym" "OK" $testerr
        } else {
            testouterr_cmd "sh" "load $modsym" [if_implicitdefault $ans ERR] [if_implicitdefault {} $nodefault]
            testouterr_cmd_re "sh" "display $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $displayerr $nodefault]
            testouterr_cmd_re "sh" "help $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $helperr $nodefault]
            testouterr_cmd_re "sh" "test $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $testerr $nodefault]
        }
    }

    if {$modavail eq ""} {
        testouterr_cmd_re "sh" "avail -l $modsym" "OK" ""
    } else {
        testouterr_cmd_re "sh" "avail -l $modsym" "OK" "$avail_pre\n$modavail"
    }
    if {$modwhatis eq ""} {
        testouterr_cmd "sh" "whatis $modsym" "ERR" [expr {[info exists moderrwh] ? $moderrwh : $moderr}]
    } else {
        testouterr_cmd_re "sh" "whatis $modsym" "OK" "$whatis_pre$modwhatis"
    }

    set ans [list]
    if {[info exists moderr]} {
        lappend ans [list text ""]
        testouterr_cmd "sh" "path $modsym" $ans "$moderr"
    } else {
        lappend ans [list text $modulefile]
        if {$nodefault eq {}} {
            testouterr_cmd "sh" "path $modsym" $ans ""
        } else {
            testouterr_cmd "sh" "path $modsym" [if_implicitdefault $ans [list [list text {}]]] [if_implicitdefault {} $nodefault]
        }
    }

    testouterr_cmd "sh" "paths $modsym" $ans_paths ""

    # setup environment for unload action
    setenv_loaded_module $module $modulefile
    if {$modalt ne ""} {
        setenv_path_var MODULES_LMALTNAME $modalt
    }

    set ans [list]
    lappend ans [list unset testsuite]
    lappend ans [list unsetpath LOADEDMODULES]
    lappend ans [list unsetpath _LMFILES_]
    if {$modalt ne ""} {
        lappend ans [list unsetpath MODULES_LMALTNAME]
    }

    testouterr_cmd "sh" "unload $modsym" $ans ""
}



#
#  Cleanup
#

reset_test_env
