PAGES_2014 = {
  "14-01JAN04" => 1,
  "14-02JAN07" => 7,
  "14-03JAN14" => 15,
  "14-04JAN29" => 21,
  "14-05FEB28" => 28,
  "14-06MARCH05" => 33,
  "14-07MARCH07" => 35,
  "14-08MAR12" => 40,
  "14-09MAR14" => 48,
  "14-10MAR29" => 55,
  "14-11APR22" => 58,
  "14-13MAY01" => 66,
  "14-13MAY19" => 71,
  "14-14MAY19" => 76,
  "14-15MAY23" => 85,
  "14-17JUN06" => 89,
  "14-18JUN10" => 96,
  "14-19JUNE13" => 101,
  "14-20JULY02" => 108,
  "14-21JULY10" => 114,
  "14-22AUG28" => 120,
  "23-140309217" => 127,
  "14-24OCT02" => 137,
  "14-25OCT29" => 140,
  "14-26NOV11" => 143,
  "14-27NOV14" => 150,
  "14-28NOV20" => 153,
  "14-29NOV21" => 159,
  "14-30DEC17" => 165,
  "14-31DEC31" => 171,
}

PAGES_2013 = {
  "13-01JAN10" => 1,
  "13-02JAN11" => 7,
  "130026999" => 10,
  "13-04-130032415-JAN16" => 20,
  "13-06JAN17" => 29,
  "13-05-130034144-JAN17" => 35,
  "13-09APR18" => 44,
  "13-04192013" => 48,
  "130258468" => 55,
  "13-13MAY03" => 73,
  "13-16MAY31" => 75,
  "13-15MAY17" => 80,
  "13-14MAY17" => 85,
  "13-17JUNE03" => 90,
  "13-18JUN16" => 94,
  "13-19JUNE18" => 98,
  "13-20JUNE22" => 103,
  "130396546" => 106,
  "13-21JUN28" => 114,
  "13-23JUL02" => 126,
  "07-0307042013" => 135,
  "13-26AUG07" => 140,
  "13-27AUG27" => 146,
  "13-09062013" => 152,
  "588206" => 155,
  "13-30SEP13" => 157,
  "13-29SEP13" => 163,
  "13-30SEP17" => 177,
  "13-32OCT11" => 182,
  "13-33NOV04" => 185,
  "13-34NOV12" => 191,
  "13-35NOV11" => 198,
  "13-36NOV20" => 205,
  "13-37DEC06" => 211,
  "13-38DEC12" => 218
}

PAGES_2012 = {
  "12-01-120024312-JAN13" => 1,
  "12-03FEB28" => 9,
  "12-04-120121422-MAR01" => 14,
  "2012-05-120153329-MAR16" => 22,
  "12-06-120160849-MAR19" => 37,
  "2012-07-120190775-APR02" => 48,
  "12-08APR18" => 63,
  "12-09-???-APR20" => 68,
  "12-10-120229050-APR23" => 75,
  "2012-11-XXXXXXXXX-22MAY" => 93,
  "2012-12-120311094-MAY24" => 97,
  "12-13MAY25" => 107,
  "2012-14-120330693-JUNE01" => 114,
  "120380388" => 124,
  "2012-16-POTUS-JUNE25-26" => 134,
  "12-17-120419840-JUL8TH" => 138,
  "12-18-XXXX-JUL09" => 146,
  "12-19AUG16" => 152,
  "12-20AUG27" => 157,
  "2012-21-120596647-SEPT22" => 164,
  "2012-22-120676147-NOV02" => 170,
  "120703431" => 181,
  "12-24-120721225-NOV16" => 188,
  "12-26-120759695-DEC21" => 202,
  "12-26-120811010-DEC28" => 215
}

