#
# C Shell emulators -- makes it easier to get used to bash
#

alias rehash='hash -r'
alias which='type -path'
alias whereis='type -all'
alias unsetenv='unset'
alias env='declare -x | sed -e "s/^declare -x //"'
alias printenv='declare -x | sed -e "s/declare -x //"'
alias fun='declare -f | sed -e "s/^declare -f //"'

setenv()
{
    local var=$1;
    shift;
    export $var=$*;
}

set()
{
    if [ "$*" = "" ]
    then
        fun >  /tmp/set.$$
        env >> /tmp/set.$$
        declare | sed -e 's/=()/ ()/' >> /tmp/set.$$
        sort /tmp/set.$$ | uniq -u | egrep -v '^_|^ *declare \||^ *$'
        rm -f /tmp/set.$$
        return 0
    fi
    local var=$1;
    shift;
    if [ "$1" = '=' ]; then shift; fi
    echo "Var=$var"
    echo "Reset=$*"
    eval $var=$*
}

#
# Bash internal variables
#

export MAIL="/usr/spool/mail/$USER"

# CDPATH is used to tell cd where to look for "dir" when you type cd "dir"
CDPATH=.:~

HISTSIZE=1000
HISTFILESIZE=500
if [ "$UID" = '0' ]; then PS0="#"; else PS0="%"; fi
PS1="\n[\h!\u]\w  (\d \t)\n<$SHLVL:\!> $PS0 "
PS2=">> "

# This would log you out after 28800 seconds of inactivity
#TMOUT=28800

# This would cause bash to ignore (for filename completion purposes) any
# file ending in "~" 
#FIGNORE="~"

history_control="ignoredups"
nolinks="TRUE"
noclobber="TRUE"
auto_resume="TRUE"
cdable_vars="TRUE"

#
# Environment variables for other applications
#
setenv MANPATH   /usr/man:///////////////////////////////////////////////////////////////////////////usr/STAGE/man
setenv TEXINPUTS .:$HOME/tex/lib:///////////////////////////////////////////////////////////////////////////usr/STAGE/slib/tex/inputs
setenv EDITOR	 emacs
setenv VISUAL	 emacs
setenv PAGER	 less
setenv MORE	 "-l"
setenv LESS      "-i"

setenv PHONEMACHINE truffle # machine where .phonelist is stored

#
# Aliases to make life simpler
#
alias h='history 20'
alias hf='history | fgrep '             # look for a string in history
alias so='source'
alias po='popd'
alias pd='pushd'
alias d='dirs'
alias j='jobs -l'
alias a='alias'

alias ls='ls -C'
alias lf='ls -F'
alias lr='ls -R'
alias lt='ls -ltr'
if [ "$systype" = 'BSD' ]
then
    alias lg='ls -gal'
    alias ll='ls -galL'
    alias ln='ln -s'
else
    alias rsh=rcmd
    alias lg='ls -al'
    alias ll='ls -al'
    # use -v option when not using GNU df
    if [ "`type -path df`" = '/bin/df' ] || [ "`type -path df`" = '//bin/df' ]
    then
         alias df='df -v'
    fi
fi

alias eph='em ~/.phonelist'

alias em='emacsclient'
alias cls='clear'
alias more='less'
alias te='fg %telnet'

alias newph='emacs ~/.phonelist'
alias ph=phone

# show a directory sorted by file size
alias lss="ls -lr +sort=size " 

# list directories in this directory
lds() { ls -ld * | egrep "^d" }

# Ring the bell
bell()
{
    echo -n ""
}

# q <cmd> will kick off the command in the background and log everything to
# a file ending in .log~, ml will more the logs, rl will remove them, fo <cmd>
# will follow the log for a particular command.
# Example:
#       q make install test
#   This will run "make install test" in the background and generate a log
#   file named "make.log~"
alias ml="more *.log~"
alias rl="rm *.log~"
q()
{
    echo "# "`date` > $1.log~
    echo "% $*"     >> $1.log~
    ( nice -10 $* >> $1.log~ 2>&1 && bell ) || ( bell ; bell ; exit 1 ) &
}
fo()
{
    nice tail -${2}f $1.log~
}

# fgrep a phone list on this or another machine for the requested entry
phone()
{
    if test $PHONEMACHINE = `uname` ; then
       fgrep -i $* ~/.phonelist;
    else
       rsh $PHONEMACHINE fgrep -i $* \~/.phonelist;
    fi
}

# pipe a compressed file through zcat into less
zless()
{
    zcat $* | less
}

if [ "$henry" != "" -o "$EMACS" != "" ]
then
    setenv PAGER cat
fi

