#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use vars qw/ %opt %libs @libs /;

sub usage
{
    print <<EOF
depwalk.pl [options] filename
EOF
    ;
    exit 1;
}


sub depwalk
{
    my $if= shift;
    my $handle;
    
    $libs{$if}= 1;
    push @libs, $if;
    
    open $handle, "ldd $if |" or die "Couldn't execute $!\nDo you have binutils installed?";
    while(defined(my $line= <$handle>))
    {
    	if( $line=~ m/.*=> ([^ ]+)/ )
	{
	    if(!($libs{$1}))
	    {
		depwalk($1);
	    }
	    else
	    {
	    	$libs{$1}++;
	    }
	}    	
    }
    close $handle;
}

sub parse_versioninfo
{
    my $if= shift;
    my $version_section_found= 0;
    
    open my $handle, "readelf -V $if |" or die "Couldn't execute $!\nDo you have binutils installed?";
    
    foreach(<$handle>)
    {
    	if($version_section_found)
	{
	    if( $_=~ m/.*: CXXABI_(.*) Flags: (.*) Version: (.*)/ )
	    {
	    	print "C++ ABI $1 ";
	    }
	}
	
	elsif( $_=~ m/Version needs section '(.*)' contains ([\d]+) entries:/ )
	{
	    $version_section_found= 1;
	}
    }
    print "(no version info) " if(!$version_section_found);
}



    #~ getopt("o", \%opt);
    
    my $infile= $ARGV[0]? $ARGV[0]: usage();
    
    depwalk($infile);
    
    # show libraries 'in order of appearance'
    foreach(@libs)
    {
    	print "$_ ";
	#~ printf( " %d", $libs{$_} );
	parse_versioninfo($_);
	print "\n";
    }
    

