#!/bin/csh
#
#
# \author mdejong
#
#--------------------------------------------------------------------------------------
#
#                   Utility script for csh library functions.
#
#--------------------------------------------------------------------------------------

setenv version 1.0


setenv     DEBUG 0                    # debug level

setenv     TIMER 0                    # timer

setenv     DEFAULT_OPTION -           # default option


# Wild card for any valid detector identifier or run number; to be used as ${ANY}.

setenv ANY_ID            "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
    
setenv PI                3.14159265359
setenv TIMESTAMP         "#splitline{}{#splitline{%d-%m-%y}{  %H:%M}}%F1970-01-01 00:00:00"
	    
    
#
# debug messages
#
alias fatal   'echo `date` FATAL   \!* ; exit'
alias error   'eval "if ( $DEBUG >= 0 ) then  \\
 echo `date` ERROR   \!*                      \\
 endif"'
alias notice  'eval "if ( $DEBUG >= 1 ) then  \\
 echo `date` STATUS  \!*                      \\
 endif"'
alias status  'eval "if ( $DEBUG >= 2 ) then  \\
 echo `date` STATUS  \!*                      \\
 endif"'
alias warning 'eval "if ( $DEBUG >= 2 ) then  \\
 echo `date` WARNING \!*                      \\
 endif"'
alias debug   'eval "if ( $DEBUG >= 3 ) then  \\
 echo `date` DEBUG   \!*                      \\
 endif"'


#
# Source including PATH. 
#
# \param  1       name
#
alias source 'eval                 \\
    "if ( \!:1 =~ */* ) then       \\
	unalias source             \\
        source \!:1 \!:2*          \\
    else                           \\
	unalias source             \\
	source `which \!:1` \!:2*  \\
    endif"'


#
# Method to print environment variables.
#
alias print_env 'eval setenv'


#
# Method to check for CC Lyon.
#
# \return         1 for CC Lyon; else 0
#
alias is_CCLyon 'eval                                             \\
    "if ( $?HOST != 0 && `echo $HOST | cut -c1-2` == cc ) then    \\
	echo 1                                                    \\
    else                                                          \\
	echo 0                                                    \\
    endif"'

#
# Method to define variable.
#
# \param  1       variable
#
alias define_variable 'eval                   \\
    "if (${?\!:1} == 0) then                  \\
    setenv \!:1                               \\
    endif"'


#
# Method to set variable.
# Note that a value equal to $DEFAULT_OPTION will not modify the variable.
#
# \param  1       variable
# \param  2-N     value(s)
#
alias set_variable 'eval                                \\
    "if ("\"\!:2*\"" != "\"$DEFAULT_OPTION\"") then     \\
    setenv \!:1 "\"\!:2*\""                             \\
    endif"'


#
# Method to locally set variable.
# Note that a value equal to $DEFAULT_OPTION will not modify the variable.
#
# \param  1       variable
# \param  2-N     value(s)
#
alias set_local_variable 'eval                          \\
    "if ("\"\!:2*\"" != "\"$DEFAULT_OPTION\"") then     \\
    set \!:1 "\"\!:2*\""                                \\
    endif"'


#
# Method to unset variable.
#
# \param  1       variable
#
alias unset_variable 'eval unsetenv \!:1'


#
# Method to print variables.
#
# \param  1-N     list of variables
#
alias print_variable 'eval                    \\
    "printf "%-20s\\ =\\ " \!:1               \\
    echo "${\!:1}"                            \\
    if (`echo \!:1* | wc -w` > 1) then        \\
    print_variable \!:2*                      \\
    endif"'


#
# Method to check validity of variables.
#
# \param  1-N     list of variables
#
alias check_variable 'eval                    \\
    "if (! ${?\!:1}) then                     \\
    fatal "Variable \!:1 not defined."        \\
    endif                                     \\
    if (`echo \!:1* | wc -w` > 1) then        \\
    check_variable \!:2*                      \\
    endif"'

    
#
# Method to set array.
#
# \param  1       array name
# \param  2-N     values
#
alias set_array 'eval "set \!:1=(\!:2*)"'


#
# Method to count directory in ':' separated path list.
#
# \param  1       path list
# \param  2       directory
#
alias count_directory 'eval "echo -n ${\!:1} | tr : \\n | grep "^\\\\\!:2\\\$" | wc -w"'

    
#
# Method to remove directory from ':' separated path list.
#
# \param  1       path list
# \param  2       directory
#
alias remove_directory 'eval "setenv \!:1 `echo -n ${\!:1} | tr : \\n | grep -v "^\\\\\!:2\\\$" | tr \\n : | sed "s/:\\\$//"`"'


#
# Method to remove variable from ':' separated path list.
#
# \param  1       path list
# \param  2       variable
#
alias remove_variable 'eval            \\
    "if (! ${?\!:2}) then              \\
    else                               \\
    remove_directory \!:1 "\${\!:2}"   \\
    endif"'
    

#
# Method to insert directory into ':' separated path list.
#
# \param  1       path list
# \param  2       directory
#
alias insert_directory 'eval                                                          \\
    "if (! ${?\!:1}) then                                                             \\
    setenv \!:1 \!:2                                                                  \\
    else if (`echo -n ${\!:1} | tr : \\n | grep "^\\\\\!:2\\\$" | wc -w` == 0) then   \\
    setenv \!:1 \!:2\:${\!:1}                                                         \\
    endif"'
