#!/usr/bin/python

import sys, re
from optparse import OptionParser

usage = "usage: reverseit infile"
parser = OptionParser(usage=usage)

(options, args) = parser.parse_args()

# Check that there is only one argument
if len(args) != 1 :
   print usage
   sys.exit(1)

f = open(args[0], 'r') # Open the file

# Read the lines of the file, throwing ones that begin with '<p>' or end with
# '</p>' into an array
begin_pgph = re.compile('^\s*<p>')
end_pgph = re.compile('.*</p>\s$')
lines = []

while 1:
   line = f.readline()
   if not line:
      break
   
   if begin_pgph.match(line):
      lines.append(line)
   elif end_pgph.match(line):
      lines[len(lines) - 1] += line
      

f.close() # Close the file

lines.reverse() # Reverse the lines

# print out the lines
for line in lines:
   print line