PAGES_2011 = {
  "11-01-100688619-23feb" => 1,
  "2011-02-POTUSVISIT-mar08" => 9,
  "2011-3-vpotusvisit-mar21" => 17,
  "11-04-110153430-mar26" => 23,
  "11-05-110176457-apr7th" => 30,
  "11-06-110185378-apr12" => 40,
  "11-07-110216684" => 46,
  "11-08-110243024may12" => 54,
  "2011-09-potusvisit-may18" => 61,
  "110324854" => 69,
  "2011-10-vpotusvisit-may25" => 77,
  "11-11-110337527-jun28" => 85,
  "11-12-110344330-jul01" => 93,
  "13-11-110346844" => 105,
  "2011-14-xxxxxxxxx-july12" => 110,
  "11-15-110371763-jul14" => 116,
  "11-16-xxxxxxxxx-aug06" => 125,
  "11-17-110417538-aug05" => 135,
  "11-18-110427466-aug11" => 142,
  "11-19-110433468-aug13" => 154,
  "2011-20-110443166-aug18" => 161,
  "11-21-xxxx-aug18" => 171,
  "11-22-110459426aug26" => 180,
  "11-23-sept8th-sept13th" => 188,
  "11-24-110496682" => 192,
  "11-26-110501323-sep15" => 201,
  "11-28-110527725-sep28" => 207,
  "2011-29-110540218-oct04" => 214,
  "11-30-110546548oct07" => 223,
  "11-31-110570884-oct19" => 233,
  "11-25-xxxx-sept14th" => 243,
  "11-33-110664907dec05" => 249,
  "1135xxxxxxxx" => 257,
  "11-32-110631709-nov18" => 265
}

PAGES_2010 = {
  "10-01-100000799-jan01" => 1,
  "10-02-100009639-jan06" => 9,
  "10-03-100014345-jan08" => 17,
  "10-04-100018365jan11" => 26,
  "100020377" => 36,
  "10-08-100035245-jan20" => 47,
  "10-09-xxxx-jan25" => 57,
  "10-10-100050545-jan28" => 65,
  "10-11-100088960-feb18" => 81,
  "10-12-100090856feb19" => 90,
  "13-06-100113242" => 99,
  "10-14-100110856mar04" => 109,
  "2010-15-100117193-march05" => 115,
  "10-16-100136308" => 125,
  "10-17-xxxx-mar26th" => 131,
  "10-19-100216540" => 149,
  "2010-20-100244597-may07" => 157,
  "10-21-xxxxxxxxx-may16" => 168,
  "2010-22-100269184-may20" => 175,
  "10-24-100281047may25" => 185,
  "10-23-100280081may25" => 194,
  "10-25-100286121" => 203,
  "2010-26-100295362-june01" => 210,
  "10-27-100296007jun02" => 217,
  "2010-28-100301109-june07" => 226,
  "2010-30-100312155-june09" => 236,
  "2010-32-100313867-june10" => 245,
  "100326056" => 254,
  "10-35-100343444-jun25" => 262,
  "2010-36-100351099-june29" => 268,
  "10-37-100376360-july12" => 279,
  "10-38-100391420" => 283,
  "10-39-100392980-jul21" => 286,
  "10-40-100400818-jul25" => 293,
  "10-41-100410593" => 296,
  "2010-43-100456107-aug22" => 304,
  "10-43-100478397-sept3rd" => 313,
  "10-43-100433193-aug10th" => 322,
  "10-45-100497047-sept12th" => 331,
  "10-47-100501871-sept15" => 340,
  "2010-48-100503318-sept15" => 349,
  "50-100517973-sept23" => 359,
  "10-51-100549231" => 370,
  "2010-53-100570861-oct21" => 377,
  "10302010" => 389,
  "2010-55-100597163-nov3" => 397,
  "100624097" => 412,
  "10-57-xxxx-dec2nd" => 420,
}

def split_and_stuff(start_pages, input_pdf)
  name_to_pages = Hash.new { |h,k| h[k] = [] }
  Dir.mktmpdir do |dir|
    system("pdfseparate", input_pdf, "#{dir}/%03d.pdf")
    Dir.glob("#{dir}/*.pdf").sort.each do |page|
      page_num = /(\d+)\.pdf$/.match(page)[1].to_i
      name = start_pages.select { |k,v| v <= page_num }.max_by { |k,v| v }.first
      name_to_pages[name] << page
    end
    name_to_pages.each do |name,pages|
      pdf_file = "output/#{name}.pdf"
      txt_file = "output/#{name}.txt"
      system(*["pdfunite", *pages, "/tmp/xyzzytmppdf.pdf"])
      system("ocrmypdf", "-l", "eng", "--sidecar", txt_file, "/tmp/xyzzytmppdf.pdf", pdf_file)
    end
  end
end

# split_and_stuff(PAGES_2014, "2014.pdf")
# split_and_stuff(PAGES_2013, "2013.pdf")
# split_and_stuff(PAGES_2012, "2012.pdf")
# split_and_stuff(PAGES_2011, "2011.pdf")
# split_and_stuff(PAGES_2010, "2010.pdf")
