#!/bin/ksh
. $HOME/bin/stack

function pd {
  case $# in
  0)
    typeset X=$DIRSTACK
    pop DIRSTACK > /dev/null
    push DIRSTACK $PWD
    cd $X
    ;;
  1)
    typeset myPWD=$PWD
    case $1 in
      +[0-9]|+[0-9][0-9]|+[0-9][0-9][0-9])
        typeset INDEX=$((-1 $1))
        cd ${DIRSTACK[$INDEX]}
        DIRSTACK[$INDEX]=$myPWD
        ;;
      *)
        cd $1 && push DIRSTACK $myPWD
        ;;
    esac
    ;;
  *)
    echo "Usage: pd [directory]" 1>&2
    return
    ;;
  esac

  case $debug in
  on|true|1|yes)
    echo $PWD ${DIRSTACK[@]}
    ;;
  esac
}

function popd {
  typeset X=$DIRSTACK
  pop DIRSTACK > /dev/null
  cd $X
  case $debug in
  on|true|1|yes)
    echo $PWD ${DIRSTACK[@]}
    ;;
  esac
}

function dirs {
  echo $PWD ${DIRSTACK[@]}
}