##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:       modules.00-init/%M%
#   Revision:       %I%
#   First Edition:  2017/05/07
#   Last Mod.:      %U%, %G%
#
#   Authors:        Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:    Testuite testsequence
#   Command:
#   Sub-Command:
#
#   Comment:    %C{
#           Check selected installation options have been honored
#       }C%
#
##############################################################################


set bad_cmd "$error_msgs: Invalid command 'foo'"
set use_desc "Search path for module files \\(in search order\\):"
set moddir_header "$install_modulefilesdirre:"
set versdir_header "$install_versionfilesdirre:"
foreach mod [list dot module-git module-info modules null use.own] {
   if {[info exists mod_examples]} {
      append mod_examples "\n"
   }
   append mod_examples $mod
   if {[lsearch -exact [split $install_loadedmodulesre :] $mod] != -1} {
      append mod_examples { <L>}
   }
}
set lm_list [split $install_loadedmodulesre ":"]

# check if modulefiles dir is part of default MODULEPATH
set modfilesdir_in_modpath 0
foreach modp [split $install_modulepath ":"] {
   # adapt modulepath entry if it contains variable reference
   regsub -all {\$[{]?[A-Za-z_][A-Za-z0-9_]*[}]?} $modp {.*} modp
   if {[regexp "^$modp\$" $install_modulefilesdir]} {
      set modfilesdir_in_modpath 1
      break
   }
}

# adapt modulepath entry if it contains variable reference
set modpath_listre [list]
foreach modp [split $install_modulepathre ":"] {
   regsub -all {\$[{]?[A-Za-z_][A-Za-z0-9_]*[}]?} $modp {.*} modp
   lappend modpath_listre $modp
}

set swmain_msg "Switching to Modules main version"
set swcompat_msg "Switching to Modules compatibility version"
set version_msg "Modules Release \\S+ (\\S+)"

#
# The tests
#

