#!/usr/bin/perl -w
#
# Bill Adams (bill@evil.nwpacfica.net)
#
# 26 Apr  1998  baa  Created Script.
# 30 Juli 1998  Hans de Goede, removed malloc stuff, beware this is my first
#               perl coding attempt ever ;)
# 1 September 1998 Hans de Goede removed makefile.objs generation, it breaks
#               With newer mame versions ;( looking for a replacement.
use strict;

my $slash = "/";        #For generating paths.

print "Getting includes.\n";

#Save the top-level directory.  
if (-d 'src') {chdir ('src')}  #Go into the src directory.
my $pwd = `pwd`; chomp($pwd);

#Be sure that we made it into the top-level source directory.
if (not -f 'mame.h') {die "Error: No mame.h in $ENV{PWD}\n"}

my $dir;

print "Getting a list of include files...\n";
my (@include_dirs, %lcase_dirs);
push (@include_dirs, '.');
foreach $dir (<*>) {
    next if (not -d $dir or $dir eq '..');
    push (@include_dirs, $dir);
    
    #Make it so we can to case-insensitive searches.
    my $lcase_dir = $dir;
    $lcase_dir =~ tr/[A-Z]/[a-z]/;
    $lcase_dirs{$lcase_dir} = $dir;
} 

print "Checking/Fixing all #include's\n";

my (@object_dirs);
foreach my $dir (<*>) {
    next if (not -d $dir or $dir eq '.' or $dir eq '..');
    push (@object_dirs, $dir);
    next if ($dir eq 'msdos'); #No osd stuff
    print "Working on $dir\n";
    my $count = 0;
    foreach my $file (<$dir$slash*.[ch]>) {
	#This way of reading directories can cause problems if there are a
	#  very large number of files in the directory.  Should be replaced
	#  with an opendir/readdir call.
	next if not -f $file;  #Just to be sure.
	
	#Verify all of the includes have the correct case in the name. 
	print "\tChecking/Fixing $file->";
	&check_includes ($file);
	print "ok.\n";
    }
    chdir ($pwd) or die $!;
}

my $count = 0;
foreach my $file (<*.[ch]>) {
    print "\tChecking/Fixing $file->";
    &check_includes ($file);
    print "Ok\n";
}

print "Done Checking/Fixing\n";
exit;


sub check_includes {
    my ($old_file) = shift;
    my $tmp_file = $old_file.'.tmp';
    open (IN, $old_file) 
	or die "\n\tCould not open [$old_file] for reading.\n$!";
    open (OUT, '>'.$tmp_file) 
	or die "\n\tCould not open [$tmp_file] for writing.\n$!";
    #print "  Checking the includes of $old_file\n";
    my $line_number = 0;
    while (<IN>) {
	$line_number++;
	s/\r//g;
	if (m/(^\#include\s*\")(.*)\"/) {
	    #print "  Check include $2->";
	    if (-f $2) {
		print OUT; next;
	    } else {
		my $old_include = $_;
		chop($old_include);
		my $flag = 0;
		my $include_text = $1;
		my $include_file = $2;
		#print "Resolve->";
		my $orig_file = $2;
		#Check if there is a directory specified in the include.
		if ($2 =~ m/(.*)\/(.*)/) {
		    my $dir = $1;
		    my $file = $2;
		    if (not -d $dir) {
			my $lcase_dir = $dir; $lcase_dir =~ tr/[A-Z]/[a-z]/;
			if (not $lcase_dirs{$lcase_dir}) {
			    print "Warning: Could not resolve ".
				$include_file."\n";
			    exit;
			}
			$dir = $lcase_dirs{$lcase_dir};
		    }
		    $file = &find_file($dir, $file);
		    if ($file) {
			my $new_include = $include_text.$dir."/".$file.'"';
			print '  '.$old_file.": ".$old_include.
			    " should be \n\t".$new_include."\n";
			print OUT $new_include."\n";
			next;
		    } else {
			print "$old_file->Error: could not find $orig_file\n";
			exit;
			next;
		    }
		} else {
		    # Search all of the dirs for the file.	
		    my $file;
		    foreach my $dir (@include_dirs) {
			if (-f $dir."/".$include_file) {
			    #We found it without looking case-insensitvly.
			    $flag = 1;
			    print OUT;
			    last;
			} elsif (($file = &find_file($dir, $include_file))) {
			    #We found it with a different case.
			    my $new_include = $include_text.$file.'"';
			    $flag = 1;
			    print '  '.$old_file.": ".$old_include.
				" should be \n\t".$new_include."\n";
			    print OUT $new_include."\n"; 
			    last;
			}
			
		    }
		}
		if (not $flag) { 
		    print "  $old_file->Warn: $include_file does not exist.\n";
		    sleep (2);
		} else {
		    #print OUT;
		}
	    }
	} else {
	    print OUT;
	}
    }
    close IN;
    close OUT;
    rename $tmp_file, $old_file 
	or die "Error: Could not rename $tmp_file to $old_file\n$!";
    #sleep (1);
}


sub find_file {
    #Find a file in a give directory without regard to case.
    my $dir = shift;
    my $file = shift;
    $file =~ tr/[A-Z]/[a-z]/;  #Make the source file lowercase.
    foreach my $f (<$dir$slash*>) {
	$f =~ s/$dir$slash//;
	my $tmp_f = $f;           #Save the original file name.
	$f =~ tr/[A-Z]/[a-z]/;
	#print "  Compare $f to $file\n";
	if ($f eq $file) {
	    #print "Found $f in $dir [$tmp_f]\n";
	    return $tmp_f;
	}
    }
    return '';
}
