#!/usr/bin/perl -w

# Copyright 2001 Patrik Stridvall
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#

use strict;

BEGIN {
    $0 =~ m%^(.*?/?tools)/winapi/winapi_fixup$%;
    require "$1/winapi/setup.pm";
}

use config qw(
    files_filter files_skip
    $current_dir $wine_dir $winapi_dir
);
use output qw($output);
use winapi_fixup_options qw($options);

if($options->progress) {
    $output->enable_progress;
} else {
    $output->disable_progress;
}

use winapi_c_parser;
use c_parser;
use type;

use winapi_fixup_documentation qw(fixup_documentation);
use winapi_fixup_editor;
use winapi_fixup_statements qw(fixup_statements);

my @c_files = $options->c_files;
@c_files = files_skip(@c_files);
@c_files = files_filter("winelib", @c_files);

my $progress_output;
my $progress_current = 0;
my $progress_max = scalar(@c_files);

foreach my $file (@c_files) {
    my $editor = new winapi_fixup_editor($file);

    $progress_current++;
    $output->progress("$file (file $progress_current of $progress_max)");
    $output->prefix("$file:");

    {
	open(IN, "< $file") || die "Error: Can't open $file: $!\n";
	local $/ = undef;
	$_ = <IN>;
	close(IN);
    }

    my $max_line = 0;
    {
      local $_ = $_;
      while(s/^.*?\n//) { $max_line++; }
      if($_) { $max_line++; }
    }

    my $parser;
    if (1) {
	$parser = new c_parser($file);
    } else {
	$parser = new winapi_c_parser($file);
    }

    my $function;
    my $line;

    my $update_output = sub {
	my $progress = "";
	my $prefix = "";

	$progress .= "$file (file $progress_current of $progress_max)";
	$prefix .= "$file:";

	if(defined($function)) {
	    my $name = $function->name;
	    my $begin_line = $function->begin_line;
	    my $begin_column = $function->begin_column;

	    $progress .= ": function $name";
	    $prefix .= "$begin_line.$begin_column: function $name: ";
	}

	if(defined($line)) {
	    $progress .= ": line $line of $max_line";
	}

	$output->progress($progress);
	$output->prefix($prefix);
    };

    my $found_preprocessor = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $preprocessor = shift;

	# $output->write("$begin_line.$begin_column: preprocessor: $preprocessor\n");

	return 1;
    };

    $parser->set_found_preprocessor_callback($found_preprocessor);

    my $found_comment = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $comment = shift;

	# $output->write("$begin_line.$begin_column: comment: $comment\n");

	return 1;
    };

    $parser->set_found_comment_callback($found_comment);

    my $found_line = sub {
	$line = shift;
	# local $_ = shift;

	&$update_output;

	# $output->progress("$file: line $line of ?");
    };

    $parser->set_found_line_callback($found_line);

    my $found_declaration = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $end_line = shift;
	my $end_column = shift;
	my $declaration = shift;

	# $output->write("$begin_line.$begin_column-$end_line.$end_column: declaration: \\\n$declaration\n");

	return 1;
    };

    $parser->set_found_declaration_callback($found_declaration);

    my $found_function = sub {
	$function = shift;

	&$update_output;

	my $name = $function->name;
	my $begin_line = $function->begin_line;
	my $begin_column = $function->begin_column;
	my $end_line = $function->end_line;
	my $end_column = $function->end_column;

	if($options->documentation) {
	    # fixup_documentation($function, $editor);
	}

	if($options->statements) {
	    fixup_statements($function, $editor);
	}

	my $statements = $function->statements;
	if(!defined($statements)) {
	    $function = undef;
	    $output->prefix("$file: ");
	} else {
	    # $output->write("$begin_line.$begin_column-$end_line.$end_column: function $name\n");
	}

	return 0;
    };

    $parser->set_found_function_callback($found_function);

    my $found_variable = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $linkage = shift;
	my $type = shift;
	my $name = shift;

	# $output->write("$begin_line.$begin_column: $linkage $type $name = /* ... */\n");

	return 1;
    };

    $parser->set_found_variable_callback($found_variable);

    my $found_function_call = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $end_line = shift;
	my $end_column = shift;
	my $name = shift;
	my $arguments = shift;

	$output->write("$begin_line.$begin_column-$end_line.$end_column: $name(" . join(", ", @$arguments) . ")\n");

	return 1;
    };

    $parser->set_found_function_call_callback($found_function_call);

    {
	my $line = 1;
	my $column = 0;
	if(!$parser->parse_c_file(\$_, \$line, \$column)) {
	    $output->write("can't parse file\n");
	}
    }

    $output->prefix("");

    $editor->flush;
}