ragel_file = ARGV[0] || File.join(File.dirname(__FILE__), "classes/command.rl")
base       = File.join(File.dirname(ragel_file), File.basename(ragel_file, '.rl'))

DOT_CMD = '/Applications/Graphviz.app/Contents/MacOS/dot' # Mac OS X in Applications (works better with fonts)
# DOT_CMD = 'dot'

# make dot file
puts `ragel #{base}.rl | rlgen-dot -p -o #{base}.tmp`
options = {
  :fontname      => 'Helvetica',
	:labelfontname => 'Helvetica',
}
# filter labels
File.open("#{base}.dot", 'w') do |file|
  File.foreach("#{base}.tmp") do |line|
    file.puts line
    if line =~ /\Adigraph/
      options.each {|k,v| file.puts "\t#{k}=#{v};"}
    end
  end
end

puts `rm #{base}.tmp`
puts `#{DOT_CMD} -Tpng #{base}.dot -o #{base}.png`
exec("open #{base}.png")
