def all_pages(name, pdf)
  return [:pdf, name, pdf]
end

def all_no_entry(name, pdf)
  return [:pdf_no_entry, name, pdf]
end

def section_offset(name, page)
  return [:section, name, page]
end

pdf_structure = [
  all_no_entry("Introduction", "intro.pdf"),
  all_pages("Requirements Document", "requirements.pdf"),
  all_pages("Design Document", "design-doc.pdf"),
  all_pages("Ryan Alder's Tech Review", "binary/techreview-ryan.pdf"),
  all_pages("Danila Fedorin's Tech Review", "techreview-daniel.pdf"),
  all_pages("Matthew Sessions' Tech Review", "binary/techreview-matt.pdf"),
  all_pages("Ryan Alder's Blog Posts", "binary/blog-ryan.pdf"),
  all_pages("Danila Fedorin's Blog Posts", "blog-daniel.pdf"),
  all_pages("Matthew Sessions' Blog Posts", "binary/blog-matt.pdf"),
  all_pages("Project Documentation and READMEs", "readme.pdf"),
  all_pages("Learning Resources", "resources.pdf"),
  all_pages("Conclusions and Reflections", "reflection.pdf"),
  all_pages("Appendix 1: Essential Code Listings", "listings.pdf"),
  all_pages("Appendix 2: Project Images", "images.pdf"),
  all_pages("Appendix 2: Code Review Changes", "review.pdf"),
  all_pages("Appendix 4: Remaining Work", "todo.pdf")
]

toc = File.open("generated.toc", "w")

page_tally = 1
section_counter = 1
pdf_structure.each do |entry|
  next unless entry[0] == :pdf or entry[0] == :pdf_no_entry
  type, name, pdf = entry
  page_count = `pdftk #{pdf} dump_data | grep NumberOfPages`.split(":")[1].to_i
  output = "\\contentsline {section}{\\numberline {#{section_counter}}#{name}}{#{page_tally}}%"

  if type == :pdf
    toc.puts output
    puts output
  end

  section_counter += 1
  page_tally += page_count
end
