#!/usr/bin/perl
$WIN32_EXCEPTION_CS="mcs/class/System/System.ComponentModel/Win32Exception.cs";
$MONO_IO_ERROR="mcs/class/corlib/System.IO/MonoIOError.cs";
$MONO_SRC_DIR="mono";

open(IN, "<$MONO_IO_ERROR");
while (<IN>)
{
    if (m/\s+([A-Z]\S+)\s+\=\s+(\d+)/)
    {
        my $id = $1;
        my $val = $2;

        $id2value{$id} = $val;
        $value2id{$val} = $id;
    };
};
close(IN);

$command = "grep SetLastError `find $MONO_SRC_DIR -name \'*.c\' -print`";
@files = `$command`;

for (@files)
{
    if (m/SetLastError\(([A-Z].*?)\)/)
    {
        my $id = $1;
        my $val = $id2value{$id} || die "Symbol $id not defined in $MONO_IO_ERROR";

        $used_id{$id} = 1;
        $used_val{$val} = 1;
    };
};

open(IN, "<$WIN32_EXCEPTION_CS");
while (<IN>)
{
    if (m/w32_errors\.Add\((\d+),/)
    {
        my $err = $1;

        $already_added{$err} = 1;
    };
};
close(IN);

for $val (keys %used_val)
{
    if (!$already_added{$val})
    {
#        print "val: $val " . $value2id{$val} . "\n";
         $id = $value2id{$val};

print qq!\t\t\tw32_errors.Add($val,\n!;
print qq!\t\t\t    Locale.GetText("$id"));\n!;
    };
};