#########################################
# CSHENV Environment, core loading file #
#########################################

# CSHENV Debugging tests
if( ( $?CSHENV_DEBUG ) || ( $?CSHENV_VERBOSE_STARTUP ) ) then
	echo "Currently running tcshrc script"
endif

set path=( ${CSHENV_DIR}/bin ${HOME}/.local/opt/bin ${path} )

setenv CSHENV_MACHINE_SUFFIX


# Figure out what kind of tcsh we are running.
if( $?tcsh ) then
	set tcsh_major=$tcsh:r:r
	set tcsh_minor=$tcsh:r:e
	set tcsh_patch=$tcsh:e
	set tcsh_features=$version:as/ /.:as/,/./

	if( $tcsh_minor < 12 ) then
		echo "We are on a very ancient TCSH!  There may be many problems..."
	else
		if( $tcsh_minor == 14 && $tcsh_patch < 2 ) then
			_debug "We may not have ADAM's patches..."
		endif
	endif

else
	# As we develop support for C++sh, we'll need this
	if( !$?cxxsh ) then
		echo "What!  We're not even in TCSH or something compatible, like C++sh!!!"
	endif
endif

set cur_feature=$tcsh_features:e

# TCSH Extended features configuration
set color
set noding
set padhour


_debug "TCSH Featureset probed..."


#The history size for your shell can be set globally now
if( $?CSHENV_HISTORY_SIZE ) then
	set history=${CSHENV_HISTORY_SIZE}
endif

_debug "Setting up optional sourcing mechanism."
# This safe_source alias is incredibly delicate.  It drives the whole
# optional sourcing mechanism -- DO NOT MESS WITH IT!
alias old_safe_source 'eval "if ( -f \!:1 ) then \\
source \!:1 \\
endif"'

alias new_safe_source "test -f \!:1 && source \!:1"

alias safe_source new_safe_source

_debug "Setting up optional session sourcing mechanism."
# These reload_session and `__load_session` aliases ar incredibly delicate.  They drives
# the whole optional session mechanism -- DO NOT MESS WITH IT LIGHTLY!
alias __load_session safe_source tcshrc.session.\$CSHENV_SESSION_PROFILE
_debug "Setting up optional session sourcing mechanism wrapper."
alias reload_session 'eval "if ( \$?CSHENV_SESSION_PROFILE ) then \\
__load_session \\
endif"'

_debug "Setting change session alias..."
alias change_session setenv CSHENV_SESSION_PROFILE \!:1\;__load_session


_debug "TCSH safe-source alias constructed..."


safe_source ${CSHENV_DIR}/tcshrc.conf

# Dispatch to find out what our OS is and any fixups
# This will figure out what system we're on and setup all
# system specific notions.  From the sourced file,
# tcshrc.system.???? will be called
_debug "Trying to source tcshrc.system"
source ${CSHENV_DIR}/tcshrc.system

#For GNU ls(1) {From coreutils} and tcsh's color ls-F
#Also, coming soon, my port of ls-F highlighting to FreeBSD's
#ls(1) -G facility
if( $?CSHENV_LINUX_SUCKS ) then
	setenv LS_COLORS 'no=00:fi=00:di=01;34:ln=01;35:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;41;37:ex=00;31:*.tar=01;31:*.tgz=01;31:*.tbz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;35:*.png=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.cpp=32:*.cc=32:*.c=32:*.l=32:*.cxx=32:*.hpp=33:*.h=33:*.i=33:*.a=31:*.asm=31:*.bin=35:*.sec=36:*.ko=31'
else
	setenv LS_COLORS 'no=00:fi=00:di=01;34:ln=01;35:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=01;05;41;37:ex=00;31:*.tar=01;31:*.tgz=01;31:*.tbz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.zst=01;31:*.lz4=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;35:*.png=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.cpp=32:*.cc=32:*.c=32:*.l=32:*.m=32:*.i=33:*.cxx=32:*.h=33:*.hpp=33:*.a=31:*.asm=31:*.bin=35:*.sec=36:*.ko=31'
endif

setenv CLICOLOR_FORCE

_debug "Trying to source tcshrc.${CSHENV_SYSTEM}"
safe_source ${CSHENV_DIR}/tcshrc.${CSHENV_SYSTEM}

# Having figured out the system, then we can load specific aliases

_debug "Trying to source tcshrc.aliases"
source ${CSHENV_DIR}/tcshrc.aliases

_debug "Trying to source tcshrc.${CSHENV_HOSTNAME}"
safe_source ${CSHENV_DIR}/tcshrc.${CSHENV_HOSTNAME}

if( $?CSHENV_ORGANIZATION ) then
    safe_source ${CSHENV_DIR}/tcshrc.${CSHENV_ORGANIZATION}
endif

_debug "Trying to source tcshrc.bindkeys"
source ${CSHENV_DIR}/tcshrc.bindkeys

set path=( ${path} /usr/sbin /sbin )

# TCSHRC Prompt is the LAST thing we do, since it's the entry to
# user interface

_debug "Trying to source tcshrc.prompt"
source ${CSHENV_DIR}/tcshrc.prompt

safe_source ${CSHENV_DIR}/tcshrc.user

_debug "Loading session, if one is set"
reload_session
_debug "Session loading done"


###########################
# vim: ft=tcsh
###########################