if {$install_setmanpath eq "y" && $install_builddoc ne "n"} {
   if {$verbose > 0} {
      send_user "\tChecking man pages availability\n"
   }
   foreach shell $shell_list {
      if {$install_compatversion eq "y"} {
         testall_cmd_re "$shell" "man -w modulefile-compat" "$install_mandirre/man4/modulefile-compat.*" {} 0
         testall_cmd_re "$shell" "man -w module-compat" "$install_mandirre/man1/module-compat.*" {} 0
      }
      testall_cmd_re "$shell" "man -w modulefile" "$install_mandirre/man4/modulefile.*" {} 0
      testall_cmd_re "$shell" "man -w module" "$install_mandirre/man1/module.*" {} 0
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping man pages availability checks\n"
}

if {$install_setbinpath eq "y"} {
   if {[file executable $install_python]} {
      if {$verbose > 0} {
         send_user "\tChecking createmodule.py availability\n"
      }
      foreach shell $shell_list {
         testall_cmd_re "$shell" "createmodule.py -h" "Usage: createmodule.py .*" {} 0
      }
   } elseif {$verbose > 0} {
      send_user "\tSkipping createmodule.py availability tests as no python interpreter found\n"
   }
   if {$verbose > 0} {
      send_user "\tChecking envml availability\n"
   }
   foreach shell $shell_list {
      testall_cmd_re "$shell" "envml -h" "Usage:.*/envml .*" {} 0
   }
   if {$install_examplemodulefiles eq "y" && $install_loadedmodules eq "" \
      && $modfilesdir_in_modpath} {
      foreach shell $shell_list {
         if {$shell eq {bash} || $shell eq {basheu} || [is_set_shell_startup_enabled]} {
            testall_cmd "$shell" "envml null printenv LOADEDMODULES" "null" "" 0
         } else {
            testall_cmd "$shell" "envml null printenv LOADEDMODULES" "" "envml: module command not found..." 1
         }
      }
   }

   if {$verbose > 0} {
      send_user "\tChecking modulecmd availability\n"
   }
   foreach shell $shell_list {
      if {$shell eq "basheu"} {
         testall_cmd_re "$shell" "modulecmd bash --version" {} "Modules Release \\S+ (\\S+)" 0
      } else {
         testall_cmd_re "$shell" "modulecmd $shell --version" {} "Modules Release \\S+ (\\S+)" 0
      }
   }
   if {$install_compatversion eq "y"} {
      # set module compat version as default
      set env(MODULES_USE_COMPAT_VERSION) 1
      if {$verbose > 0} {
         send_user "\tSetup MODULES_USE_COMPAT_VERSION = 1\n"
      }
      foreach shell $shell_list {
         # compat version does not support Fish shell
         if {$shell ne "fish"} {
            if {$shell eq "basheu"} {
               testall_cmd_re "$shell" "modulecmd bash --version" " test 0 = 1;\$" "${compat_warn_setvar}VERSION=\\S+.*" 1
            } else {
               testall_cmd_re "$shell" "modulecmd $shell --version" " test 0 = 1;\$" "${compat_warn_setvar}VERSION=\\S+.*" 1
            }
         }
      }
      # restore environment setup
      unset env(MODULES_USE_COMPAT_VERSION)
      if {$verbose > 0} {
         send_user "\tUnset MODULES_USE_COMPAT_VERSION\n"
      }
   }

   if {$verbose > 0} {
      send_user "\tChecking other utilities availability\n"
   }
   foreach shell $shell_list {
      switch -- $shell {
         csh - tcsh {
            testall_cmd "$shell" "which add.modules" "$install_bindir/add.modules" "" 0
            testall_cmd "$shell" "which mkroot" "$install_bindir/mkroot" "" 0
         }
         default {
            testall_cmd "$shell" "command -v add.modules" "$install_bindir/add.modules" "" 0
            testall_cmd "$shell" "command -v mkroot" "$install_bindir/mkroot" "" 0
         }
      }
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping createmodule.py availability checks\n"
   send_user "\tSkipping envml availability checks\n"
   send_user "\tSkipping modulecmd availability checks\n"
   send_user "\tSkipping other utilities availability checks\n"
}

if {$install_compatversion eq "y"} {
   if {$verbose > 0} {
      send_user "\tChecking switchml availability\n"
   }
   foreach shell $shell_list {
      switch -- $shell {
         sh - bash - ksh - zsh {
            testall_cmd_re "$shell" "type switchml" "switchml is a.*" {} 0
         }
         csh - tcsh {
            testall_cmd_re "$shell" "alias switchml" ".+" {} 0
         }
      }
   }

   if {$verbose > 0} {
      send_user "\tChecking switchml functionality\n"
   }
   foreach shell $shell_list {
      if {$shell ne "fish"} {
         testall_cmd_re "$shell" "module --version; switchml; switchml; module --version;" "$swcompat_msg\n$swmain_msg" "$version_msg\n$version_msg" 0
      }
   }
   # set module compat version as default
   set env(MODULES_USE_COMPAT_VERSION) 1
   foreach shell $shell_list {
      if {$shell ne "fish"} {
         testall_cmd_re "$shell" "switchml; module --version; switchml; switchml; module --version;" "$swmain_msg\n$swcompat_msg\n$swmain_msg" "$version_msg\n$version_msg" 0
      }
   }
   unset env(MODULES_USE_COMPAT_VERSION)
} elseif {$verbose > 0} {
   send_user "\tSkipping switchml availability checks\n"
}

if {$install_modulepath ne ""} {
   if {$verbose > 0} {
      send_user "\tChecking default modulepath setup\n"
   }
   foreach shell $shell_list {
      testall_cmd_re "$shell" "module use" {} "$use_desc\n\\s+[join $modpath_listre {\n\\s+}]" 0
   }
   foreach shell $othlang_list {
      testall_cmd_re "$shell" "use" {} "$use_desc\n\\s+[join $modpath_listre {\n\\s+}]" 0
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping default modulepath checks\n"
}

if {$install_examplemodulefiles eq "y" && $modfilesdir_in_modpath} {
   if {$verbose > 0} {
      send_user "\tChecking example modulefiles installation\n"
   }
   set env(MODULES_TAG_ABBREV) {loaded=L}
   foreach shell $shell_list {
      testall_cmd_re "$shell" "module avail -t" {} "$moddir_header\n$mod_examples" 0
   }
   foreach shell $othlang_list {
      testall_cmd_re "$shell" "avail,-t" {} "$moddir_header\n$mod_examples" 0
   }
   unset env(MODULES_TAG_ABBREV)
   # check version-specific modulefile, related to versioning installation mode
   if {$install_versioning eq "y"} {
      foreach shell $shell_list {
         testall_cmd_re "$shell" "module load modules; module avail -t $install_version" {} "$versdir_header\n$install_versionre" 0
      }
      foreach shell $othlang_list {
         testall_cmd_re "$shell" "load,modules:avail,-t,$install_version" {} "$versdir_header\n$install_versionre" 0
      }
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping example modulefiles installation checks\n"
}

if {$install_loadedmodules ne ""} {
   if {$verbose > 0} {
      send_user "\tChecking default loaded modules setup\n"
   }
   foreach shell $shell_list {
      testall_cmd_re "$shell" "module list -t" {} "$cur_loaded\n[join $lm_list {.*\n}]" 0
   }
   foreach shell $othlang_list {
      testall_cmd_re "$shell" "list,-t" {} "$cur_loaded\n[join $lm_list {.*\n}]" 0
   }
} elseif {$verbose > 0} {
   send_user "\tSkipping default loaded modules checks\n"
}

# basic test for MODULES_SILENT_SHELL_DEBUG, checking module answer is still present
# among the debugging output
if {$install_silentshdbgsupport eq y} {
if {$verbose > 0} {
   send_user "\tChecking MODULES_SILENT_SHELL_DEBUG\n"
   send_user "\tSetup MODULES_SILENT_SHELL_DEBUG = '1'\n"
}
set env(MODULES_SILENT_SHELL_DEBUG) 1

foreach shell $shell_sh_family {
   if {[info exists shell_opts($shell)]} {
      set shell_opts_save $shell_opts($shell)
      set shell_opts($shell) "$shell_opts($shell)xv"
   } else {
      set shell_opts($shell) "-xv"
   }
   testall_cmd_re "$shell" "module --version" {} ".*Modules Release \\S+ (\\S+).*" 0
   if {[info exists shell_opts_save]} {
      set shell_opts($shell) $shell_opts_save
      unset shell_opts_save
   } else {
      unset shell_opts($shell)
   }
}

if {$verbose > 0} {
   send_user "\tSetup MODULES_SILENT_SHELL_DEBUG = '0'\n"
}
set env(MODULES_SILENT_SHELL_DEBUG) 0

foreach shell $shell_sh_family {
   if {[info exists shell_opts($shell)]} {
      set shell_opts_save $shell_opts($shell)
      set shell_opts($shell) "$shell_opts($shell)xv"
   } else {
      set shell_opts($shell) "-xv"
   }
   testall_cmd_re "$shell" "module --version" {} ".*Modules Release \\S+ (\\S+).*" 0
   if {[info exists shell_opts_save]} {
      set shell_opts($shell) $shell_opts_save
      unset shell_opts_save
   } else {
      unset shell_opts($shell)
   }
}

unset env(MODULES_SILENT_SHELL_DEBUG)
} elseif {$verbose > 0} {
   send_user "\tSkipping silent shell debug checks\n"
}

# test Windows-specific install
if {$verbose > 0} {
   send_user "\tChecking Windows-specific files availability\n"
}
testall_cmd_re "sh" "test -e $install_initdir/cmd.cmd" "" "" [expr {$install_windowssupport ne {y}}]
testall_cmd_re "sh" "test -e $install_bindir/module.cmd" "" "" [expr {$install_windowssupport ne {y}}]
testall_cmd_re "sh" "test -e $install_bindir/ml.cmd" "" "" [expr {$install_windowssupport ne {y}}]
testall_cmd_re "sh" "test -e $install_bindir/envml.cmd" "" "" [expr {$install_windowssupport ne {y}}]


#
#  Clean up variables used in this test case
#

reset_test_env

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