#
# Can't just do '#!/usr/local/bin/perl' since that won't work on NT.  Sigh.
#

push(@INC, "$ENV{OMNIMAKE_ROOT}/utilities");
require "arch.perl";

%shellArgs = 0;
$debug = 0;

$debug && print STDERR "Entering omnimake.perl\n";

#
# To ensure that related variables get propogated during recursive builds,
# we snork all environment variables starting with OMNIMAKE_ and OMNI_,
# and pass them on the $(MAKE) command-line.
#

foreach $key (keys(%ENV)) {
    if ($key =~ m/^OMNI_/) {
	$shellArgs{$key} = $ENV{$key};
    }
    if ($key =~ m/^OMNIMAKE_/) {
	$shellArgs{$key} = $ENV{$key};
    }
}

$shellArgs{LOCAL_MAKEFILEDIR} = $ENV{LOCAL_MAKEFILEDIR};

#
#  Set default values for required variables that weren't found
#

if (!$shellArgs{OMNIMAKE_ROOT}) {
    $USER = $ENV{USER};
    if ($winnt) {
        # Can't use &fatal() here since we get it from OMNIMAKE_ROOT
        print STDERR "You must set OMNIMAKE_ROOT on Windows NT!\n";
        exit(1);
    } else {
        $shellArgs{OMNIMAKE_ROOT} = "/build/$USER/OmniGroup";
    }
}

if (!$shellArgs{OMNIMAKE_BUILD_OUTPUT_ROOT}) {
    $shellArgs{OMNIMAKE_BUILD_OUTPUT_ROOT} = "/Cache/$ENV{USER}/BuildOutput";
}

if (!$shellArgs{OMNIMAKE_INSTALL_ROOT}) {
    $shellArgs{OMNIMAKE_INSTALL_ROOT} = "/Cache/$ENV{USER}/InstallRoot";
}

if (!$shellArgs{LOCAL_MAKEFILEDIR}) {
    $shellArgs{LOCAL_MAKEFILEDIR} = "$shellArgs{OMNIMAKE_ROOT}/Makefiles";
}

if ($winnt) {
    # The MicroSoft linker will need this
    $shellArgs{Lib} = $ENV{Lib};
}

# Testing
# default fastcp location for mach.

$shellArgs{"CLONEHDRS"} = "/usr/lib/fastcp";

# Override if on NT or Solaris.

if ($winnt) {
    $shellArgs{"CLONEHDRS"} = "$ENV{NEXT_ROOT}/NextDeveloper/Executables/fastcp";
}
if ($solaris) {
    $shellArgs{"CLONEHDRS"} = "/NextDeveloper/bin/fastcp";
}

#
#  If we are building a project, see if there are arguments defined in PB for
#  this project
#

# For some reason projectInfo currently crashs on NT 4.0b2
if ( -r "PB.project" && !$winnt ) {
    # Add the arguments defined in ProjectBuilder

    if ($ENV{OMNIMAKE_INSTALL_ROOT}) {
        $toolsRoot = "$ENV{OMNIMAKE_INSTALL_ROOT}/Tools";
    } else {
        $toolsRoot = "$ENV{OMNIMAKE_ROOT}/Tools";
    }

    $debug && print STDERR "toolsRoot = $toolsRoot\n";

    # Setup projectInfo
    $projectInfo = "$toolsRoot/projectInfo" . $executableExtension;

    $debug && print STDERR "projectInfo = $projectInfo\n";

    # Make sure projectInfo has been built
    if ( $projectInfo && -x $projectInfo ) {
	$projectType = `$projectInfo PROJECTTYPE`;
	chop($projectType);

	if (($projectType ne "Component") && ($projectType ne "Subproject")) {
	    $debug && print STDERR "projectType = $projectType\n";
	    $projectName = `$projectInfo PROJECTNAME`;
    
	    chop($projectName);
	
	    $debug && print STDERR "Project Name = $projectName\n";
    
#
#  We weren't using the below anyway, and the error output from defaults
#  was pretty annoying.  It's completely disabled now.  If we start using
#  it in the future, we should redirect stderr to stdout when calling
#  defaults via 2>&1, and then parse the return value for the defaults
#  error message and act appropriately.  -luke
#
#	    if ( $projectName ) {	    
#		$defaults = "/usr/bin/defaults";
#	    
#		$pbArgs = `$defaults read ProjectBuilder $projectName-Args`;
#		chop($pbArgs);
#	    
#		if (!$pbArgs) {
#		    # Get the default arguments PB would use
#		    # This breaks in some cases
#		    #$pbArgs = `$defaults read ProjectBuilder Args`;
#		    #chop($pbArgs);
#		}
#	    }
        }
    }
}

#
# Force sub-makes to use this wrapper around make
#

if ($winnt) {
  $perl = "perl.exe"; # WinNT will search our path
} else {
  $perl = "/usr/local/bin/perl"; # Mach won't
}

$shellArgs{MAKE}="$perl $shellArgs{OMNIMAKE_ROOT}/utilities/omnimake.perl";

#
# Build up the argument array, first with the shell arguments and then
# with anything defined in PB (which may override the shell settings).
# Finally, add any arguments on the command line
#

$makeArgs = "";

sub quoteArg { # string
  local($string) = @_;

  if ($string =~ m/ /) {
	$string = "\"$string\"";
  }

  return $string;
}

foreach $key (keys(%shellArgs)) {
    $normalArg = $shellArgs{$key};
    $debug && print STDERR "normalArg = $normalArg\n";
    $quotedArg = &quoteArg($normalArg);
    $makeArgs .= "$key=$quotedArg ";
}

    # NOTE: This will screw up on quoted arguments from PB, for example:
    #  foo="wonga blegga" would get sent as two arguments.
$makeArgs .= "$pbArgs ";

while ($arg = shift) {
    $normalArg = $arg;
    $debug && print STDERR "normalArg = $normalArg\n";
    $quotedArg = &quoteArg($normalArg);
    $makeArgs .= "$quotedArg ";
}

#
#  Announce the argument we arrived at and run 
#

$debug && print STDERR "Building with arguments: '$makeArgs'\n";

$status = system("$gnumake $makeArgs");
exit ($status >> 8);
