#!/bin/sh
#Afribin specific shell settings

if ! echo $PATH | \grep -q :/usr/local/afribin && [ -d /usr/local/afribin ] ; then PATH=$PATH:/usr/local/afribin ; fi
if ! echo $PATH | \grep -q :/usr/local/bin     && [ -d /usr/local/bin ]     ; then PATH=$PATH:/usr/local/bin ; fi
if ! echo $PATH | \grep -q :$HOME/bin          && [ -d $HOME/bin ]          ; then PATH=$PATH:$HOME/bin ; fi


# AgentForwarding considered harmful - this host is not trusted
\rm -f $SSH_AUTH_SOCK

# Squirrels keep nuts for winter
HISTSIZE=50000
HISTFILESIZE=50000
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "


# It's amazing how this is not there by default ...
shopt -s checkwinsize

# For Ryan
alias ryan="source /usr/local/afribin/ryan-scripts/bashrc"

if [ $REMOTEUSER ] ; then
        logger "SSH LOGIN: $REMOTEUSER logged in from $SSH_CONNECTION"
        if [ -f /usr/local/afribin/profiles.d/$REMOTEUSER.sh ] ; then
                . /usr/local/afribin/profiles.d/$REMOTEUSER.sh
        fi
fi

# Don't allow bare mysql or rm ...
#alias mysql="/usr/local/afribin/mysql"
alias rm="/usr/local/afribin/safe-rm -i"
[ -f /usr/local/apache/bin/apachectl ] && alias apachectl="/usr/local/apache/bin/apachectl"
#This sets paths required for any customised afrihost scripts

alias l='ls -alh'
alias lc='ls'
alias cd..='cd ..'
alias cd...='cd ../..'
alias grep='grep --color=auto'
alias goto='exec goto.py'

LESS="-i"
export LESS

# Nobody uses this, not so?
#RS="rsync -azvP --stats -e ssh"; export RS
#alias rs="$RS"

# Bash completion:
if [ "$BASH_VERSION" ] ; then
_service() {
    case $COMP_CWORD in
        1) COMPREPLY=( $(compgen -W "$(\ls /etc/init.d )" "${COMP_WORDS[COMP_CWORD]}") ) ;;
        2) COMPREPLY=( $(compgen -W "$(service ${COMP_WORDS[1]} 2>&1|grep -i usage|sed 's/.*{//;s/}.*//;s/|/ /g;')" "${COMP_WORDS[COMP_CWORD]}") ) ;;
        *) COMPREPLY=( $(compgen -fd "${COMP_WORDS[COMP_CWORD]}" ) );;
    esac
}
_frob() {
    case $COMP_CWORD in
        1) COMPREPLY=( $(compgen -W "$(frob complete)" "${COMP_WORDS[COMP_CWORD]}") ) ;;
        *) COMPREPLY=( $(compgen -fd "${COMP_WORDS[COMP_CWORD]}" ) );;
    esac
}
complete -F _service chkconfig service
complete -F _frob frob
fi
#cPanel Added local::lib -- BEGIN
LOCALLIBUSER=$USER
if [ -e "/usr/bin/whoami" ]; then
        LOCALLIBUSER=`/usr/bin/whoami`
fi
if [ "$LOCALLIBUSER" != "root" ]; then
    eval $(perl -Mlocal::lib)
fi
#cPanel Added local::lib -- END

# Define a timeout command for CentOS5, because timeout is cool:
[ -x /usr/bin/timeout ] || timeout() {
    SIGNAL=15
    while getopts "s:t:" YEAH ; do
        case "$YEAH" in
            s) SIGNAL="$OPTARG";;
            *) echo "Invalid option $YEAH"; return 1 ;;
        esac
    done
    shift $((OPTIND-1))

    sleep $1 & P1=$!
    CMD=$2
    shift 2
    "$CMD" "$@" & P2=$!
    if [ "$BASH_VERSION" ] && [ "${BASH_VERSION:0:1}" -ge 4 ]  ; then
        wait -n
        kill -$SIGNAL $(jobs -rp)
    else
        trap 'kill -$SIGNAL $P1 $P2 2> /dev/null' CHLD
        wait
    fi
}

# MailCheck for things 
alias mailcheck='/usr/local/afribin/maintenance/mailcheck'


# Clean tab title
function set_title() {
PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}\007"' ; HOSTNAME=`hostname -s`
}

