package WB26WorkspaceCreator;

# ************************************************************
# Description   : Workbench 2.6 / VxWorks 6.4 generator
# Author        : Johnny Willemsen
# Create Date   : 07/01/2008
# ************************************************************

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

use strict;
use FileHandle;
use File::Basename;

use WB26ProjectCreator;
use WorkspaceCreator;

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

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

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

sub workspace_file_name {
  #my $self = shift;
  return 'org.eclipse.core.resources.prefs';
}

sub pre_workspace {
  my($self, $fh) = @_;
  my $crlf = $self->crlf();

  $self->print_workspace_comment($fh,
            '#----------------------------------------------------------------------------', $crlf,
            '#       WindRiver Workbench generator', $crlf,
            '#', $crlf,
            '# $Id: WB26WorkspaceCreator.pm 1400 2008-05-07 13:27:20Z 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,
            '# This file should be placed in the .metadata\.plugins\org.eclipse.core.runtime\.settings directory', $crlf,
            '#', $crlf,
            '# MPC Command:', $crlf,
            "# $0 @ARGV", $crlf,
            '#----------------------------------------------------------------------------', $crlf);
  print $fh 'version=1', $crlf,
            'eclipse.preferences.version=1', $crlf,
            'description.defaultbuildorder=false', $crlf;
}

sub write_comps {
  my($self, $fh) = @_;
  my $pjs  = $self->get_project_info();
  my @list = $self->sort_dependencies($self->get_projects(), 0);
  my $all  = 'description.buildorder=';

  ## Construct the target
  foreach my $project (@list) {
    $all .= "$$pjs{$project}->[0]/";
  }
  print $fh $all, $self->crlf();
}

sub post_workspace {
  my($self, $fh, $creator) = @_;
  my $crlf = $self->crlf();

  foreach my $project ($self->sort_dependencies($self->get_projects(), 0)) {
    print $fh "$project$crlf";
    $self->add_dependencies($creator, $project);
  }
}

sub get_additional_output {
  ## Create the accompanying list file.  It always goes in the same
  ## directory as the first workspace output file.  See
  ## WorkspaceCreator.pm for a description of the array elements.
  return [[undef, 'wb26projects.lst', \&list_file_body]];
}

sub list_file_body {
  my($self, $fh) = @_;
  my $crlf = $self->crlf();

  $self->print_workspace_comment($fh,
            '#----------------------------------------------------------------------------', $crlf,
            '#       WindRiver Workbench generator', $crlf,
            '#', $crlf,
            '# $Id: WB26WorkspaceCreator.pm 1400 2008-05-07 13:27:20Z 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,
            '# MPC Command:', $crlf,
            "# $0 @ARGV", $crlf,
            '#----------------------------------------------------------------------------', $crlf);

  ## Print out each target separately
  foreach my $project ($self->sort_dependencies($self->get_projects(), 0)) {
    print $fh Cwd::abs_path($self->mpc_dirname($project)),
                '/.project', $crlf;
  }
}

sub add_dependencies {
  my($self, $creator, $proj) = @_;
  my $fh      = new FileHandle();
  my $pre     = "\t\t" . '<project>';
  my $post    = '</project>';
  my $outdir  = Cwd::abs_path($self->mpc_dirname($proj));
  my $outfile = $outdir . '/.project';

  ## Go through twice to edit both the .project and .wrproject files
  for(my $i = 0; $i < 2; $i++) {
    if (open($fh, "$outfile")) {
      my @read = ();
      my $cwd  = $self->getcwd();
      while(<$fh>) {
        if (/MPC\s+ADD\s+DEPENDENCIES/) {
          my $crlf = $self->crlf();
          my $deps = $self->get_validated_ordering($proj);
          foreach my $dep (@$deps) {
            my $relative = $self->get_relative_dep_file($creator,
                                                        "$cwd/$proj", $dep);
            if (defined $relative) {
              push(@read, "$pre$dep$post$crlf");
            }
          }
        }
        else {
          push(@read, $_);
        }
      }
      close($fh);

      if (open($fh, ">$outfile")) {
        foreach my $line (@read) {
          print $fh $line;
        }
        close($fh);
      }
    }

    ## The dependencies need to go into the .wrproject too, so transform
    ## the name and the pre and post values.
    $outfile = $outdir . '/.wrproject';
    $pre = '        <subproject project="/';
    $post = '"/>';

  }
}


1;
