#! /usr/bin/perl -w

# Copyright 2002 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_cleanup$%;
    require "$1/winapi/setup.pm";
}

use config qw($current_dir $wine_dir file_absolutize file_directory);
use output qw($output);
use winapi_cleanup_options qw($options);

use nativeapi qw($nativeapi);
use util qw(edit_file);

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

########################################################################
# cleanup_file

sub cleanup_file($$$) {
    local *IN = shift;
    local *OUT = shift;

    my $file = shift;
    my $dir = do {
	my $file2 = file_absolutize($file);
	my $dir = file_directory($file2);
	"$wine_dir/$dir";
    };

    my $indent;
    my @comments = ();
    my $format_comments = sub {
	local $_ = "";
	if ($#comments == 0) {
	    my $comment = $comments[0];

	    $_ = "$indent/*$comment */";
	} elsif ($#comments > 0) {
	    $_ = "$indent/*\n";
	    foreach my $comment (@comments) {
		$_ .= "$indent *$comment\n";
	    }
	    $_ .= "$indent */";
	}
	$indent = "";
	@comments = ();

	return $_;
    };

    my $in_comment = 0;
    my $modified = 0;
    while (<IN>) {
	chomp;

	if ($options->trailing_whitespace) {
	    s/(.*?)\s+$/$1/ && do { $modified = 1; };
	} else {
	    s/(.*?)\r$/$1/ && do { $modified = 1; };
	}

	if ($options->cpp_comments) {
	    if ($in_comment) {
		if(/^.*?\*\//) {
		    $in_comment = 0;
		}
	    } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\*(.*?)$/) {
		my $indent2 = $1;
		my $comment = $2;
		if($comment !~ /^.*?\*\//) {
		    $in_comment = 1;
		}
	    } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\/(.*?)\s*$/) {
		my $indent2 = $1;
		my $comment = $2;
		
		if ($indent2 =~ /^\s*$/) {
		    if (!$indent || $indent eq $indent2) {
			$indent = $indent2;
			push @comments, $comment;
			next;
		    } else {
			$_ .= "$indent2/*$comment */";
		    }
		} else {
		    my $comments = &$format_comments();
		    if ($comments) {
			$_  = "$comments\n$indent2/*$comment */";
		    } else {
			$_  = "$indent2/*$comment */";
		    }

		    $modified = 1;
		}
	    } else {
		my $comments = &$format_comments();
		if ($comments) {
		    $_  = "$comments\n$_";
		    $modified = 1;
		}
	    }
	}

	if ($options->include_quotes) {
	    my $module = "";
	    if ($dir =~ m%^$wine_dir/dlls/(.*?)/.*?$%) {
		$module = $1;
	    }

	    if (/^(\s*\#\s*include\s+)(?:<(.*?)>|\"(.*?)\")(.*?)\s*$/) {
		my $prefix = $1;
		my $header = $2 || $3;
		my $local = !defined($2) && defined($3);
		my $suffix = $4;

		my $exists_system = 0; {
		    my @system_paths = qw(/usr/include /usr/local/include /usr/X11R6/include);
		    foreach my $path (@system_paths) {
			$exists_system ||= (-e "$path/$header" || 0);
		    }
		    $exists_system ||= $nativeapi->is_conditional_header($header);
		}

		my $exists_windows = 0; {
		    if ($header !~ m%^wine/% && $header ne "config.h") {
			$exists_windows ||= (-e "$wine_dir/include/$header" || 0);
		    }
		}

		my $exists_wine = 0; {
		    $exists_wine ||= ($header eq "config.h");

		    if ($header =~ m%^wine/%) {
			$exists_wine ||= (-e "$wine_dir/include/$header" || 0);
		    }
		}

		my $exists_local = 0; {
		    $exists_local ||= ($header eq "y.tab.h");

		    my @local_paths = ();
		    push @local_paths, "$dir" if $dir !~ m%^$wine_dir/include%;
		    push @local_paths, "$wine_dir/dlls/$module" if $module;
		    foreach my $path (@local_paths) {
			$exists_local ||= (-e "$path/$header" || 0);
		    }
		}

		if (!$exists_system && !$exists_windows && !$exists_wine && !$exists_local) {
		    $output->write("warning: header '$header': doesn't exist\n");
		} elsif (($exists_system + $exists_windows + $exists_wine + $exists_local) > 1) {
		    if ($header !~ /^(?:async\.h|comcat\.h|sql(?:ext|types)?\.h|thread\.h|user\.h)$/) {
			$output->write("warning: header '$header': more than one possibillity\n");
		    }
		}

		if (!$local && $exists_local) {
		    $_ = "$prefix\"$header\"$suffix";
		    $modified = 1;
		} elsif ($local && !$exists_local && ($exists_system || $exists_windows || $exists_wine)) {
		    $_ = "$prefix<$header>$suffix";
		    $modified = 1;
		}
	    }
	}

	print OUT "$_\n";
    }

    my $comments = &$format_comments();
    if ($comments) {
	print OUT "$comments\n";
	$modified = 1;
    }

    return $modified;
}

########################################################################
# main

my @h_files = $options->h_files;
my @c_files = $options->c_files;

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

foreach my $file (@h_files) {
    $progress_current++;
    $output->progress("$file (file $progress_current of $progress_max)");
    $output->prefix("$file: ");

    if (edit_file($file, \&cleanup_file, @_, $file)) {
	$output->write("$file: modified\n");
    }
}

foreach my $file (@c_files) {
    $progress_current++;
    $output->progress("$file (file $progress_current of $progress_max)");
    $output->prefix("$file: ");

    if (edit_file($file, \&cleanup_file, $file)) {
	$output->write("$file: modified\n");
    }
}