##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.00-init/%M%
#   Revision:		%I%
#   First Edition:	2019/05/05
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, display
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Tests startup of the modulecmd script when access to its
#           external files is forbidden
#       }C%
#
##############################################################################

# cannot test access if cannot change file permission
if {!$is_file_perms_editable} {
    send_user "\tskipping access tests as file permissions cannot be changed\n"
# cannot test access if superuser privileges are in use
} elseif {$tcl_platform(user) eq "root"} {
    send_user "\tskipping access tests as tests are run by superuser\n"
# cannot test access if restricted file permissions have no effect
} elseif {$is_locked_dir_file_readable && $is_locked_file_readable} {
    send_user "\tskipping access tests as locked files or directories are still readable\n"
} else {

set ans [list]

if {[info exists tclextlib_file]} {
    # restrict read access to Tcl extension library
    change_file_perms $tclextlib_file ugo-rx
    lappend ans "DEBUG setState: cmdline set to '$MODULECMD sh --no-pager -D -V'"
    if {$::install_multilibsupport eq {y}} {
        lappend ans "DEBUG setState: machine set to '$::tcl_platform(machine)'"
    }
    lappend ans "DEBUG setConf: tcl_ext_lib set to '$tclextlib_file'"
    lappend ans "DEBUG setConf: siteconfig set to '$siteconfig_filere'(\\nDEBUG sourceSiteConfig: Source site configuration \\($siteconfig_filere\\))?(\\nDEBUG setState: siteconfig_loaded set to '1')?"
    lappend ans "DEBUG setConf: locked_configs set to '$install_lockedconfigs'"
    lappend ans "DEBUG setState: shell set to 'sh'"
    lappend ans "DEBUG setState: shelltype set to 'sh'"
    lappend ans "DEBUG setState: subcmd set to ''"
    lappend ans "DEBUG setState: subcmd_args set to ''"
    lappend ans "DEBUG setState: init_error_report set to '1'"
    lappend ans "DEBUG setConf: verbosity set to 'debug'"
    lappend ans "DEBUG setConf: term_background set to '$install_termbg'"
    lappend ans "(DEBUG initConfColors: Ignore invalid default.*\\n)?DEBUG setConf: colors set to '.*'"
    lappend ans "DEBUG setConf: color set to '0'"
    lappend ans "DEBUG setConf: pager set to '$install_pagercmd'"
    lappend ans "DEBUG setState: paginate set to '0'"
    lappend ans "DEBUG setState: report_format set to 'regular'"
    lappend ans "DEBUG setState: reportfd set to 'stderr'"
    lappend ans $vers_reportre
    testouterr_cmd_re sh {--no-pager -D -V} {} [join $ans \n]
    restore_file_perms $tclextlib_file

    # replace Tcl extension library by a non lib file
    file rename $tclextlib_file $tclextlib_file.orig
    file copy lib/envmodules.c $tclextlib_file
    set tclextlib_file_abs_re [regsub -all "\(\[.+?\]\)" [file normalize $tclextlib_file] {\\\1}]
    if {$tcl_platform(os) eq {Darwin}} {
        set tserr "$error_msgs: dlopen\\\($tclextlib_file_abs_re,.*"
    } else {
        set tserr "$error_msgs: couldn't load file \"$tclextlib_file_abs_re\":.*"
    }
    testouterr_cmd_re sh {-V} {} $tserr
    file delete $tclextlib_file
    file rename $tclextlib_file.orig $tclextlib_file
}

# restrict read access to site-specific configuration script
if {[file readable $siteconfig_file] && [file owned $siteconfig_file]} {
    change_file_perms $siteconfig_file ugo-rx
    testouterr_cmd_re sh {-V} {} $vers_reportre
    restore_file_perms $siteconfig_file
}

# restrict read access to additional site-specific configuration script
if {![is_config_locked extra_siteconfig]} {
    set addsiteconfig_file $env(TESTSUITEDIR)/example/siteconfig.tcl-2
    setenv_var MODULES_SITECONFIG $addsiteconfig_file
    change_file_perms $addsiteconfig_file ugo-rx
    testouterr_cmd_re sh {-V} {} $vers_reportre
    restore_file_perms $addsiteconfig_file
    unsetenv_var MODULES_SITECONFIG
}

# restrict read access to MODULERCFILE
set modulerc_file $env(TESTSUITEDIR)/etc/modulerc
setenv_var MODULERCFILE $modulerc_file
change_file_perms $modulerc_file ugo-rx
testouterr_cmd_re sh {list} {} $no_loaded
restore_file_perms $modulerc_file
setenv_var MODULERCFILE $ORIG_MODULERCFILE


#
# Cleanup
#

reset_test_env

}
