#!/usr/bin/env python
# -*- coding: utf-8 -*-

### BEGIN LICENSE
# Copyright (C) 2010 Stephan Tetzel <stephan@zefanjas.de>
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import os, sys

session = open(sys.argv[1], "r")
file1 = open("modulePart1.xml", "w")
i = 1
firstLines = ""
finder = 0
for line in session.readlines():
    if finder == 0:
        firstLines = firstLines + line
        file1.write(line)
        if line.find("<XMLBIBLE") != -1:
            finder = 1
    else:
        if i == 1:
            if line.find('''bnumber="10"''') != -1:
                file1.write("</XMLBIBLE>"  + "\n")
                file1.close()
                i = i + 1
                filename = "modulePart" + str(i) + ".xml"
                file1 = open(filename, "w")
                file1.write(firstLines)
                file1.write(line)
            else:
                file1.write(line)
        elif i == 2:
            if line.find('''bnumber="20"''') != -1:
                file1.write("</XMLBIBLE>"  + "\n")
                file1.close()
                i = i + 1
                filename = "modulePart" + str(i) + ".xml"
                file1 = open(filename, "w")
                file1.write(firstLines)
                file1.write(line)
            else:
                file1.write(line)
        elif i == 3:
            if line.find('''bnumber="40"''') != -1:
                file1.write("</XMLBIBLE>"  + "\n")
                file1.close()
                i = i + 1
                filename = "modulePart" + str(i) + ".xml"
                file1 = open(filename, "w")
                file1.write(firstLines)
                file1.write(line)
            else:
                file1.write(line)
        elif i == 4:
            if line.find("</XMLBIBLE>") != -1:
                file1.write(line)
                file1.close()
            else:
                file1.write(line)
print "Finished"


