dotfiles/.bash_profile

175 lines
5.8 KiB
Bash

# Souce 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 MANPAGER="less -Mis -PM'Page %dt$'"
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 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 MANPATH="/opt/local/share/man:$MANPATH"
export PATH="/opt/local/sbin:/opt/local/bin:$PATH"
else
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2
fi
unset _PLATFORM
# 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 [[ -z "$SSH_AUTH_SOCK" ]]; then
# Try to find an existing agent socket to connect to.
SSH_AUTH_SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
# Found a socket.
export SSH_AUTH_SOCK
echo "Connected to ssh-agent."
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')"
ssh-add -l >/dev/null 2>&1
if (( $? < 2 )); then
# Agent started - add new socket to the agent sockets file.
if [[ "$_PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
_GOT_LOCK=1
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
_GOT_LOCK=1
break
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping clean up of agents file!$(tput op)"
fi
[[ "$_GOT_LOCK" == "1" ]] && {
if _push_agent_sock; then
echo "Started new ssh-agent."
else
echo "$(tput setaf 1 || tput AF 1)Started new ssh-agent, but failed to register socket!$(tput op)"
fi
}
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo "$(tput setaf 1 || tput AF 1)Failed to start new ssh-agent!$(tput op)"
unset SSH_AGENT_PID SSH_AUTH_SOCK
fi
fi
else
# Got a forwarded ssh-agent connection.
export SSH_AUTH_SOCK
ssh-add -l >/dev/null 2>&1
if (( $? < 2 )); then
# Agent is connected - add new socket to the agent sockets file.
if [[ "$_PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
_GOT_LOCK=1
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
_GOT_LOCK=1
break
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping update of agents file!$(tput op)"
fi
[[ "${_GOT_LOCK:-0}" == "1" ]] && {
if _push_agent_sock; then
echo "Connected to forwarded ssh-agent."
else
echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)"
fi
}
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo -e "$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)"
fi
fi
echo
}
unset I _GOT_LOCK
# Screen.
hash screen >/dev/null 2>&1 && {
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
}