##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:       modules.00-init/%M%
#   Revision:       %I%
#   First Edition:  2020/04/25
#   Last Mod.:      %U%, %G%
#
#   Authors:        Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:    Testuite testsequence
#   Command:
#   Sub-Command:
#
#   Comment:    %C{
#           Check modulefiles generated by 'sh-to-mod' sub-command
#       }C%
#
##############################################################################

set shtomod_supported_shells [list sh bash ksh zsh dash ksh93 fish csh tcsh]
set shtomod_supported_fshells [list sh bash ksh zsh fish csh tcsh]
set testscriptsh testsuite/example/sh-to-mod.sh
set testscriptcsh testsuite/example/sh-to-mod.csh
set testscriptfish testsuite/example/sh-to-mod.fish
set testscriptshargs "v\\\'l val\\\"e val\\\\\\\"e \"\""
set testscriptshargs_csh "v\'\"\\\'\"\'l val\\\"e val\\\\\\\"e \"\""

# what are the installed shells
set shtomod_avail_shells [list]
foreach sh $shtomod_supported_shells {
     set shbin [lindex [auto_execok $sh] 0]
    if {$shbin ne {}} {
       # mksh flavor of ksh is not supported
       # sh from FreeBSD is not supported too
        if {($sh ne {ksh} || !$ksh_is_mksh) && ($sh ne {sh} || $tcl_platform(os) ne {FreeBSD})} {
           lappend shtomod_avail_shells $sh
        }
        if {[lsearch -exact $shtomod_supported_fshells $sh] != -1} {
           lappend shtomod_avail_fshells $sh
        }
    }
}

set testshenvcmds "echo \"\$FOOCB:\$FOOEM:\$FOOSP:\$FOOARG1:\$FOOARG2:\$FOOARG3:\$FOOARG4\"\;"
append testshenvcmds "alfoo\; alcb\; alsp\;"
append testshenvcmds "funcnl\; funccb\; funcsp\;"

set testshenvcmds_dash "echo \"\$FOOCB:\$FOOEM:\$FOOSP:\$FOOARG1:\$FOOARG2:\$FOOARG3:\$FOOARG4\"\;"
append testshenvcmds_dash "alfoo\; alcb\; alsp\;"

set testshenvcmds_csh "echo \$\{FOOCB\}:\$\{FOOEM\}:\$\{FOOSP\}:\$\{FOOARG1\}:\$\{FOOARG2\}:\$\{FOOARG3\}:\$\{FOOARG4\}\;"
append testshenvcmds_csh "alfoo\; alcb\; alsp\;"

if {$verbose > 0} {
   send_user "\tChecking modulefiles generated by 'sh-to-mod' sub-command\n"
}

set tsout "va{ue::value :v'l:val\"e:val\\\"e:\n"
append tsout "Release\nf{o\nf\"o\nb\\\"r\nf'o\n"
append tsout "foo\nbar\nf{o\nf\"o\nb\\\"r\nf'o"

set tsout_nonl "va{ue::value :v'l:val\"e:val\\\"e:\n"
append tsout_nonl "Release\nf{o\nf\"o\nb\\\"r\nf'o\n"
append tsout_nonl "foo echo bar\nf{o\nf\"o echo b\\\"r echo f'o"

# no argument passed to script nor function defined with dash
set tsout_dash "va{ue::value ::::\n"
append tsout_dash "Release\nf{o\nf\"o\nb\\\"r\nf'o"

# no function defined with csh
set tsout_csh "va{ue::value :v'l:val\"e:val\\\"e:\n"
append tsout_csh "Release\nf{o\nf\"o\nb\\\"r\nf'o"

foreach sh $shtomod_avail_shells {
    # test modulefile definition also cope with other shells
    foreach fsh $shtomod_avail_fshells {
        if {$fsh eq {fish} || $sh eq {fish}} {
           # no shell mix for fish, as a function or alias defined for a sh shell will not work on fish
           if {$fsh eq {fish} && $sh eq {fish}} {
              if {!$fish_version_ge31} {
                 send_user "\tSkipping sh-to-mod test from fish shell as output redirection of an evaluated test command does not work on Fish <3.1\n"
              } else {
                 testall_cmd $fsh "module sh-to-mod $sh $testscriptfish $testscriptshargs 2>shtomod\; module load ./shtomod\; $testshenvcmds" $tsout {} 0
              }
           }
        } elseif {$fsh eq {csh} || $fsh eq {tcsh} || $sh eq {csh} || $sh eq {tcsh}} {
           # no shell mix for csh, as a function or alias defined for a sh/fish shell will not work on csh
           # disable test if wa277 is enabled as in this case csh module alias has no protection against special chars in output
           if {($fsh eq {csh} || $fsh eq {tcsh}) && ($sh eq {csh} || $sh eq {tcsh}) && $install_wa277 eq {n}} {
              set tserr [expr {$tcl_platform(os) eq {Darwin} || $tcl_platform(os) eq {FreeBSD} ? {} : {tr: warning: an unescaped backslash at end of string is not portable}}]
              testall_cmd $fsh "module sh-to-mod $sh $testscriptcsh $testscriptshargs_csh >& shtomod\; module load ./shtomod\; $testshenvcmds_csh" $tsout_csh $tserr 0
           }
        } elseif {$sh eq {dash} || ($sh eq {sh} && $sh_kind eq {dash})} {
           testall_cmd $fsh "module sh-to-mod $sh $testscriptsh $testscriptshargs 2>shtomod\; module load ./shtomod\; $testshenvcmds_dash" $tsout_dash {} 0
        } else {
           # with quarantine mechanism disabled, no specific IFS is set when modulecmd.tcl output is evaluated
           # with zsh and ksh shells, sh-to-mod does not output ';' character at end of each function line, thus
           # when evaluated with default IFS the multiple lines are treated as a single command line
           set out [expr {$install_quarantinesupport eq {n} && [lsearch -exact [list ksh zsh ksh93] $sh] != -1 ? $tsout_nonl : $tsout}]
           testall_cmd $fsh "module sh-to-mod $sh $testscriptsh $testscriptshargs 2>shtomod\; module load ./shtomod\; $testshenvcmds" $out {} 0
        }
        file delete shtomod
    }
}


#
#  Clean up variables used in this test case
#

reset_test_env

# vim:set tabstop=3 shiftwidth=3 expandtab autoindent:
