#! /usr/local/bin/perl

# Changes Makefile.in to work correctly with moc files. When called
# without parameters, automoc works recursively on all Makefile.in in
# and below the current subdirectory. When called with parameters,
# only these Makefile.in are changed.
# The Makefile.in should have an entry with the following syntax:
#
# {program}_METASOURCES = USE_AUTOMOC [{suffix}]
#
# Moc files will have a 'cpp' suffix unless overriden by the optional
# {suffix} parameter.
# Be careful not to use a suffix that isn't used by at least one
# of the main source files in {program}_SOURCES.
#
# 1998-07-23  Harri Porten  <porten@tu-harburg.de>
#       * small bugfixes for MOCSOURCES
# 1998-11-08  Alex Zepeda  <garbanzo@hooked.net>
#	* tweak it so it will run from a "common" directory.
# 1998-11-11  Harri Porten <porten@tu-harburg.de> & David Faure <faure@kde.org>
#       * bug fix for invocation with arguments
# 1998-11-16  Harri Porten <porten@kde.org>
#       * take care of "ar" object files, too.
#         (as proposed by Martin Vogt <mvogt@rhrk.uni-kl.de>)
# 1999-01-13  Harri Porten <porten@kde.org>
#       * implemented {suffix} parameter
# 1999-01-16  Stephan Kulow <coolo@kde.org>
#       * implemented support for included MOC files and command line 
#         parameters

use Cwd;
use File::Find;
use File::Basename;

# TRUE if in verbose mode.
$verbose = 0;

$direct_arguments = 0;

@arglist = @ARGV;

@input_files = ();

$errorflag = 0;

$metasource_variable = '';

$printname = '';

$lastdirname = '';

$srcdir = '$(srcdir)';

@headersourcedirs = ($srcdir);

while (@arglist) {
  if ($arglist[0] eq "--version")
    {
      print "\n\n";
      print "*********************************************\n";
      print "automoc - Software that makes even more sense\n";
      print "*********************************************\n\n";
      print "Welcome to the wonderful world of automoc!\n";
      print "This is really free software, unencumbered by the GPL.\n";
      print "You can do anything you like with it except sueing me.\n";
      print "Copyright 1998 Kalle Dalheimer <kalle\@kde.org>\n";
      print "Concept, design and unnecessary questions about Perl by Matthias Ettrich <ettrich\@kde.org>\n\n";

      exit 0;
    } 
  elsif ($arglist[0] eq '-verbose' || $arglist[0] eq '-v')
    {
      $verbose = 1;
    }
  else 
    {
      push (@input_files, $arglist[0]);
      $direct_arguments = 1;
    }

  shift @arglist;
}

if ($direct_arguments == 0)
  {
    find( \&add_makefile, cwd() );
  }

$curdir = cwd();
while( @input_files )
  {
    $filename = $input_files[0];
    process_makefile( $filenname );
    chdir ( $curdir);
    shift @input_files;
  }

sub add_makefile()
  {
	if( $_ ne "Makefile.in" )
	  {
		return;
	  }
	else
	  {
	    push (@input_files, $File::Find::name );
	  }
  }


sub process_automoc_makefile
  {
    	# find the name of the program
	$search_use_automoc[0] =~ /^([^=]*)_METASOURCES/; 
	$programname = $1;

        $cxx_suffix = "cpp";

        # check whether a suffix is given
	if( $search_use_automoc[0] =~ /USE_AUTOMOC\s+([^\s]+)/ )
          {
              $cxx_suffix = $1;
          }

	@mocable_files = `grep -l Q_OBJECT *.h 2> /dev/null`;

	foreach $mocable_file (@mocable_files) {
	  chomp $mocable_file;
	  $thisfile = $mocable_file;
	  $thisfile =~ s/\.h//;
	  push @mocnames, $thisfile;
	  $thisfile = $mocable_file;
	  $thisfile =~ s/\.h/\.moc\.$cxx_suffix/;
	  push @mocsources, $thisfile;
	}

	$objectline = $programname . "_OBJECTS";

	LINE: while( <FILEIN> )
	  {
                if( /^#/ ) {
		     print FILEOUT $_;
                     next LINE;
                }
			
		if( /(.*_METASOURCES\s*=\s*)(USE_AUTOMOC)/ )
		  {
		    print FILEOUT "$1 ";
		    foreach $mocsource (@mocsources) {
		      print FILEOUT $mocsource . " ";
		    }
		    print FILEOUT "\n";
		  }
		elsif( /^$objectline/ ) # look for programs and libraries
		  {
			chomp $_;
                        $line = $_;
                        $idx = rindex( $line, "\\");
                        $morelines = 0;
			# Found a backslash in the line
                        if($idx > 0) {
                          $line = substr( $line, 0, idx-1);
                          $morelines = 1;
                        }
			print FILEOUT "$line ";
			foreach $mocname (@mocnames) {
			  if ( $objectline !~ /_la_OBJECTS$/ ) {
			    print FILEOUT $mocname . ".moc.o ";
			  }
			  else  {
			    print FILEOUT $mocname . ".moc.lo ";
			  }
			}
                        if($morelines) {
                          print FILEOUT "\\";
                        }
			print FILEOUT "\n";
		  }
		else {
		  process_generell_rules($_);
		}
	      }
	
	print FILEOUT "\n\n";
	foreach $file (@mocnames) {
	  print FILEOUT "$file.moc.$cxx_suffix: \$(srcdir)/$file.h\n";
	  print FILEOUT "\t\$(MOC) \$(srcdir)/$file.h -o $file.moc.$cxx_suffix\n\n"
	}

  }

