#!/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;
my $depth="";

open(MAKEFILE, $makelist) || die ("Could not open '$makelist'\n");
while (my $l = <MAKEFILE>) {
  chomp($l);
  if ($l =~ /^\s*DEPTH\s*=/) {
    $l =~ s/(^\s*DEPTH\s*=\s*)(.*)$/$2/;
    $depth = $l;
  }
}
close(MAKEFILE);

if ($depth eq "") {
  while ( (length($topdir)>0) && ( basename($topdir) ne "mozilla" )) {
    $topdir=dirname($topdir);
  }
}
else {
  chdir($depth) || die "Directory '$depth' not found\n";
  $topdir=`pwd`;
  chomp($topdir);
  chdir($cwd);
}
print "INFO: found toplevel source directory $topdir\n";

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 ".") {
    # no OBJDIR specified
    if ($depth eq "") {
      while ( (length($wd)>0) && (basename($wd) ne "mozilla") ) {
        if (length($top_srcdir) == 0) {
          $top_srcdir="..";
        }
        else {
          $top_srcdir="../$top_srcdir";
        }
        $wd=dirname($wd);
      }
    }
    else {
      $top_srcdir=$topdir;
    }
    $srcdir=".";
  }
  else {
    # using OBJDIR
    if ($depth eq "") {
      while ( (length($wd)>0) && (basename($wd) ne "mozilla") ) {
        $wd=dirname($wd);
      }
      $top_srcdir=$wd;
    }
    else {
      $top_srcdir=$topdir;
    }
    $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;
}
