#!/opt/local/bin/ruby

require 'latex/parser'
require 'yaml'

root = '/opt/local/share/texmf-dist'

ignores_file = 'parse-lots-ignore.list'

ignores = File.read(ignores_file).split("\n")

ignores_fp = File.open(ignores_file,"a")
ignores_fp.sync = true

manual_ignores_file = 'parse-lots-ignore.yaml'

manual_ignores = YAML::load(File.read(manual_ignores_file))

Dir.chdir(root)
Dir['doc/**/*.tex'].each do |tex|
  next if ignores.include?(tex)
  puts File.join(root,tex)
  if reason = manual_ignores[tex] then
    puts " .. ignored because #{reason}"
    next
  end
  
  content = File.read(tex)
  
  if content =~ /\\begin\{document\}/ then
    begin
      LaTeX::Parser.new.parse_string(content)
    rescue
      $stderr.puts $!
      sleep 2
    end
  else
    ignores_fp.puts tex
    puts " ... not latex"
  end

end

