# -*- coding: UTF8 -*-

"""
Copyright: Eesti Vabariigi Valimiskomisjon
(Estonian National Electoral Committee), www.vvk.ee
Written in 2004-2014 by Cybernetica AS, www.cyber.ee

This work is licensed under the Creative Commons
Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-nd/3.0/.
"""

import sys
import exception_msg

# groups
VERIFICATION_ERRORS = "VERIFICATION_ERRORS"

GROUPS = [VERIFICATION_ERRORS]

MSGSTRINGS_IN_FILE = 'evstrings.in'
MSGSTRINGS_FILE = 'evstrings.py'
TEATED_EXAMPLE_FILE = 'teated.example'


def create_msgstrings_py(strings):
    def write_lines(ffile, lines):
        keys = sorted(lines.keys())
        for i in keys:
            ffile.write("# %s\n%s = \\\n    '%s'\n\n" %
                        (lines[i][0], i, lines[i][1]))

    _wf = open(MSGSTRINGS_FILE, 'w')

    _wf.write('# Log\n')
    _wf.write('# -*- coding: UTF8 -*-\n\n')

    _wf.write('# NB! This file is auto generated by script %s. Any changes\n' %
              sys.argv[0])
    _wf.write('# made to it will be overwritten when the script next '
              'updates this file.\n\n')

    _wf.write('"""\n')
    _wf.write('Outcoming server error and message strings\n')
    _wf.write('"""\n\n')

    _wf.write('################################\n')
    _wf.write('# Verification protocol errors #\n')
    _wf.write('################################\n\n')
    write_lines(_wf, strings[VERIFICATION_ERRORS])

    _wf.write('\n# vim:set ts=4 sw=4 et fileencoding=utf8:\n')
    _wf.close()


def create_teated_example(strings):
    def write_lines(ffile, group, lines):
        ffile.write('\n\n[%s]\n\n' % group)
        keys = sorted(lines.keys())
        for i in keys:
            ffile.write("# %s\n%s=%s\n\n" % (lines[i][0], i, lines[i][1]))

    _wf = open(TEATED_EXAMPLE_FILE, 'w')

    _wf.write('# Väljaminevad (vea)teated serveritest HES ja HTS.\n')
    _wf.write('# NB!\n')
    _wf.write('#\t* Teated peavad olema UTF-8 kodeeringus.\n')
    _wf.write('#\t* Võtmesõnu muuta ei tohi\n')
    _wf.write('#\t* Sümbolipaare "%s", mis asendatakse dünaamilise tekstiga\n')
    _wf.write('#\t  võib teates ringi tõsta, kuid neid ei tohi sealt välja \n')
    _wf.write('#\t  jätta ega juurde lisada.\n')
    _wf.write('\n')

    for group in GROUPS:
        write_lines(_wf, group, strings[group])

    _wf.write('\n# vim:set ts=4 sw=4 et fileencoding=utf8:\n')
    _wf.close()


def main_function():
    try:
        sstrings = dict([(_g, {}) for _g in GROUPS])

        llines = []
        ff = open(MSGSTRINGS_IN_FILE, 'r')
        for ii in ff.readlines():
            line = ii.strip()
            if line and line[0] != '#':
                llines.append(line)
        ff.close()

        # jagame read gruppidesse
        ii = 3
        while ii < len(llines):
            ggroup, comment, key, value = llines[ii-3:ii+1]
            if ggroup in GROUPS:
                sstrings[ggroup][key] = comment, value
            else:
                raise Exception('Vigane grupp "%s", peab kuuluma "%s"' %
                                (ggroup, str(GROUPS)))
            ii += 4

        create_msgstrings_py(sstrings)
        create_teated_example(sstrings)

    except:  # pylint: disable=W0702
        print exception_msg.trace()
        sys.exit(1)

    sys.exit(0)


if __name__ == '__main__':
    main_function()

# vim:set ts=4 sw=4 et fileencoding=utf8:
