#!/usr/bin/env python
#
# gut2lrf.py:  converts Project Gutenberg etexts to BBeB lrf format
# author:  (c) 2007 Tamas Decsi, http://www.forwhatitworths.com/
#
# requires: makelrf
# 
# changelog:
#   2007-11-09: keep single linebreaks at enumerations and verses
#   2007-11-02: initial revision

import os,os.path,sys

def usage(msg=None):
    print " Project Gutenberg text ebook to BBeB lrf converter "
    print " requires: makelrf"
    print " usage: %s filename.txt" % __file__
    if msg is not None: print " problem: %s" % msg
    sys.exit()

def main():
    
    if len(sys.argv)!=2:
        usage()

    filename = sys.argv[-1]
    if not os.path.exists(filename):
        usage("File '%s' not found." % filename)

    ifp = open(filename,'r')
    content = ifp.readlines()
    ifp.close()

    # chop .txt from the end of the filename
    basename = filename[:-4] 

    infile =  filename + '.tmp.txt'
 
    # extract metadata  
    startpos = 0
    endpos = len(content)
    for i in range(0,len(content)):
        # looking for *** START OF THE/THIS PROJECT GUTENBERG EBOOK
        if not startpos and content[i][:15] == '*** START OF TH':
            startpos = i
        # looking for *** END OF THE/THIS PROJECT GUTENBERG EBOOK
        if content[i][:13] == '*** END OF TH':
            endpos = i
            break

    head = content[0:startpos]
    title = '' + ''.join([line[7:].strip() for line in head if line[0:7]=='Title: '])
    author = '' + ''.join([line[8:].strip() for line in head if line[0:8]=='Author: '])
    description = title + ' by ' + author
    print "Title: %s\nAuthor: %s\n" % (title,author)

    # re-run text (which is wrapped at 72 characters per line)
    ofp = open(infile,'w')
    buf = []
    for line in content[startpos+1:endpos]:
        sline = line.strip()
        if sline!='' and line[0] in ' \t-*':
            # keep single linebreaks at enumerations and verses 
            ofp.write('%s\n' % ' '.join(buf))
            buf = []
        else:
            if buf != []:
                if sline == '':
                    ofp.write('%s\n\n' % ' '.join(buf))
                    buf = []
        buf.append(sline)
    if buf != []:
        ofp.write('%s\n\n' % ' '.join(buf))
    ofp.close()

    # invoke makelrf
    imagefile = ''
    if os.path.exists(basename + '.gif'):
        imagefile = '-i "%s.gif"' % basename
    cmd = './makelrf -a "%s" -t "%s" -d "%s" %s -o "%s.lrf" %s' % (author, title, description, imagefile, basename, infile)
    os.system(cmd)

    # clean up
    os.remove(infile)

if __name__ == "__main__":
    main()
