package BMakeWorkspaceCreator;

# ************************************************************
# Description   : A Borland Make Workspace (Makefile) creator
# Author        : Chad Elliott
# Create Date   : 2/03/2004
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;

use BMakeProjectCreator;
use WorkspaceCreator;

use vars qw(@ISA);
@ISA = qw(WorkspaceCreator);

# ************************************************************
# Data Section
# ************************************************************

my($max_line_length) = 32767; ## Borland Make's maximum line length
my(@targets) = ('clean', 'generated', 'realclean', '$(CUSTOM_TARGETS)');

# ************************************************************
# Subroutine Section
# ************************************************************

sub supports_make_coexistence {
  #my($self) = shift;
  return 1;
}


sub crlf {
  my($self) = shift;
  return $self->windows_crlf();
}


sub workspace_file_name {
  my($self) = shift;
  if ($self->make_coexistence()) {
    return $self->get_modified_workspace_name($self->get_workspace_name(),
                                              '.bmake');
  }
  else {
    return $self->get_modified_workspace_name('Makefile', '');
  }
}


sub workspace_per_project {
  #my($self) = shift;
  return 1;
}


sub pre_workspace {
  my($self) = shift;
  my($fh)   = shift;
  my($crlf) = $self->crlf();

  print $fh '#----------------------------------------------------------------------------', $crlf,
            '#       Borland Workspace Makefile', $crlf,
            '#', $crlf,
            '# $Id: BMakeWorkspaceCreator.pm 1069 2006-10-16 13:36:23Z elliott_c $', $crlf,
            '#', $crlf,
            '# This file was generated by MPC.  Any changes made directly to', $crlf,
            '# this file will be lost the next time it is generated.', $crlf,
            '#', $crlf,
            '# MPC Command:', $crlf,
            '# ', $self->create_command_line_string($0, @ARGV), $crlf,
            '#', $crlf,
            '#----------------------------------------------------------------------------', $crlf,
            $crlf;
}


sub write_project_targets {
  my($self)     = shift;
  my($fh)       = shift;
  my($and)      = shift;
  my($target)   = shift;
  my($list)     = shift;
  my($crlf)     = $self->crlf();
  my($cwd)      = $self->getcwd();

  foreach my $project (@$list) {
    my($dir)   = $self->slash_to_backslash($self->mpc_dirname($project));
    my($chdir) = ($dir ne '.');

    print $fh "\t", ($chdir ? "\$(COMSPEC) /c \"cd $dir $and " : ''),
              "\$(MAKE) -\$(MAKEFLAGS) -f ",
              $self->mpc_basename($project), " $target",
              ($chdir ? '"' : ''), $crlf;
  }
}


sub write_comps {
  my($self)     = shift;
  my($fh)       = shift;
  my($creator)  = shift;
  my($and)      = $creator->get_and_symbol();
  my($projects) = $self->get_projects();
  my($pjs)      = $self->get_project_info();
  my(%targnum)  = ();
  my(@list)     = $self->number_target_deps($projects, $pjs, \%targnum, 0);
  my($crlf)     = $self->crlf();
  my(@ltargets) = @targets;

  ## Set up the custom targets
  print $fh '!ifndef CUSTOM_TARGETS', $crlf,
            'CUSTOM_TARGETS=_EMPTY_TARGET_', $crlf,
            '!endif', $crlf;

  ## Construct the "all" target
  my($all) = 'all:';
  foreach my $project (@list) {
    $all .= " $$pjs{$project}->[0]";
  }
  if (length($all) < $max_line_length) {
    print $fh $crlf, $all, $crlf;
  }
  else {
    unshift(@ltargets, 'all');
  }

  ## Print out all other targets here
  foreach my $target (@ltargets) {
    print $fh $crlf, $target, ':', $crlf;
    $self->write_project_targets($fh, $and, $target, \@list);
  }

  ## Print out each target separately
  foreach my $project (@list) {
    print $fh $crlf, $$pjs{$project}->[0], ':';
    if (defined $targnum{$project}) {
      foreach my $number (@{$targnum{$project}}) {
        print $fh " $$pjs{$list[$number]}->[0]";
      }
    }

    print $fh $crlf;
    $self->write_project_targets($fh, $and, 'all', [ $project ]);
  }

  ## Print out the project_name_list target
  print $fh $crlf, 'project_name_list:', $crlf;
  foreach my $project (sort @list) {
    print $fh "\t\@echo $$pjs{$project}->[0]$crlf";
  }
}



1;
