import os
import sys

import shell_util

month = {"1": "January",
         "2": "February",
         "3": "March",
         "4": "April",
         "5": "May",
         "6": "June",
         "7": "July",
         "8": "August",
         "9": "September",
         "10": "October",
         "11": "November",
         "12": "December"}

def build(nickname, defs, show=0):

    entry_dir = "{}/journal-{}/entries/".format(defs[nickname]["working_path"], nickname)

    entries = []
    years = []
    
    # get the list of directories in entries/
    for d in os.listdir(entry_dir):
        if os.path.isdir(entry_dir + d):
            entries.append(d)

            y, m, d = d.split("-")
            if not y in years:
                years.append(y)

            
    os.chdir(entry_dir)
                
    # years are chapters
    try: f = open("chapters.tex", "w")
    except:
        sys.exit("ERROR: unable to create chapters.tex")
        

    for y in years:
        f.write("\\chapter{{{}}}\n".format(y))
        f.write("\\input{{entries/{}.tex}}\n\n".format(y))

    f.close()

    
    # within each year, months are sections
    for y in years:

        try: f = open("{}.tex".format(y), "w")
        except:
            sys.exit("ERROR: unable to create chapters.tex")

        current_month = None
        
        for e in entries:
            ytmp, m, _ = e.split("-")            
            if not ytmp == y:
                continue

            if not m == current_month:
                f.write("\\section{{{}}}\n".format(month[m]))

            tex = []
            for t in os.listdir(e):
                if t.endswith(".tex"):
                    tex.append(t)

            tex.sort()
            for t in tex:
                f.write("\\HRule\\\\ \n")
                idx = t.rfind(".tex")
                tout = t[:idx].replace("_", " ")
                f.write("{{\\bf {} }}\\\\[0.5em] \n".format(tout))
                f.write("\\input{{entries/{}/{}}}\n\n".format(e, t))
                f.write("\\vskip 2em\n")
                    
            f.write("\n")
            
        f.close()

        
    # now do the latexing to get the PDF
    build_dir = "{}/journal-{}/".format(defs[nickname]["working_path"], nickname)
    os.chdir(build_dir)
    
    for i in range(3):
        stdout, stderr, rc = shell_util.run("pdflatex --halt-on-error journal.tex")

    # if we were not successful, then the PDF is not produced
    # note: pdflatex does not seem to use stderr at all
    pdf = os.path.normpath("{}/journal.pdf".format(build_dir))
    if os.path.isfile(pdf):
        print "journal is located at {}".format(pdf)
    else:
        print stdout
        sys.exit("There were LaTeX errors")

    # show it in a PDF viewer
    if show == 1:
        os.system("evince {} &".format(pdf))
    

@@ -1550,16 +1550,66 @@
 th%5Bm%5D))%0A
+                current_month = m%0A                
 %0A       
@@ -2736,25 +2736,22 @@
         
-sys.exit(
+print 
 %22There w
@@ -2767,17 +2767,161 @@
  errors%22
-)
+%0A        print %22Check the source in %7B%7D/entries/%22.format(build_dir)%0A        print %22be sure to 'git commit' to store any fixes%22%0A        sys.exit()%0A
 %0A%0A    # 

fix the sectioning and print out more helpful information if a build fails