require File.expand_path File.join(File.dirname(__FILE__), "ruby_base.rb")




class BuildWise::Builder::Shell < BuildWise::Builder::Base
  def initialize(config)
    super(config, "shell")
  end

=begin
  def run(cmd)
    ENV['PATH'] = "#{@config[:builder, :shell, :path]}::#{ENV['PATH']}"
    cmd_array = cmd.split(" ")

    require 'tempfile'
    command_log_file = Tempfile.new('foo')
    command_line_output = ::CommandLine.execute(cmd_array, {:stdout => command_log_file.path, :stderr => command_log_file.path})


    @output = File.read(command_log_file.path)
    successful?
  end
=end
  
  def run_task(build_step, log_file)
    task_name = build_step.name
    target = build_step.task

    cmd_array = target.split(" ")

    if log_file

      command_line_output = ::CommandLine.execute(cmd_array, {:stdout => log_file, :stderr => log_file} )
      @output = File.read(log_file)      
    else
      require 'tempfile'
      command_log_file = Tempfile.new('foo')
      command_line_output = ::CommandLine.execute(cmd_array, {:stdout => command_log_file.path, :stderr => command_log_file.path})

      @output = File.read(command_log_file.path)
    end

    successful?
  end


  def successful?
    $?.exitstatus == 0
  end

  def brokeness
    return 0;
=begin
    if @config[:builder, @name.to_sym, :brokeness]
      re = Regexp.new(@config[:builder, @name.to_sym, :brokeness])
      md = re.match(@output)
      if md
        return md.captures.inject(0) { |sum, n| sum += n.to_i }
      end
    else
      super # use RubyBase default if custom :brokeness not specified
    end
=end    
  end

end