#!/usr/bin/ruby -Ke
# grepfile 正規表現 ファイル名
# 指定ファイル中に正規表現を満たすものがあるならば、ファイル名を出力する
# ディレクトリ名があればその配下もチェックする

def go_error
  puts 'grepfile <Rubyの正規表現> ファイル名'
  exit
end

if ARGV.size != 2
  go_error
end

regexp = Regexp.new(ARGV[0])

files = [ARGV[1]]
i = 0
while i < files.size
  currentfile = files[i]
  if File.directory?(currentfile)
    if !( /\/$/ =~ currentfile )
      currentfile += '/'
    end
    Dir.entries(currentfile).each{|file|
	if file != '.' && file != '..'
	    files << currentfile+file
	end
    }
  elsif File.file?(currentfile)
    fp = open(currentfile,'r')
    while !( fp.eof? )
      if regexp =~ fp.gets
        puts currentfile
        break
      end
    end
    fp.close
  end
  i += 1
end
