#%Module

module-help {    version - initializes new version of the module command.}
module-help "    Version $::version"
module-help {}

module-whatis {initializes new version of the module command}

# for Tcl script use only
set version @VERSION@
set shell [module-info shell]

# get rid of old version
if {[module-info mode] in {load display}} {
    module unload --not-req null
    module unload --not-req modules
    setenv MODULE_VERSION @VERSION@
}

# version stack
prepend-path MODULE_VERSION_STACK @VERSION@

if {[module-info mode remove]} {
    # unload modules modulefile to get PATHs refreshed after previous module
    # command restoration
    module load modules
    set prevversion [lindex [split $env(MODULE_VERSION_STACK) :] 0]
    unsetenv MODULE_VERSION $prevversion

    # re-initialize previous module command version by sourcing its
    # initialization script, as module function definition is not compatible
    # between Modules 3 and Modules 4
    switch -- $shell {
        sh - bash - ksh - zsh - csh - tcsh - fish {
            set versionre [regsub -all "\(\[.+?\]\)" $version {\\\1}]
            regsub $versionre "@initdir@/$shell" $prevversion previnitshell
            if {[file exists $previnitshell]} {
                switch -- $shell {
                    sh - bash - ksh - zsh {
                        set sourcecmd .
                    }
                    csh - tcsh - fish {
                        set sourcecmd source
                    }
                }
                puts stdout "$sourcecmd $previnitshell"
                puts stdout {module load modules}
            } else {
                puts stderr "No initialization script for '$shell' on version '$prevversion'"
            }
        }
        default {
            # no support for re-initializing module command on scripting shell
            # as it is not a common practice for most of these languages to
            # redefine an already set function
            puts stderr "No re-initialization supported for '$shell' on version '$prevversion'"
        }
    }
}

# bring in new version
if {[module-info mode] in {load display}} {
    module load modules
    puts stdout [exec @TCLSH@ {@libexecdir@/modulecmd.tcl} $shell autoinit]
}
