
$libraryName = shift;
@ofiles      = @ARGV;

# Should probably write a 'usage' function later
$libraryName || die "Must pass a library name!\n";
$#ofiles     || die "Must pass a list of ofiles!\n";

$libraryName =~ s/\.framework$//;

open(SYMBOLS, "link -dump -symbols " . join(" ", @ofiles). "|") || die "$!: Cannot run the dump symbols command!\n";

$firstTime = 1;

while (<SYMBOLS>) {

    next unless /SECT.*External.*\|/;
    next if /__GLOBAL_\$I|_OBJC_/;

    next unless /^\S+\s+\S+\s+(\S+).*\| (.*)$/;
    $section = $1;
    $symbol = $2;

    # Get rid of leading underscores
    $symbol =~ s/^_//;

    if ($firstTime) {
	print "LIBRARY $libraryName.dll\n";
	print "EXPORTS\n";
	$firstTime = 0;
    }

    next if $foundSymbol{$symbol};
    $foundSymbol{$symbol} = 1;

    if ($section eq "SECT1" && $symbol =~ /^\.objc_c/) {
        print "\t$symbol CONSTANT\n";
    } elsif ($section eq "SECT1" && $symbol !~ /^\.objc_c/) {
        print "\t$symbol\n";
    }
}
