make_agent_directory() {(
	umask 077
	mkdir -p "$agent_directory" &&
	chmod a+rwxt "$agent_directory"
);}

use_agent() {
	[ -s "$agent_file" ] &&
	. "$agent_file" >/dev/null &&
	[ -r "$SSH_AUTH_SOCK" ]
}

replace_agent() {
  ( umask 077
	kill_agent &&
	ssh-agent >"$agent_file" ) &&
	use_agent
}

give_key_only_if_needed() {( # because typing the passphrase is a waste of time ; not much fun!
        [ "`ssh-add -l`" = "The agent has no identities." ] && ssh-add
);}

anti_security_measures() {
	[ -n "$agent_directory" ] || agent_directory=/tmp/.sa
	[ -n "$agent_file" ] || agent_file="$agent_directory/`whoami`" 
	if [ -n "`glob "$HOME/.ssh/id*"`" ]
	then
		[ -d "$agent_directory" ] ||
			make_agent_directory || return
		use_agent ||
		replace_agent
		give_key_only_if_needed
	fi
}

kill_agent() {
	use_agent
	rm -f "$agent_file"
	kill $SSH_AGENT_PID 2>/dev/null
	SSH_AGENT_PID=
	SSH_AUTH_SOCK=
}

anti_security_measures
