# ***** 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 "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Verbosio Backport Script for XULWidgets.
#
# The Initial Developer of the Original Code is
# Alexander J. Vincent <ajvincent@gmail.com>.
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. 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 *****

# The goal of this script is to copy modified files back from the objdir into
# the srcdir, except for those which the srcdir's make-project.pl preprocessed
# on the way over here.  This is the "backport".
#
# This script will call on make-project to verify the copy went correctly.

use strict;
use warnings;

my $log = 1;
my $level = 0;

my ($backportExtensions, $backportExtensionsSet) = processParameters();

# Diagnostic logging: called at the beginning of a function, logs the function
# name and parameters, and increments the indent level.
sub enterLog($@)
{
  if ($log)
  {
    my ($f, @parms) = @_;
    $level++;
    my $format = "%" . $level * 2 . "s Enter %s";
    printf ($format, " ", $f);
    while (@parms)
    {
      print " ";
      my $parm = shift (@parms);
      print ($parm);
    }
    print "\n\n";
  }
}

# Diagnostic logging: called in the middle of functions, logs the data at the 
# current indent level.
sub printLog($)
{
  if ($log)
  {
    my ($arg) = @_;
    my $format = "%" . $level * 2 . "s %s\n";
    printf ($format, " ", $arg);
  }
}

# Diagnostic logging: called at the end of functions, decrements the indent.
sub exitLog
{
  if ($log)
  {
    my ($f, @parms) = @_;
    my $format = "%" . $level * 2 . "s Exit %s";
    printf ($format, " ", $f);
    while (@parms)
    {
      print " ";
      my $parm = shift (@parms);
      print ($parm);
    }
    print "\n\n";
    $level--;
  }
}

# User documentation of command line parameters.
# Note: format to 80 character line length in cygwin shell window.
sub instructions {
print <<"Instructions";

usage: backport [--extensions=[all|none]]

Instructions
exit;
}

# Determine what to build: start with environment variables, override them with
# the command line parameters.
sub processParameters
{
  enterLog("processParameters", @_);

  # defaults
  my $backportExtensionsSet = 0;
  my $backportExtensions    = 0;
  while (@ARGV)
  {
    my $param = shift (@ARGV);
    if ($param eq "--help")
    {
      instructions();
    }
    if ($param eq "--extensions=all")
    {
      if ($backportExtensionsSet)
      {
        die("backport.pl aborted: You can't use set --extensions twice!");
      }
      $backportExtensions = "all";
      $backportExtensionsSet = 1;
      print "--extensions=all recognized\n";
    }
    elsif ($param eq "--extensions=none")
    {
      if ($backportExtensionsSet)
      {
        die("backport.pl aborted: You can't use set --extensions twice!");
      }
      $backportExtensions = "none";
      $backportExtensionsSet = 1;
      print "--extensions=none recognized\n";
    }
    elsif ($param eq "--level")
    {
      $level = shift (@ARGV);
      print "--level $level accepted\n";
      $level--;
      print "--level dropped to $level\n";
    }
  }

  exitLog("processParameters", @_);

  return ($backportExtensions, $backportExtensionsSet);
}

sub executeCommands($)
{
  enterLog("executeCommands", @_);
  my ($cmdSource) = @_;
  # No one exits this command in a different directory than they started.
  my $pwd = `pwd`;

  my @cmdArray = split(/\n/, $cmdSource);
  my $cmd;
  foreach $cmd (@cmdArray) {
    print $cmd . "\n";

    # blank lines
    if (!($cmd =~ /\S/))
    {
      next;
    }

    # comments
    # XXX ajvincent We leave the possibility for ifdefs by requiring a space
    #     after the #.
    if ($cmd =~ /^# /)
    {
      next;
    }

    # changing directories
    if ($cmd =~ /^cd /)
    {
      $cmd =~ s/^cd //;
      my $dir = $cmd;
      if (index($cmd, "/") != 0)
      {
        $dir = `pwd`;
        $dir =~ s/\n//;
        $dir .= "/$cmd";
      }
      chdir $dir;
      next;
    }

    my $cmd_status = system($cmd);
    if ($cmd_status != 0)
    {
      exit 2;
    }
  }
  my $nowPwd = `pwd`;
  if ($pwd ne $nowPwd)
  {
    die("executeCommand called without returning to the original directory!");
  }
  exitLog("executeCommands", @_);
}

sub ensureSourceDir($$)
{
  enterLog("ensureSourceDir", @_);
  my ($srcdir, $subdir) = @_;

  chomp($srcdir);
  chomp($subdir);
  if (!(-d "$srcdir/$subdir"))
  {
    executeCommands("mkdir -pv $srcdir/$subdir\n");
  }

  exitLog("ensureSourceDir", @_);
}


sub backport($$$$)
{
  enterLog("backport", @_);
  my ($srcdir, $title, $xpfe, $time) = @_;

#  backportChrome($srcdir, $title);

  exitLog("backport", @_);
}

