if ( $?histchars ) then
   set histchar   = `echo $histchars | cut -c1`
   set _histchars = $histchars
   set prefix     = 'unset histchars;'
   set postfix    = 'set histchars = $_histchars;'
else
   set histchar = \!
   set prefix   = ''
   set postfix  = ''
endif

if ( $?prompt ) then
   set prefix  = "$prefix"'set _prompt="$prompt";set prompt="";'
   set postfix = "$postfix"'set prompt="$_prompt";unset _prompt;'
endif

if ( $?noglob ) then
   set prefix  = "$prefix"'set noglob;'
   set postfix = "$postfix"'unset noglob;'
endif

set postfix = "set _exit="'$status'"; $postfix; test 0 = "'$_exit;'

# clarify compat version is not enabled for below tests
if ( ! $?MODULES_USE_COMPAT_VERSION ) then
   setenv MODULES_USE_COMPAT_VERSION 0
endif

# set module command in accordance with active version
if ( "$MODULES_USE_COMPAT_VERSION" == '1' ) then
   alias module $prefix'eval `/usr/lib/arm-linux-gnueabihf/modulecmd-compat csh '$histchar'*`; '$postfix
else
   alias module $prefix'eval `/usr/bin/tclsh /usr/lib/arm-linux-gnueabihf/modulecmd.tcl csh '$histchar'*`; '$postfix
endif
unset _histchar
unset prefix
unset postfix

# define function to switch between C and Tcl versions of Modules
if ( "`alias switchml`" == '' ) then
   alias switchml 'set swfound=1; \
      if ( ! $?MODULES_USE_COMPAT_VERSION ) setenv MODULES_USE_COMPAT_VERSION 0; \
      if ( "$MODULES_USE_COMPAT_VERSION" == "1" ) set swname="main"; \
      if ( "$MODULES_USE_COMPAT_VERSION" == "1" && -e /usr/lib/arm-linux-gnueabihf/modulecmd.tcl ) set swfound=0; \
      if ( "$MODULES_USE_COMPAT_VERSION" != "1" ) set swname="compatibility"; \
      if ( "$MODULES_USE_COMPAT_VERSION" != "1" && -e /usr/lib/arm-linux-gnueabihf/modulecmd-compat ) set swfound=0; \
      if ( $swfound == 0 && $swname == "main" ) setenv MODULES_USE_COMPAT_VERSION 0; \
      if ( $swfound == 0 && $swname == "compatibility" ) setenv MODULES_USE_COMPAT_VERSION 1; \
      if ( $swfound == 0 ) echo "Switching to Modules $swname version"; \
      if ( $swfound == 0 ) source /usr/init/csh; \
      if ( $swfound != 0 ) echo "Cannot switch to Modules $swname version, command not found"; \
      unset swfound swname;'
endif

if ( $?tcsh && $?prompt && -r /usr/init/tcsh_completion ) then
if ( "$MODULES_USE_COMPAT_VERSION" != '1' ) then
   source /usr/init/tcsh_completion
else
   # no completion support on compat version
   uncomplete module
endif
endif

# setup ENV variables to get module defined in sub-shells (works for 'sh'
# and 'ksh' in interactive mode and 'sh' (zsh-compat), 'bash' and 'ksh'
# (zsh-compat) in non-interactive mode.
setenv ENV /usr/init/profile.sh
setenv BASH_ENV /usr/init/bash

setenv MODULESHOME /usr

if ( ":${PATH}:" !~ '*:/usr/bin:*' ) then
   if ( "$PATH" == '' ) then
      setenv PATH /usr/bin
   else
      setenv PATH "/usr/bin:$PATH"
   endif
endif

if ( $?MANPATH ) then
   set manpath = $MANPATH
else
   set manpath = `manpath`
endif
if ( ":${manpath}:" !~ '*:/usr/share/man:*' ) then
   if ( "$manpath" == '' ) then
      setenv MANPATH /usr/share/man
   else
      setenv MANPATH "/usr/share/man:$manpath"
   endif
endif
unset manpath

if (! $?MODULEPATH ) then
   if ( -r /usr/init/.modulespath ) then
      setenv MODULEPATH `sed -n 's/[ 	#].*$//; /./H; $ { x; s/^\n//; s/\n/:/g; p; }' /usr/init/.modulespath`
   else
      setenv MODULEPATH ''
   endif
endif

if (! $?LOADEDMODULES ) then
   setenv LOADEDMODULES ''
endif

# load modulerc only if module environment is empty
if ( "$MODULES_USE_COMPAT_VERSION" != '1' && -r /usr/init/modulerc && "$MODULEPATH" == '' && "$LOADEDMODULES" == '' ) then
   module source /usr/init/modulerc
endif
