#Date: Tue, 18 May 1999 18:26:54 -0400 (EDT)
#From: Ed Hynan <ehy@delphi.com>
#To: Michele Andreoli <andreoli@pisoft.it>

#### Begin etc/pushpop.sh ----
# pushpop.sh: push, pop, and dirs commands for
# Bourne-like shells without built-in versions.
# Source this in the current interactive shell;
# e.g. put ``. /etc/pushpop.sh'' in ~/.profile.
#
# Uses: sed, pwd, echo.
# $HOME must be defined.
#
# Flaws:
#  -  There must be one character that cannot appear in
#     directory names (other than '/') for use as separator.
#  -  The [ -d $foo ] test must succeed for symbolic links
#     to directories, or pushd will fail for those links.
#  -  Must maintain state in vulnerable shell variables.
#
# Distributed under the terms of the GNU GPL.
# Ed Hynan <ehynan@suffolk.lib.ny.us>
#

# directory pseudo-stack
__dstk=""
# set to a character that will not appear in directory names
__dsep='|'

dirs () {
  [ "n${__dstk}" = "n" ] &&{
    echo "The directory stack is empty" 1>&2
    return 1
  }
  echo "$__dstk" | sed 's;\'$__dsep'; <- ;g' 1>&2
  return 0
}

pushd () {
  [ "n$1" = "n" ] && set "$HOME"
  [ -d "$1" ] ||{ echo "$1 -- not a directory" 1>&2
    return 1
  }
  __tcwd="`pwd`"
  if [ "n$__dstk" = "n" ]; then
    __dstk="$__tcwd"
  else
    __dstk="${__tcwd}${__dsep}${__dstk}"
  fi
  cd "$1" && echo "`pwd` <- $__tcwd" 1>&2 || \
    (echo "Failed cd $1" 1>&2; exit 1)
  return $?
}

popd () {
  [ "n${__dstk}" = "n" ] &&{
    echo "The directory stack is empty." 1>&2
    return 1
  }
  __tcwd="`pwd`"
  __tifs="$IFS"
  IFS=$__dsep
  set ${__dstk}
  ___d="$1"
  shift
  __dstk="$1"
  shift > /dev/null 2>&1
  for __dd in ${1+"$@"}; do
    __dstk="${__dstk}${__dsep}${__dd}"
  done
  IFS="$__tifs"
  cd "$___d" && echo "$__tcwd -> $___d" 1>&2 || \
    (echo "Failed cd $___d" 1>&2; exit 1)
  return $?
}

#### End etc/pushpop.sh ----

