dotfiles/.bash_profile

157 lines
5 KiB
Bash

# Source bash specific set up,
[[ -f ~/.bashrc ]] && . ~/.bashrc
# Environment.
export EDITOR="nano"
export LANG="en_GB.UTF-8"
export LC_COLLATE="POSIX" # 'C' causes issues with some applications
# export LC_CTYPE="POSIX" # Not sure why I set this in the first place...
export LESS="-RMi -PM?f%F:stdin. -- Page %dt of %D -- %lt/%L (%Pt\%)$"
export PAGER="less"
export PATH="/usr/local/sbin:/usr/sbin:/opt/sbin:/sbin:$PATH"
export VISUAL="$EDITOR"
[[ -x /usr/bin/lesspipe ]] && eval "$(SHELL=/bin/sh lesspipe)"
# Platform specific set up.
_PLATFORM="$(uname -s)"
if [[ "$_PLATFORM" = "Linux" ]]; then
export I_WANT_A_BROKEN_PS=1
export LYNX_CFG="$HOME/.lynx.cfg"
export LYNX_LSS="$HOME/.lynx.lss"
export MANPAGER="less"
export MANPATH="$HOME/.local/share/man:$MANPATH"
hash dircolors >/dev/null 2>&1 && eval "$(dircolors -b ~/.dir_colors)"
elif [[ "$_PLATFORM" = "Darwin" ]]; then
export LSCOLORS="ExGxdxdxCxDxDxbcacbeae"
export MANPAGER="less -Mis -PM'Page %dt$'"
export MANPATH="/opt/local/share/man:$MANPATH"
export PATH="/opt/local/sbin:/opt/local/bin:$PATH"
else
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2
fi
# Add bin directories to PATH.
[[ -d "$HOME/files/bin" ]] && export PATH="$HOME/files/bin:$PATH"
[[ -d "$HOME/.local/bin" ]] && export PATH="$HOME/.local/bin:$PATH"
[[ -d "$HOME/bin" ]] && export PATH="$HOME/bin:$PATH"
# Make ssh-agent work better.
hash ssh-add ssh-agent >/dev/null 2>&1 && {
if [[ -e "$SSH_AUTH_SOCK" ]]; then
# Got a possible ssh-agent connection.
export SSH_AUTH_SOCK
_OUTPUT="$(ssh-add -l 2>&1 >/dev/null)"
if (( ${PIPESTATUS[0]} < 2 )) && [[ -z "$_OUTPUT" ]]; then
# Agent is connected.
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to ssh-agent."
else
echo -e "$(tput setaf 3)Connected to ssh-agent, but failed to register socket.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
# Agent connection failed.
SSH_AUTH_SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
# Found a new socket.
export SSH_AUTH_SOCK
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to alternate ssh-agent - you may need to re-add keys."
else
echo "$(tput setaf 3)Connected to, but failed to register, alternate ssh-agent - you may need to re-add keys.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo -e "$(tput setaf 1)Lost connection to ssh-agent - no alternate available!$(tput op)"
fi
fi
else
# No ssh-agent connection.
SSH_AUTH_SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
# Found a socket.
export SSH_AUTH_SOCK
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to ssh-agent."
else
echo "$(tput setaf 3)Connected to, but failed to register, ssh-agent.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
# No viable socket - try to start an agent.
eval "$(ssh-agent -s | grep -v 'echo')"
_OUTPUT="$(ssh-add -l 2>&1 >/dev/null)"
if (( ${PIPESTATUS[0]} < 2 )) && [[ -z "$_OUTPUT" ]]; then
# Agent started - register socket in the agents file.
_lock_agents_file && {
if _push_agent_sock; then
echo "Started new ssh-agent."
else
echo "$(tput setaf 3)Started, but failed to register, new ssh-agent$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo "$(tput setaf 1)Failed to start new ssh-agent!$(tput op)"
unset SSH_AGENT_PID SSH_AUTH_SOCK
fi
fi
fi
echo
}
unset _OUTPUT _PLATFORM
# Screen.
hash screen >/dev/null 2>&1 && {
# Reattach existing screens.
screen_attach
# Output some screen info.
export SCREENDIR="$HOME/.screen-${HOSTNAME%%.*}"
if [[ ! -e ~/.hushlogin ]]; then
if [[ ! -z "$STY" ]]; then
echo "Screen $STY, window $WINDOW."
echo
else
_SCREENS="$(screen -ls | grep '[[:alpha:]]' | egrep -v '^([[:digit:]]+|No) Socket(s)?')"
if [[ -n "$_SCREENS" ]]; then
echo "$_SCREENS"
echo
else
echo "No screens."
echo
fi
unset _SCREENS
fi
fi
}
# Mail check.
[[ ! -z "$SSH_CONNECTION" ]] && {
if [[ -s /var/spool/mail/$USER ]]; then
if [[ -N /var/spool/mail/$USER ]]; then
echo "You have new mail."
echo
else
echo "You have mail."
echo
fi
# else
# echo "No mail."
# echo
fi
}