########################################################################
#                                                                      #
#               This software is part of the ast package               #
#          Copyright (c) 1982-2012 AT&T Intellectual Property          #
#          Copyright (c) 2020-2023 Contributors to ksh 93u+m           #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 2.0                  #
#                                                                      #
#                A copy of the License is available at                 #
#      https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html      #
#         (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d)         #
#                                                                      #
#                  David Korn <dgk@research.att.com>                   #
#                  Martijn Dekker <martijn@inlv.org>                   #
#                                                                      #
########################################################################

# popd: pop the current directory from the directory stack
# and change to the next directory on the stack.
# For use in conjunction with 'cd', 'dirs', 'mcd', 'pushd'.

# Use cd wrapper from .../fun/cd
autoload cd

# Non-destructively initialize global parameters for directory stack.
# Default stack size is 32, or $CDSTACK if set before init.
integer _push_max=${_push_max:-${CDSTACK:-32}}
integer _push_top=${_push_top:-${CDSTACK:-32}}
typeset -a _push_stack

# Pops the top directory
function popd
{
	typeset dir
	if	((_push_top >= _push_max))
	then	print -u2 "$0: Nothing to pop."
		return 1
	fi
	case $1 in
	"")	dir=${_push_stack[_push_top]}
		# change ~ to $HOME
		case $dir in
		\~*)   dir=$HOME${dir#\~} ;;
		esac
		\command cd -- "$dir" || return
		;;
	+[1-9]|+[1-9][0-9])
		typeset savedir
		integer i=_push_top$1-1
		if	((i >= _push_max))
		then	print -u2 "$0: Directory stack not that deep."
			return 1
		fi
		while ((i > _push_top))
		do	_push_stack[i]=${_push_stack[i-1]}
			i=i-1
		done
		;;
	*)	print -u2 "$0: Bad directory."
		return 1
		;;
	esac
	unset '_push_stack[_push_top]'
	_push_top=_push_top+1
	dirs
}
