#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

############################################################################
#   copyright            : (C) 2006 Marko Grönroos                         #
#   email                : magi@iki.fi                                     #
############################################################################
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
#                                                                          #
############################################################################

import sys,re

################################################################################
# Notes
################################################################################

# - File is currently split after the size limit has been reached,
# thus generating slightly larger files than the limit.

################################################################################
# Print help
################################################################################
def help():
    print "Usage: msgdig [options] <filename.po>"
    print "Options:"
    print "\t-i <value>  Interval to split at"
    print "\t-s <size>   Maximum block size in kilobytes"
    print "\t-r          Recode UTF-8 as ISO-8859-15"
    print "\t-h          Print this help"

################################################################################
# Parse command-line arguments
################################################################################

msglimit  = 0
sizelimit = 0
recode    = 0

# Parse options
while len(sys.argv) > 1 and sys.argv[1][0] == "-":
    if sys.argv[1] == "-h":
        help()
        sys.exit(0)
    elif sys.argv[1] == "-i" and len(sys.argv)>=3:
        msglimit = int (sys.argv[2])
        del sys.argv[1:3]
    elif sys.argv[1] == "-s" and len(sys.argv)>=3:
        sizelimit = int (sys.argv[2]) * 1024
        del sys.argv[1:3]
    elif sys.argv[1] == "-r":
        recode = 1
        del sys.argv[1:2]
    else:
        print "Error: Invalid option '%s'" % (sys.argv[1])
        help()
        sys.exit (1)

# Get .po filename
if len(sys.argv) <= 1:
    print "Error: Input file name not given."
    help()
    sys.exit(1)

if (not msglimit and not sizelimit) or (msglimit and sizelimit):
    print "Error: You have to give either -i or -s option."
    sys.exit(1)

m = re.search (r'([^\/^\.]+)\.po', sys.argv[1])
if not m:
    print "Error: File extension must be .po."
    sys.exit(1)

basename = m.group(1)

################################################################################
# Read .po file
################################################################################
fin = open (sys.argv[1], "r")
lines = fin.readlines()
fin.close ()


################################################################################
# Parse input file and write output file
################################################################################
fout         = None  # Output file
initialstate = 1     # For opening the first file
splitstate   = 0     # Indicator that causes file split at next comment line
filecount    = 0     # File number counter
msgcount     = 0     # Message number in current file
sizecount    = 0     # Size of current output
for i in xrange(len(lines)):
    line = lines[i]

    # Split at comment line, and at beginning
    m = re.match (r'^#', line)
    if (m and splitstate) or initialstate:
        if fout:
            fout.close ()
        fout = open ("%s_%02d.po" % (basename,filecount), "w")
        filecount += 1
        initialstate = 0
        splitstate   = 0

    # Change character encoding
    if recode:
        line = line.decode("utf-8").encode("iso-8859-15")

    # Write the line
    fout.write (line)
    sizecount += len (line)

    # Detect message change
    m = re.match (r'^msgid', line)
    if m:
        msgcount += 1
        if msglimit and msgcount >= msglimit:
            # Split at next comment line
            splitstate = 1
            msgcount   = 0
        if sizelimit and sizecount >= sizelimit:
            # Split at next comment line
            splitstate = 1
            sizecount  = 0

if fout:
    fout.close ()
