+#! /bin/sh
+
+set -e
+usage="Usage: $0 [-bc] [COMMAND [ARGS...]]"
+
+### Parse options
+case "${SHELL-/bin/sh}" in
+ *csh*) style=csh;;
+ *) style=sh;;
+esac
+force= chosen= commands=
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -h | --help) echo "$usage"; exit 0;;
+ -c | --cshell | --tcsh) style=csh chosen=t;;
+ -b | --bourne | --bash | --zsh) style=sh chosen=t;;
+ -f | --force) force=t;;
+ --) shift; break;;
+ -*) echo >&2 "$usage"; exit 1;;
+ *) break;;
+ esac
+ shift
+done
+[ $# -gt 0 ] && style=commands
+case $chosen,$style in
+ t,commands)
+ echo >&2 "$0: output style and commands? you're odd"
+ exit 1
+ ;;
+esac
+
+### Should I start a new agent?
+case "$force,$SSH_AUTH_SOCK" in
+ t,* | ,)
+ start=t
+ ;;
+ *)
+ start=
+ set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
+ [ $rc -ge 2 ] && start=t
+ ;;
+esac
+
+### If so, do that
+if [ "$start" ]; then
+ hostname=${HOST-$(hostname)}
+ user=${USER-${LOGNAME-$(whoami)}}
+ dir=$TMPDIR/.ssh-agent.$hostname.$user socket=$dir/sock pid=$dir/pid
+ mkdir -p -m700 "$dir"
+ SSH_AUTH_SOCK=$socket; export SSH_AUTH_SOCK
+ set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
+ if [ $rc -ge 2 ]; then
+ if [ -f "$pid" ]; then
+ kill $(cat "$pid") >/dev/null 2>&1 || :
+ fi
+ rm -f "$socket" "$pid"
+ (cd /; exec ssh-agent -d -a "$socket" >/dev/null 2>&1)&
+ echo $! >"$pid"
+ SSH_AUTH_SOCK=$socket; export SSH_AUTH_SOCK
+ fi
+fi
+
+### Run a program, or export the details
+case $style in
+ sh)
+ echo "SSH_AUTH_SOCK='$SSH_AUTH_SOCK'; export SSH_AUTH_SOCK"
+ ;;
+ csh)
+ echo "setenv SSH_AUTH_SOCK '$SSH_AUTH_SOCK'"
+ ;;
+ commands)
+ exec "$@"
+ ;;
+esac