module Dunlop::FileGrep

  def self.egrep( input_filename, output_filename, regexp )
    %x[egrep '#{regexp}' #{input_filename} > #{output_filename}]
  end

  def self.egrep_v( input_filename, output_filename, regexp )
    %x[egrep -v '#{regexp}' #{input_filename} > #{output_filename}]
  end

  def self.select(input_filename, regexp_string)
    filter input_filename, regexp_string, filter_method: :egrep
  end

  def self.filter(input_filename, regexp_string, filter_method: :egrep)
    local_working_file = Tempfile.new("file_grep", WORKING_PATH)
    #egrep(input_filename, local_working_file.path, regexp_string)
    public_send(filter_method, input_filename, local_working_file.path, regexp_string)
    FileUtils.cp(local_working_file.path, input_filename)
    FileUtils.chmod(0644, input_filename)
  ensure
    local_working_file.unlink
  end

  def self.reject(input_filename, regexp_string)
    filter input_filename, regexp_string, filter_method: :egrep_v
    #local_working_file = Tempfile.new("file_grep", WORKING_PATH)
    #egrep_v(input_filename, local_working_file.path, regexp_string)
    #FileUtils.cp(local_working_file.path, input_filename)
    #FileUtils.chmod(0644, input_filename)
  #ensure
    #local_working_file.unlink
  end

end
