#!/usr/bin/env python

import sys
import math

av = sys.argv

# Inputdatei ist eine po-Datei (poEdit)

if len(sys.argv) < 2:
    print "Fehlender Dateiname"
    print "usage: python convert-po.py Inputdatei Ausgabedatei"
    sys.exit(1)

#Datei zum lesen oeffnen
scanfile = file(av[1],"r")

#Zeilenweise alles einlesen
inputline = []

for line in scanfile:
    inputline.append(line)
   
print  "Anzahl inputline:" , len(inputline)

file_out = file(av[2],'w')

for i in range(len(inputline)): 
    if i < 17:
        file_out.write(inputline[i])
    else:
        line = inputline[i]
        if line.startswith("msgid"):
            if inputline[i+1].startswith("msgstr"):
                changetext = inputline[i+1]
                changetext = changetext.replace("msgstr","msgid")  
                file_out.write(changetext)
            else:
                file_out.write('msgid ""'+'\n') 
                z = i+1
                write = False
                while inputline[z].strip() != "":
                    if inputline[z].startswith("msgstr"):
                        write = True
                    if write == True:
                        if inputline[z].strip() != 'msgstr ""':
                            file_out.write(inputline[z])       
                        else:
                            print inputline[z]
                    z = z + 1
                
                file_out.write('msgstr ""'+'\n')
                file_out.write('\n')
                
                # TODO
        elif line.startswith("msgstr"):
            file_out.write('msgstr ""'+'\n')
            file_out.write('\n')
        elif line.startswith("#"):
            if line.startswith("#, fuzzy"):
                continue
            file_out.write(inputline[i])


print "Ausgabe: Konvertierung abgeschlossen"
