module Dunlop::FileSed

  def self.in_place(filename, sed_expressions)
    expression_list = Array.wrap(sed_expressions).map { |exp| "-e '#{exp}'" }.join(' ')
    if /Darwin/i =~ %x[uname]
      #BSD
      %x[ LC_CTYPE=C LANG=C gsed -i'' -r #{expression_list} #{filename} ]
      raise 'error in sed command line' unless $?.exitstatus == 0
    else
      #GNU
      %x[ LC_CTYPE=C LANG=C sed -i'' -r #{expression_list} #{filename} ]
      raise 'error in sed command line' unless $?.exitstatus == 0
    end
  end

  def self.fix_line_endings(filename)
    %x[dos2unix -f #{filename} > /dev/null 2>&1]
    raise 'error in dos2unix command line' unless $?.exitstatus == 0
    %x[dos2unix -f -c mac #{filename} > /dev/null 2>&1]
    raise 'error in dos2unix command line' unless $?.exitstatus == 0
  end

end
