#!/usr/bin/python2
# -*- coding: utf-8; mode: Python; indent-tabs-mode: t; tab-width: 4; python-indent: 4 -*-

import sys
import os.path
import datetime

license="""\
/* Copyright (C) {year}  Olga Yakovleva <yakovleva.o.v@gmail.com> */

/* 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 2.1 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 <https://www.gnu.org/licenses/>. */
"""

hpp=license+"""
#ifndef RHVOICE_{NAME}_HPP
#define RHVOICE_{NAME}_HPP

#include \"str.hpp\"
#include \"fst.hpp\"
#include \"language.hpp\"

namespace RHVoice
{{
  class {name}_info: public language_info
  {{
  public:
    {name}_info(const std::string& data_path,const std::string& userdict_path);

    #ifdef WIN32
    unsigned short get_id() const
    {{
      return {langid};
    }}
    #endif

  private:
    std::shared_ptr<language> create_instance() const;
  }};

  class {name}: public language
  {{
  public:
    explicit {name}(const {name}_info& info_);

    const {name}_info& get_info() const
    {{
      return info;
    }}

    std::vector<std::string> get_word_transcription(const item& word) const;

  private:
    const {name}_info& info;
    const fst g2p_fst;
    const fst lseq_fst;
  }};
}}
#endif
"""

cpp=license+"""
#include \"core/path.hpp\"
#include \"core/utterance.hpp\"
#include \"core/relation.hpp\"
#include \"core/item.hpp\"
#include \"core/{name}.hpp\"

namespace RHVoice
{{
  {name}_info::{name}_info(const std::string& data_path,const std::string& userdict_path):
    language_info(\"{Name}\",data_path,userdict_path)
  {{
    set_alpha2_code(\"{alpha2code}\");
    set_alpha3_code(\"{alpha3code}\");
{alphabet_registration}\
{vowel_registration}\
  }}

  std::shared_ptr<language> {name}_info::create_instance() const
  {{
    return std::shared_ptr<language>(new {name}(*this));
  }}

  {name}::{name}(const {name}_info& info_):
    language(info_),
    info(info_),
    g2p_fst(path::join(info_.get_data_path(),\"g2p.fst\")),
    lseq_fst(path::join(info_.get_data_path(),\"lseq.fst\"))
  {{
  }}

  std::vector<std::string> {name}::get_word_transcription(const item& word) const
  {{
    std::vector<std::string> transcription;
    const std::string& name=word.get(\"name\").as<std::string>();
    if(word.has_feature(\"lseq\"))
      lseq_fst.translate(str::utf8_string_begin(name),str::utf8_string_end(name),std::back_inserter(transcription));
    else
      g2p_fst.translate(str::utf8_string_begin(name),str::utf8_string_end(name),std::back_inserter(transcription));
    return transcription;
  }}
}}
"""

if __name__=="__main__":
	letters=sorted(set(sys.argv[1].decode("utf-8")))
	vowels=sorted(set(sys.argv[2].decode("utf-8")))
	letter_ranges=list()
	first=ord(letters[0])
	last=first
	for letter in letters[1:]:
		code=ord(letter)
		if code!=(last+1):
			letter_ranges.append((first,last))
			first=code
		last=code
	letter_ranges.append((first,last))
	args={"name":sys.argv[3].lower()}
	args["NAME"]=args["name"].upper()
	args["Name"]=args["name"].title()
	args["langid"]=int(sys.argv[4],16)
	args["alpha3code"]=sys.argv[5]
	args["alpha2code"]=sys.argv[6] if (len(sys.argv)>6) else ""
	args["alphabet_registration"]="".join("    register_letter_range({},{});\n".format(first,last+1-first) for first,last in letter_ranges)
	args["vowel_registration"]="".join("    register_vowel_letter({});\n".format(ord(v)) for v in vowels)
	args["year"]=datetime.date.today().year
	with open(os.path.join("src","include","core",args["name"]+".hpp"),"wb") as f:
		f.write(hpp.format(**args))
	with open(os.path.join("src","core",args["name"]+".cpp"),"wb") as f:
		f.write(cpp.format(**args))