sub process_generell_rules
  {
    if ( m+cd \$\(top_srcdir\) \&\& \$\(AUTOMAKE\)+ )
      {
	print FILEOUT $_;
	print FILEOUT "\tcd \$(top_srcdir) && perl $0 $printname\n";
        return;
      }

    if (/^[^=]*DISTCLEANFILES.*=/) 
      {
	s/^.*DISTCLEANFILES.*=//;
	print FILEOUT "DISTCLEANFILES = \$(METASOURCES) ";
      }

    if (/^[^=]*META_INCLUDES.*=(.*)/)
      {
	chomp $1;
	@list = split(' ', $1);
	for $dir (@list) {
	  $realdir = $dir;
	  $realdir =~ s#\$\(srcdir\)#.#;
	    if (! -d $realdir) {
	      print STDERR "$printname: warning $dir can't be found. Must be a relative path to \$(srcdir) $realdir\n";
	    } else {
	      if ($dir !~ /$srcdir/) 
		{
		  $dir = $srcdir . "/" . $dir; 
		}
	      push @headersourcedirs, $dir;
	    }
	}
      }
    
    print FILEOUT $_;
    
  }

sub process_moc_makefile() 
  {
    local (@mocnames) = ();
    local ($saw_bk) = 0;

    LINE: while( <FILEIN> )
      {
        if ( /^#/ ) {
		print FILEOUT $_;
		next LINE;
	}
 
	$textline = $_;
	
	if ($textline =~ /^[^=]*METASOURCES.*/ || $saw_bk)
	  {
	    $saw_bk = /\\$/;
	    $line=$textline;
	    $line =~ s/\\$//;
	    $line =~ s/.*METASOURCES.*=[ ]*//;
	    @list = split(' ', $line);
	    foreach $mocname (@list) {
	      if ($mocname !~ /\.moc$/) {
		print STDERR "error: filename $lastdirname/$mocname doesn't end with .moc\n";
                $errorflag = 1;
	      } else {
		$mocname =~ s/\.moc$//;
		push(@mocnames, $mocname);
	      }
	    }

	  } else {
            $saw_bk = 0;
          }
	
	process_generell_rules($test_line);
      }

    if (@mocnames) {
      print FILEOUT "\n\n";
    }

    foreach $mocfile (@mocnames) {
      
      print STDERR "Looking for moc file $mocfile.moc\n" if $verbose;
      
      $found = 0;
      
      for $dir (@headersourcedirs) {
	
	$realdir = $dir;
	$realdir =~ s/\$\(srcdir\)/./;
	  
	if (-f $realdir . '/' . $mocfile . '.h') 
	  {
	    $sourcedir = $dir;
	    $found = 1;
	    break;
	  }
	
      }
      
      if ($found == 0) {
	print STDERR "error: no header file found for mocfile $lastdirname/$mocfile.moc\n";
        $errorflag = 1;
      }

      @sourcenames=`grep -l "^#include .$mocfile\.moc." *.cpp *.cc *.cxx *.C 2> /dev/null`;

      if (@sourcenames == 1) {
	$sourcename = $sourcenames[0];
	$sourcename =~ s/\n$//;
      } else {
	print STDERR "error: no source file found for mocfile $lastdirname/$mocfile.moc\n";
	$errorflag = 1;
      }
      
      print FILEOUT "\$(srcdir)/$sourcename: $mocfile.moc\n";
      print FILEOUT "$mocfile.moc: $sourcedir/$mocfile.h\n";
      print FILEOUT "\t\$(MOC) $sourcedir/$mocfile.h -o $mocfile.moc\n";
      print FILEOUT "\n";

    }
   
  }

sub process_makefile
  {
    
    $printname = $filename;
    $printname =~ s#^$curdir/##;
    $lastdirname = dirname($filename);
    $lastdirname =~ s#^$curdir/##;

    print STDERR "Processing " .$printname . "\n" if $verbose;
    
    chdir( dirname( $filename ) );
    
    $filename = basename($filename);
    
    $search = `grep '[^=#]*METASOURCES.*=' $filename`;
    return if ($search eq '');
    
    $search = `grep "DO_NOT_USE_AUTOMOC" $filename`;
    return if ($search);
    
    $newfilename = $filename . ".tmp";

    open( FILEIN, "$filename" ) or die "Could not open $filename: $!\n";
    open( FILEOUT, ">$newfilename" ) or die "Could not open $filename: $!\n"; 
    
    # search for USE_AUTOMOC
    @search_use_automoc = `grep "[^#]*USE_AUTOMOC" $filename`;
    
    if( @search_use_automoc == 0 ) {
      process_moc_makefile();
    } else {
      process_automoc_makefile();
    }
    
    if ($errorflag == 0) {
      print FILEOUT "\n# DO_NOT_USE_AUTOMOC\n";
      rename $newfilename, $filename;
    } else {
      system("rm $newfilename");
    }

    undef @mocsources;
    undef @mocnames;
    
   
  }


