#!/usr/bin/python

# Lightspark, a free flash player implementation
#
# Copyright (C) 2013  Antti Ajanki (antti.ajanki@iki.fi)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This script generates errorconstants.{h,cpp} from ErrorConstants.xml

# TODO: Extract translated error messages from Tamarin, and append
# them into po files

import sys
import xml.etree.ElementTree as ET

def extract_errors(input_file):
    try:
        tree = ET.parse(input_file)
    except IOError, exc:
        print 'XML parsing failed ' + str(exc)
        sys.exit(1)

    errors = []
        
    root = tree.getroot()
    for child in root.findall('error'):
        errorID = child.attrib.get('id', None)
        errorLabel = child.attrib.get('label', None)
        if not errorID or not errorLabel:
            continue

        errors.append((errorID, errorLabel, child.text.strip()))

    return errors

def print_copyright(out):
    out.write("""/**************************************************************************
    Lightspark, a free flash player implementation

    Copyright (C) 2013  Alessandro Pignotti (a.pignotti@sssup.it)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

""");
    
def output_errorconstantsh(errors):
    out = open('errorconstants.h', 'w')

    print_copyright(out)
    out.write("""/* This file is generated by errorgen - DO NOT EDIT */

#ifndef ERRORCONSTANTS_H
#define ERRORCONSTANTS_H 1

#include <map>

namespace lightspark
{

""")
    
    out.write('enum\n{\n')
    for errorID, label, text in errors:
        out.write('\t%-40s= %s,\n' % (label, errorID))
    out.write('};\n\n')

    out.write('extern const std::map<int, const char *> errorMessages;\n\n')

    out.write("""};

#endif /* ERRORCONSTANTS_H */
""")

def output_errorconstantscpp(errors):
    out = open('errorconstants.cpp', 'w')
    
    print_copyright(out)
    
    out.write("""/* This file is generated by errorgen - DO NOT EDIT */

#include <initializer_list>
#include "errorconstants.h"
#include "compat.h"

namespace lightspark
{

const std::map<int, const char *> errorMessages = {
""");

    for errorID, label, text in errors:
        out.write('\t{%s, _("%s")},\n' % (label, text.replace('\\', '\\\\').replace('"', '\\"')))
    out.write('\t};\n\n')

    out.write('};\n')

def main():
    if len(sys.argv) < 2:
        print 'Expected path to ErrorConstants.xml'
        sys.exit(1)

    input_name = sys.argv[1]
    errors = extract_errors(sys.argv[1])

    output_errorconstantsh(errors)
    output_errorconstantscpp(errors)

if __name__ == '__main__':
    main()
