%module lttoolbox

%include <lttoolbox/fst_processor.h>
%include <lttoolbox/lt_locale.h>


%typemap(in) (int argc, char **argv) {
  if (PyTuple_Check($input)) {
    int i = 0;
    $1 = PyTuple_Size($input);
    $2 = (char **) malloc(($1 + 1)*sizeof(char *));
    for (i = 0; i < $1; i++) {
      PyObject *py_obj = PyTuple_GetItem($input, i);
      if (PyUnicode_Check(py_obj)) {
        $2[i] = strdup(PyUnicode_AsUTF8(py_obj));
      }
      else {
        PyErr_SetString(PyExc_TypeError, "tuple must contain strings");
        free($2);
        return NULL;
      }
    }
    $2[i] = 0;
  } else {
    PyErr_SetString(PyExc_TypeError, "not a tuple");
    return NULL;
  }
}

%typemap(freearg) (int argc, char **argv) {
  free((char *) $2);
}

%inline%{
#define SWIG_FILE_WITH_INIT
#include <lttoolbox/fst_processor.h>
#include <lttoolbox/my_stdio.h>
#include <lttoolbox/lt_locale.h>

#include <unicode/ustdio.h>

#include <getopt.h>

class FST: public FSTProcessor
{
public:
  /**
   * Imitates functionality of lt-proc using file path
   */
  FST(char *dictionary_path)
  {
    FILE *dictionary = fopen(dictionary_path, "rb");
    load(dictionary);
    fclose(dictionary);
  }

  void lt_proc(int argc, char **argv, char *input_path, char *output_path)
  {
    InputFile input;
    input.open(input_path);
    UFILE* output = u_fopen(output_path, "w", NULL, NULL);
    int cmd = 0;
    int c = 0;
    optind = 1;
	GenerationMode bilmode = gm_unknown;
    while (true)
    {
      c = getopt(argc, argv, "abcdeglmnoptwxzCIW");
      if (c == -1)
      {
        break;
      }
      switch(c)
      {
	  case 'a':
	  case 'b':
	  case 'e':
	  case 'g':
	  case 'p':
	  case 't':
	  case 'x':
          if(cmd == 0) {
			  cmd = c;
		  } else if (cmd == 'g' && c == 'b') {
			  cmd = c;
		  }
          break;
	  case 'c':
		  setCaseSensitiveMode(true);
		  break;
	  case 'd':
		  if (cmd == 0) cmd = 'g';
		  bilmode = gm_all;
		  break;
	  case 'l':
		  if (cmd == 0) cmd = 'g';
		  bilmode = gm_tagged;
		  break;
	  case 'm':
		  if (cmd == 0) cmd = 'g';
		  bilmode = gm_tagged_nm;
		  break;
	  case 'n':
		  if (cmd == 0) cmd = 'g';
		  bilmode = gm_clean;
		  break;
	  case 'o':
		  if (cmd == 0) cmd = 'b';
		  setBiltransSurfaceForms(true);
		  break;
	  case 'w':
          setDictionaryCaseMode(true);
          break;
	  case 'z':
		  setNullFlush(true);
		  break;
	  case 'C':
		  if (cmd == 0) cmd = 'g';
		  bilmode = gm_carefulcase;
		  break;
	  case 'I':
		  setUseDefaultIgnoredChars(false);
		  break;
	  case 'W':
		  setDisplayWeightsMode(true);
		  break;
        default:
          break;
      }
    }

    switch(cmd)
    {
	case 'b':
		initBiltrans();
		bilingual(input, output, bilmode);
		break;

	case 'e':
		initDecomposition();
		analysis(input, output);
		break;

	case 'g':
		initGeneration();
		generation(input, output, bilmode);
		break;

	case 'p':
		initPostgeneration();
		postgeneration(input, output);
		break;

	case 't':
		initPostgeneration();
		transliteration(input, output);
		break;

	case 'x':
		initPostgeneration();
		intergeneration(input, output);
		break;

	case 'a':
	default:
		initAnalysis();
        analysis(input, output);
        break;
	}

    u_fclose(output);
  }
};

%}
