#!/usr/bin/perl

# **** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "MPL"); you may not use this file
# except in compliance with the MPL. You may obtain a copy of
# the MPL at http://www.mozilla.org/MPL/
#
# Software distributed under the MPL is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the MPL for the specific language governing
# rights and limitations under the MPL.
#
# The Original Code is Enigmail.
#
# The Initial Developer of the Original Code is Patrick Brunschwig.
# Portions created by Patrick Brunschwig <patrick@mozilla-enigmail.org> are
# Copyright (C) 2005 Patrick Brunschwig. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# ***** END LICENSE BLOCK *****

# 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";
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 -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$//;

  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));
  }

  my $enigMakefile = $makefile;
  $enigMakefile =~ s/\.in$/.enig/;
  if (-e $enigMakefile) {
    $makefile = $enigMakefile;
  }
  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;
}
