# -*- Perl -*-
eval "exec /usr/local/bin/perl -S $0 $*"
	if $running_under_some_shell;
;# pwd.pl - keeps track of current working directory in PWD environment var
;#
;# $Id: pwd.pl,v 1.2 1996/07/29 21:20:20 jfk Exp $
;#
;# Usage:
;#	require "pwd.pl";
;#	&initpwd;
;#	...
;#	&chdir($newdir);

package pwd;

sub main'initpwd {
    chop($ENV{'PWD'} = `pwd`);
}

sub main'chdir {
    local($newdir) = shift;
    if (chdir $newdir) {
	if ($newdir =~ m#^/#) {
	    $ENV{'PWD'} = $newdir;
	}
	else {
	    local(@curdir) = split(m#/#,$ENV{'PWD'});
	    @curdir = '' unless @curdir;
	    foreach $component (split(m#/#, $newdir)) {
		next if $component eq '.';
		pop(@curdir),next if $component eq '..';
		push(@curdir,$component);
	    }
	    $ENV{'PWD'} = join('/',@curdir) || '/';
	}
    }
    else {
	0;
    }
}

1;
