#!/usr/bin/env perl

use strict;
use warnings;

use File::Slurp;

my $muslpath = "/usr/local/musl";
$muslpath = defined($ARGV[0]) ? $ARGV[0] : $muslpath;

my @muslobjs = qw(libc.a); #add missing object files here

die "need path to musl as ARGV0, if not in /usr/local/musl" unless(-e $muslpath);

my $url = "http://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html";
my @posixsite = `wget -q -O - $url`;
my %pxsyms;

for(@posixsite) {
	if (/href=\"\.\.\/functions\/(\w+)\.html/) {
		$pxsyms{$1} = 1;
	}
}

my @pxsyma;
push @pxsyma, $_ . "\n" for(keys(%pxsyms));
write_file "posix-syms.txt", @pxsyma;

my $cmd = "nm -g";
$cmd .= " $muslpath/lib/" . $_  for (@muslobjs);
my @tmuslsyms = `$cmd`;
my %muslsyms;
for(@tmuslsyms) {
	if(/\d+ T (\w+)/) {
		$muslsyms{$1} = 1;
	}
}

my @muslsyma;
push @muslsyma, $_ . "\n" for(keys(%muslsyms));
write_file "musl-syms.txt", @muslsyma;

print "symbols defined in posix, but not in musl:\n";
for(keys %pxsyms) {
	print $_, "\n" unless defined $muslsyms{$_};
}


