#!/usr/bin/perl
# makemake: script to generate Makefile from Makefile.in
#           -r:     process recursively all subdirectories
#           -o DIR: use DIR as MOZ_OBJDIR
# Usage: makemake [-o @TOPSRCDIR@/somedir] [-r]

my $makelist="./Makefile.in.ipc";
my $mozObjDir=".";
my $makefile;

my $cwd=`pwd`;
chomp($cwd);
my $topdir=$cwd;

while ( (length($topdir)>0) && ( basename($topdir) ne "mozilla" )) {
  $topdir=dirname($topdir);
}

if (open(MOZCONFIG, "$topdir/.mozconfig")) {
  while (my $l = <MOZCONFIG>) {
    chomp($l);
    if ($l =~ /^\s*mk_add_options\s+MOZ_OBJDIR\s*=/) {
      $l =~ s/(^\s*mk_add_options\s+MOZ_OBJDIR\s*=\s*)(.*)$/$2/;
      $mozObjDir = $l;
    }
  }
  close(MOZCONFIG);
}
else {
  print "INFO: no .mozconfig file found\n";
}


while ( $#ARGV >= 0 ) {
  if ($ARGV[0] eq "-o") {
    $mozObjDir=$ARGV[1];
    shift @ARGV;
  }
  if ($ARGV[0] eq "-r") {
    $makelist=`find . -name Makefile.in.ipc -print`;
    break;
  }
  shift @ARGV;
}


$mozObjDir =~ s/\@TOPSRCDIR\@/$topdir/;

if ($mozObjDir eq ".") {
  print "INFO: no MOZ_OBJDIR used\n";
}
else {
  print "INFO: using MOZ_OBJDIR=$mozObjDir\n\n";
}

foreach $makefile (split(/[ \n\r]+/, $makelist)) {
  $makefile =~ s/^\.\///;
  my $dir=dirname("$cwd/$makefile");

  my $wd=$dir;
  print "Processing: $wd\n";
  my $top_srcdir="";
  my $newMakefile = $makefile;
  $newMakefile =~ s/.in.ipc$//;

  if ($mozObjDir eq ".") {
    while ( (length($wd)>0) && (basename($wd) ne "mozilla") ) {
      if (length($top_srcdir) == 0) {
        $top_srcdir="..";
      }
      else {
        $top_srcdir="../$top_srcdir";
      }
      $wd=dirname($wd);
    }
    $srcdir=".";
  }
  else {
    while ( (length($wd)>0) && (basename($wd) ne "mozilla") ) {
      $wd=dirname($wd);
    }
    $top_srcdir=$wd;
    $srcdir=$dir;
    my $targetDir=$srcdir;
    $targetDir =~ s/$top_srcdir/$mozObjDir/x;
    system("mkdir -p $targetDir");
    $newMakefile=sprintf("%s/%s", $targetDir, basename($newMakefile));
  }
  
  open(INFILE, $makefile) || die ("cannot open input file '$makefile'\n");
  open(OUTFILE, ">$newMakefile") || die ("cannot open output file '$newMakefile'\n");
  
  my $line;
  while ($line = <INFILE>) {
    $line =~ s/\@srcdir\@/$srcdir/g;
    $line =~ s/\@top_srcdir\@/$top_srcdir/g;
    print OUTFILE $line;
  }
  close(INFILE);
  close(OUTFILE);
}

if ($mozObjDir eq ".") {
  print "Done\n\n";
}
else {
  my $newWd=$cwd;
  $newWd =~ s/$topdir/$mozObjDir/x;
  print "Done. The code can now be compiled from $newWd\n\n";
}

sub basename {
  my $fn=$_[0];
  $fn =~ s/^(.*)\/(.*)$/$2/;
  return $fn;
}

sub dirname {
  my $dn=$_[0];
  $dn =~ s/^(.*)\/(.*)$/$1/;
  return $dn;
}
