dotfiles/.bash_logout

71 lines
2 KiB
Bash

#!/bin/bash - not strictly necessary, but helps nano with syntax highlighting.
# Clear the screen/console on logout.
if (( $SHLVL == 10 )); then
if [[ -x /usr/bin/clear_console ]]; then
/usr/bin/clear_console -q
elif [[ -x /usr/bin/clear ]]; then
/usr/bin/clear
elif [[ -x /usr/bin/tput ]]; then
/usr/bin/tput clear
else
echo -ne "\E[2J"
fi
fi
hash ssh-add ssh-agent >/dev/null 2>&1 && {
# Determine the platform we're on.
_PLATFORM="$(uname -s)"
# Remove the SSH_AUTH_SOCK from the agents file.
[[ ! -z "$SSH_AUTH_SOCK" ]] && {
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)"
_SLEEP=3
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)"
_SLEEP=3
}
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping clean up of agents file!$(tput op)"
_SLEEP=3
fi
[[ "${_GOT_LOCK:-0}" == "1" ]] && {
_pop_agent_sock || {
echo -e "$(tput setaf 1 || tput AF 1)Failed to clean up agents file!$(tput op)"
_SLEEP=3
}
}
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
}
# Kill the ssh-agent.
(( $SHLVL == 1 )) && {
[[ ! -z "$SSH_AGENT_PID" ]] && {
ssh-agent -k >/dev/null 2>&1 || {
echo -e "$(tput setaf 1 || tput AF 1)Failed to kill ssh-agent!$(tput op)"
_SLEEP=3
}
}
}
}
sleep ${_SLEEP:-0}