# Start ssh-agent if it is not already running. If it is running, ensure the
# environment variables are set properly.

# This is intended for sourcing within some startup file like .bashrc or .zshrc

sock_file="$HOME/.ssh-agent-sock"
pid_file="$HOME/.ssh-agent-pid"

touch $sock_file
touch $pid_file

if ps -p $(cat $pid_file) > /dev/null 2>&1; then
    export SSH_AUTH_SOCK="$(cat $sock_file)"
    export SSH_AGENT_PID="$(cat $pid_file)"
else
    # Start a new instance of the SSH agent.
    eval "$(ssh-agent)"

    # Record the new settings in the agent tracking files.
    echo $SSH_AUTH_SOCK > $sock_file
    echo $SSH_AGENT_PID > $pid_file
fi