#! /usr/bin/env python
import sys
import string
import re

buffer = ""
if len(sys.argv) > 1 and sys.argv[1] == "-h":
    print "Usage: " + sys.argv[0] + " [<input file>] [<output file>]"
    print "replace line breaks in log entries with <br>"
    print "log entries are presumed to begin with a year"
    sys.exit(0)

if len(sys.argv) > 1:
    if sys.argv[1] == "-":
        infile = sys.stdin
    else:
        infile = open(sys.argv[1],"r")
else:
    infile = sys.stdin

if len(sys.argv) > 2:
    if sys.argv[2] == "-":
        outfile = sys.stdout
    else:
        outfile = open(sys.argv[2],"w")
else:
    outfile = sys.stdout


for line in infile:
    try:
        if re.match('2\d\d\d-\d\d-\d\d',line):
            outfile.write(re.sub('<br>$', '', re.sub('[\r\n]+','<br>',buffer)))
            outfile.write("\n");
            buffer = ""
        buffer += line
    except IndexError:
        pass

if len(buffer) != 0:
    outfile.write(re.sub('<br>$', '', re.sub('[\r\n]+','<br>',buffer)))
    outfile.write("\n");
