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

sub chase_link 
{
    local ($lname, $prevdir) = @_;

    $prevdir = "." if ! $prevdir;

    local ($offset) = rindex ($lname, '/');
    local ($dir) = substr ($lname, 0, $offset);
    local ($basename) = substr ($lname, $offset + 1);


    print "dir = $dir\nbasename = $basename\n";

    $dir = "." if ! $dir;
    chdir $dir || die $!;

    if ($dir =~ m;^/;)
    {
	$prevdir = $dir;
    }
    else
    {
	$prevdir = sprintf ("%s/%s", $prevdir, $dir);
    }
    
    local ($realname) = readlink $basename;

    if (-l $realname)
    {
	&chase_link ($realname, $prevdir);
    }
    else
    {
	if ($realname =~ m;^/;)
	{
	    return $realname;
	}
	else
	{
	    return sprintf ("%s/%s", $prevdir, $realname);
	}				
    }
}

print &chase_link ($ARGV[0]), "\n";
