#!/usr/bin/perl -w
#
# Copywrite 2008 - Steven Rostedt
# Licensed under the terms of the GNU GPL License version 2
#
# 
my $linuxpath = ".";

my @makefiles = `find $linuxpath -name Makefile`;

my %objects;
my $var;
my $cont = 0;

foreach my $makefile (@makefiles) {
	chomp $makefile;
	
	open(MIN,$makefile) || die "Can't open $makefile";
	while (<MIN>) {
		my $catch = 0;
		
		if ($cont && /(\S.*)$/) {
			$objs = $1;
			$catch = 1;
		}
		$cont = 0;
		
		if (/obj-\$\((CONFIG_[^)]*)\)\s*[+:]?=\s*(.*)/) {
			$var = $1;
			$objs = $2;
			$catch = 1;
		}
		if ($catch) {
			if ($objs =~ m,(.*)\\$,) {
				$objs = $1;
				$cont = 1;
			}
			
			foreach my $obj (split /\s+/,$objs) {
				$obj =~ s/-/_/g;
				if ($obj =~ /(.*)\.o$/) {
					$objects{$1} = $var;
				}
			}
		}
	}
	close(MIN);
}

my @modules;

if ($#ARGV >= 0) {
	@modules = @ARGV;
} else {
	open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
	while (<LIN>) {
		next if (/^Module/);  # Skip the first line.
		if (/^(\S+)/) {
			$modules[$#modules+1] = $1;
		}
	}
	close (LIN);
}

my %configs;
foreach my $module (@modules) {
	if (defined($objects{$module})) {
		$configs{$objects{$module}} = $module;
		printf "%30s = %s\n", $module, $objects{$module};
	} else {
		print STDERR "$module config not found!!\n";
	}
}

