#!/afs/athena/contrib/perl/perl

sub print_path {
    local ($name, @path) = @_;
    print "$name is:\n";
    print join (" -> ", @path), "\n";
}

sub chase_link {
    local (@path) = @_;
    local (@savepath);
    chdir '/';
    shift (@path);
    while (@path) {
	local ($elem) = shift @path;
	print "elem = $elem\n";
#	push (@savepath, $elem);
#	&print_path ("savepath", @savepath);
	local ($new) = readlink ($elem);
	if (defined $new) {     # A symbolic link.
	    $new =~ s!^\./!!;
	    
	    # Prepend symbolic link to rest of path.
	    
	    unshift (@path, split (m;/;, $new));
	    
	    # Deal with special cases.
	    
	    if (@path && $path[0] eq '') {
		
		# Absolute path starts over.
		
		@savepath = ();
		chdir '/';
		shift @path;
		next;
	    }
	    
	    # Back up the tree as necessary.
	    
	    while ($path[0] eq '..') {
		chdir '..'
		    || die "\n\nCan't cd to ..: $!\n";
		shift @path;
	    }
	    &print_path ("path", @path);
	    &print_path ("savepath before", @savepath);
	    splice (@savepath, $#savepath, $#path + 1, @path); 
	    &print_path ("savepath after", @savepath);
	}
	else {                  # An ordinary directory.
	    if (@path) {
		chdir $elem
		    || die "\n\nCan't cd to $elem: $!\n";
		push (@savepath, $elem);
	    }
	}
    }
    return (@savepath);
}

print "> ";
while (<>)
{
    s/\s*$//;
    local (@path) = split (m;/;, $_);
    print join (" ==> ", &chase_link (@path)), "\n> ";
}
