cd ()
{
if [ -d "$@" ]; then
command cd "$@";
if [ -e "bin/activate" ]; then
if [ -n "${ACTIVATED}" ]; then
if [ "${ACTIVATED}" != "${PWD}" ]; then
if [[ -n "`type -t deactivate`" ]]; then
deactivate;
fi;
unset ACTIVATED;
else
return;
fi;
fi;
source "bin/activate";
export ACTIVATED="$PWD";
fi;
if [ -n "${ACTIVATED}" ]; then
if (( ! `expr match "${PWD}" "${ACTIVATED}"` )); then
if [[ -n "`type -t deactivate`" ]]; then
deactivate;
fi;
unset ACTIVATED;
fi;
fi;
else
if [ -e "$@" ]; then
command cd `dirname "$@"`;
else
if [[ "$@" == "^" && -n "${ACTIVATED}" ]]; then
command cd "${ACTIVATED}";
return;
fi;
command cd "$@";
fi;
fi
}