use Cwd;

sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

$cwd = getcwd();
@fl= ('/common/errors.hpp','/common/gettext.h','/lib/filter-c.cpp','/lib/word_list-c.cpp','/lib/info-c.cpp','/lib/mutable_container-c.cpp','/lib/error-c.cpp','/lib/document_checker-c.cpp','/lib/string_map-c.cpp','/lib/config-c.cpp','/lib/string_enumeration-c.cpp','/lib/can_have_error-c.cpp','/lib/string_list-c.cpp','/lib/speller-c.cpp','/lib/string_pair_enumeration-c.cpp');
@filelist = ();
foreach $flName (@fl){
    $tmp = scalar(grep(/.*$flName.*/,@filelist));
    print $flName."\n";
    print $tmp;
    if(scalar($tmp) ==0 ){
        push(@filelist,$cwd.$flName );
    }
}


open(FILE, "symbols.def")||die("Shit couldnt open symbols.def");
@symbols = <FILE>;
$symbols_length = scalar (@symbols);
close(FILE);


foreach(@filelist){
    $fileName = $_;
    open(FILE, $fileName )||die("File ".$fileName." not found");
    @content = <FILE>;
    unshift(@content,"#include \"aspell_export.h\"\n");
    close(FILE);
    for($i=0;$i<$symbols_length;++$i){
        $sym = trim($symbols[$i]);
        if( ! defined $sym ){
            delete $symbols[$i];
            next;
        }else{
            foreach $line (@content){
                if($line =~ m/^\s*extern "C".*$sym\s*\(.*\).*/ 
				|| $line =~ m/^\s*extern "C".*$sym;/){
                    print "Found Symbols: ". $symbols[$i]." in File: ".$fileName;
                    @sp = split(/\"C\"/, trim($line));
                    $tmp = $sp[0]."\"C\" ASPELL_EXPORT ".$sp[1];
                    $tmp = $tmp."\n";
                    
                    print "modified\n".$line."\nto\n".$tmp;
                    print $line
                    $line = $tmp;
                }
            }
        }
    }
    open(FILE, ">" ,$fileName );
    print FILE @content;
    close(FILE);
}

#foreach(@symbols){
#    if(defined $_){
#        print "\ncouldn't find ".$_; 
#    }
#}


